From ca5e0c6db0ee1b104158b973d9095d95d80de835 Mon Sep 17 00:00:00 2001 From: Wesley Wolfe Date: Sun, 6 Oct 2013 22:37:44 -0500 Subject: [PATCH] Add exception-resilience to reading UUID. Fixes BUKKIT-4833 When a "uid.dat" file is corrupt (empty or <16 bytes), WorldNBTStorage will silently fail to read and return null. Non-null behavior is expected everywhere that this value is used. This change will force a random UUID when the previous UUID cannot be read, and getUUID to no longer silently ignore read/write exceptions. --- .../net/minecraft/server/WorldNBTStorage.java | 50 ++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/minecraft/server/WorldNBTStorage.java b/src/main/java/net/minecraft/server/WorldNBTStorage.java index e9134b3297..321f65b951 100644 --- a/src/main/java/net/minecraft/server/WorldNBTStorage.java +++ b/src/main/java/net/minecraft/server/WorldNBTStorage.java @@ -8,7 +8,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.logging.Logger; // CraftBukkit start import java.util.UUID; @@ -247,25 +246,42 @@ public class WorldNBTStorage implements IDataManager, IPlayerFileData { // CraftBukkit start public UUID getUUID() { if (uuid != null) return uuid; + File file1 = new File(this.baseDir, "uid.dat"); + if (file1.exists()) { + DataInputStream dis = null; + try { + dis = new DataInputStream(new FileInputStream(file1)); + return uuid = new UUID(dis.readLong(), dis.readLong()); + } catch (IOException ex) { + MinecraftServer.getServer().getLogger().severe("Failed to read " + file1 + ", generating new random UUID", ex); + } finally { + if (dis != null) { + try { + dis.close(); + } catch (IOException ex) { + // NOOP + } + } + } + } + uuid = UUID.randomUUID(); + DataOutputStream dos = null; try { - File file1 = new File(this.baseDir, "uid.dat"); - if (!file1.exists()) { - DataOutputStream dos = new DataOutputStream(new FileOutputStream(file1)); - uuid = UUID.randomUUID(); - dos.writeLong(uuid.getMostSignificantBits()); - dos.writeLong(uuid.getLeastSignificantBits()); - dos.close(); + dos = new DataOutputStream(new FileOutputStream(file1)); + dos.writeLong(uuid.getMostSignificantBits()); + dos.writeLong(uuid.getLeastSignificantBits()); + } catch (IOException ex) { + MinecraftServer.getServer().getLogger().severe("Failed to write " + file1, ex); + } finally { + if (dos != null) { + try { + dos.close(); + } catch (IOException ex) { + // NOOP + } } - else { - DataInputStream dis = new DataInputStream(new FileInputStream(file1)); - uuid = new UUID(dis.readLong(), dis.readLong()); - dis.close(); - } - return uuid; - } - catch (IOException ex) { - return null; } + return uuid; } public File getPlayerDir() {