From 0c81b97ff26b577dd003daa1fa255b6b50464daf Mon Sep 17 00:00:00 2001 From: David Cernat Date: Thu, 19 Nov 2020 06:12:19 +0200 Subject: [PATCH] [General] Add error messages & shutdowns for invalid file reading --- scripts/cell/json.lua | 11 +++++++--- scripts/player/json.lua | 15 +++++++++----- scripts/recordstore/json.lua | 11 +++++++--- scripts/serverCore.lua | 40 +++++++++++++++++++++--------------- scripts/world/json.lua | 11 +++++++--- 5 files changed, 57 insertions(+), 31 deletions(-) diff --git a/scripts/cell/json.lua b/scripts/cell/json.lua index d15335ea07..c20e95e3bc 100644 --- a/scripts/cell/json.lua +++ b/scripts/cell/json.lua @@ -48,9 +48,14 @@ end function Cell:LoadFromDrive() self.data = jsonInterface.load("cell/" .. self.entryFile) - -- JSON doesn't allow numerical keys, but we use them, so convert - -- all string number keys into numerical keys - tableHelper.fixNumericalKeys(self.data) + if self.data == nil then + tes3mp.LogMessage(enumerations.log.ERROR, "cell/" .. self.entryFile .. " cannot be read!") + tes3mp.StopServer(2) + else + -- JSON doesn't allow numerical keys, but we use them, so convert + -- all string number keys into numerical keys + tableHelper.fixNumericalKeys(self.data) + end end -- Deprecated function with confusing name, kept around for backwards compatibility diff --git a/scripts/player/json.lua b/scripts/player/json.lua index dd926eded1..22805eed94 100644 --- a/scripts/player/json.lua +++ b/scripts/player/json.lua @@ -49,12 +49,17 @@ end function Player:LoadFromDrive() self.data = jsonInterface.load("player/" .. self.accountFile) - -- JSON doesn't allow numerical keys, but we use them, so convert - -- all string number keys into numerical keys - tableHelper.fixNumericalKeys(self.data) + if self.data == nil then + tes3mp.LogMessage(enumerations.log.ERROR, "player/" .. self.accountFile .. " cannot be read!") + tes3mp.StopServer(2) + else + -- JSON doesn't allow numerical keys, but we use them, so convert + -- all string number keys into numerical keys + tableHelper.fixNumericalKeys(self.data) - if self.data.login.password ~= nil and self.data.login.passwordHash == nil then - self:ConvertPlaintextPassword() + if self.data.login.password ~= nil and self.data.login.passwordHash == nil then + self:ConvertPlaintextPassword() + end end end diff --git a/scripts/recordstore/json.lua b/scripts/recordstore/json.lua index 9b86a1f94f..2112381c58 100644 --- a/scripts/recordstore/json.lua +++ b/scripts/recordstore/json.lua @@ -43,9 +43,14 @@ end function RecordStore:LoadFromDrive() self.data = jsonInterface.load("recordstore/" .. self.recordstoreFile) - -- JSON doesn't allow numerical keys, but we use them, so convert - -- all string number keys into numerical keys - tableHelper.fixNumericalKeys(self.data) + if self.data == nil then + tes3mp.LogMessage(enumerations.log.ERROR, "recordstore/" .. self.recordstoreFile .. " cannot be read!") + tes3mp.StopServer(2) + else + -- JSON doesn't allow numerical keys, but we use them, so convert + -- all string number keys into numerical keys + tableHelper.fixNumericalKeys(self.data) + end end -- Deprecated functions with confusing names, kept around for backwards compatibility diff --git a/scripts/serverCore.lua b/scripts/serverCore.lua index 1047b96718..05f3074384 100644 --- a/scripts/serverCore.lua +++ b/scripts/serverCore.lua @@ -297,31 +297,37 @@ function LoadDataFileList(filename) local jsonDataFileList = jsonInterface.load(filename) - -- Fix numerical keys to print plugins in the correct order - tableHelper.fixNumericalKeys(jsonDataFileList, true) + if jsonDataFileList == nil then + tes3mp.LogMessage(enumerations.log.ERROR, "Data file list " .. filename .. " cannot be read!") + tes3mp.StopServer(2) + else + -- Fix numerical keys to print plugins in the correct order + tableHelper.fixNumericalKeys(jsonDataFileList, true) - for listIndex, pluginEntry in ipairs(jsonDataFileList) do - for entryIndex, checksumStringArray in pairs(pluginEntry) do + for listIndex, pluginEntry in ipairs(jsonDataFileList) do + for entryIndex, checksumStringArray in pairs(pluginEntry) do - dataFileList[listIndex] = {} - dataFileList[listIndex].name = entryIndex + dataFileList[listIndex] = {} + dataFileList[listIndex].name = entryIndex - local checksums = {} - local debugMessage = ("- %d: \"%s\": ["):format(listIndex, entryIndex) + local checksums = {} + local debugMessage = ("- %d: \"%s\": ["):format(listIndex, entryIndex) - for _, checksumString in ipairs(checksumStringArray) do + for _, checksumString in ipairs(checksumStringArray) do - debugMessage = debugMessage .. ("%X, "):format(tonumber(checksumString, 16)) - table.insert(checksums, tonumber(checksumString, 16)) + debugMessage = debugMessage .. ("%X, "):format(tonumber(checksumString, 16)) + table.insert(checksums, tonumber(checksumString, 16)) + end + dataFileList[listIndex].checksums = checksums + table.insert(dataFileList[listIndex], "") + + debugMessage = debugMessage .. "\b\b]" + tes3mp.LogAppend(enumerations.log.WARN, debugMessage) end - dataFileList[listIndex].checksums = checksums - table.insert(dataFileList[listIndex], "") - - debugMessage = debugMessage .. "\b\b]" - tes3mp.LogAppend(enumerations.log.WARN, debugMessage) end + + return dataFileList end - return dataFileList end function OnRequestDataFileList() diff --git a/scripts/world/json.lua b/scripts/world/json.lua index 5658bc934b..6aaee3275c 100644 --- a/scripts/world/json.lua +++ b/scripts/world/json.lua @@ -52,9 +52,14 @@ function World:LoadFromDrive() self.coreVariables = jsonInterface.load("world/" .. self.coreVariablesFile) self.data = jsonInterface.load("world/" .. self.worldFile) - -- JSON doesn't allow numerical keys, but we use them, so convert - -- all string number keys into numerical keys - tableHelper.fixNumericalKeys(self.data) + if self.data == nil then + tes3mp.LogMessage(enumerations.log.ERROR, "world/" .. self.worldFile .. " cannot be read!") + tes3mp.StopServer(2) + else + -- JSON doesn't allow numerical keys, but we use them, so convert + -- all string number keys into numerical keys + tableHelper.fixNumericalKeys(self.data) + end end