13
0
geforkt von Mirrors/Paper

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.
Dieser Commit ist enthalten in:
Wesley Wolfe 2013-10-06 22:37:44 -05:00
Ursprung cfa5490a58
Commit ca5e0c6db0

Datei anzeigen

@ -8,7 +8,6 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.logging.Logger;
// CraftBukkit start // CraftBukkit start
import java.util.UUID; import java.util.UUID;
@ -247,25 +246,42 @@ public class WorldNBTStorage implements IDataManager, IPlayerFileData {
// CraftBukkit start // CraftBukkit start
public UUID getUUID() { public UUID getUUID() {
if (uuid != null) return uuid; 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 { try {
File file1 = new File(this.baseDir, "uid.dat"); dos = new DataOutputStream(new FileOutputStream(file1));
if (!file1.exists()) { dos.writeLong(uuid.getMostSignificantBits());
DataOutputStream dos = new DataOutputStream(new FileOutputStream(file1)); dos.writeLong(uuid.getLeastSignificantBits());
uuid = UUID.randomUUID(); } catch (IOException ex) {
dos.writeLong(uuid.getMostSignificantBits()); MinecraftServer.getServer().getLogger().severe("Failed to write " + file1, ex);
dos.writeLong(uuid.getLeastSignificantBits()); } finally {
dos.close(); 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() { public File getPlayerDir() {