Skip to content
Permalink
Browse files

Harden JsonFileSessionStore against nulls/Gson oddities

  • Loading branch information...
kenzierocks committed Sep 23, 2019
1 parent 982caaf commit fa25ad22cda96470fe9d8073e59c50a48cb7f81f
@@ -88,7 +88,15 @@ public LocalSession load(UUID id) throws IOException {
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 LocalSession load(UUID id) throws IOException {

@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 void save(UUID id, LocalSession session) throws IOException {
}
}

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());
}

0 comments on commit fa25ad2

Please sign in to comment.
You can’t perform that action at this time.