From fa25ad22cda96470fe9d8073e59c50a48cb7f81f Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Mon, 23 Sep 2019 11:46:24 -0700 Subject: [PATCH] Harden JsonFileSessionStore against nulls/Gson oddities --- .../session/storage/JsonFileSessionStore.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java b/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java index ad8e422bf..aa2d579fd 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/session/storage/JsonFileSessionStore.java @@ -88,7 +88,15 @@ public class JsonFileSessionStore implements SessionStore { try (Closer closer = Closer.create()) { FileReader fr = closer.register(new FileReader(file)); BufferedReader br = closer.register(new BufferedReader(fr)); - return gson.fromJson(br, LocalSession.class); + LocalSession session = gson.fromJson(br, LocalSession.class); + if (session == null) { + log.warn("Loaded a null session from {}, creating new session", file); + if (!file.delete()) { + log.warn("Failed to delete corrupted session {}", file); + } + session = new LocalSession(); + } + return session; } catch (JsonParseException e) { throw new IOException(e); } catch (FileNotFoundException e) { @@ -98,6 +106,7 @@ public class JsonFileSessionStore implements SessionStore { @Override public void save(UUID id, LocalSession session) throws IOException { + checkNotNull(session); File finalFile = getPath(id); File tempFile = new File(finalFile.getParentFile(), finalFile.getName() + ".tmp"); @@ -115,6 +124,10 @@ public class JsonFileSessionStore implements SessionStore { } } + if (tempFile.length() == 0) { + throw new IllegalStateException("Gson wrote zero bytes"); + } + if (!tempFile.renameTo(finalFile)) { log.warn("Failed to rename temporary session file to " + finalFile.getPath()); }