From fd4547376c05d31bea1b2f28303d5443212de47c Mon Sep 17 00:00:00 2001 From: abraunegg Date: Sun, 22 Sep 2019 06:40:39 +1000 Subject: [PATCH] Fix crash when resume_upload file is not a valid JSON (#664) * Validate that there is JSON data in the string before the string is read via parseJSON() * Add try block to catch file system exceptions if they are generated when attempting to update the file date & time --- src/sync.d | 14 ++++++++++++-- src/upload.d | 11 ++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/sync.d b/src/sync.d index 61a8463f..e5b864d0 100644 --- a/src/sync.d +++ b/src/sync.d @@ -1342,7 +1342,12 @@ final class SyncEngine // handle changed time if (newItem.type == ItemType.file && oldItem.mtime != newItem.mtime) { - setTimes(newPath, newItem.mtime, newItem.mtime); + try { + setTimes(newPath, newItem.mtime, newItem.mtime); + } catch (FileException e) { + // display the error message + displayFileSystemErrorMessage(e.msg); + } } } } @@ -1457,7 +1462,12 @@ final class SyncEngine if ((getSize(path) == fileSize) || (OneDriveFileHash == quickXorHash) || (OneDriveFileHash == sha1Hash)) { // downloaded matches either size or hash log.vdebug("Downloaded file matches reported size and or reported file hash"); - setTimes(path, item.mtime, item.mtime); + try { + setTimes(path, item.mtime, item.mtime); + } catch (FileException e) { + // display the error message + displayFileSystemErrorMessage(e.msg); + } } else { // size error? if (getSize(path) != fileSize) { diff --git a/src/upload.d b/src/upload.d index f2f86ab9..c78fd7ea 100644 --- a/src/upload.d +++ b/src/upload.d @@ -62,7 +62,16 @@ struct UploadSession { if (exists(sessionFilePath)) { log.vlog("Trying to restore the upload session ..."); - session = readText(sessionFilePath).parseJSON(); + // We cant use JSONType.object check, as this is currently a string + // We cant use a try & catch block, as it does not catch std.json.JSONException + auto sessionFileText = readText(sessionFilePath); + if(canFind(sessionFileText,"@odata.context")) { + session = readText(sessionFilePath).parseJSON(); + } else { + log.vlog("Upload session resume data is invalid"); + remove(sessionFilePath); + return false; + } // Check the session resume file for expirationDateTime if ("expirationDateTime" in session){