From c5871576efcce807bb6f62fd956aa63d2588f2fc Mon Sep 17 00:00:00 2001 From: Magnus Ulf Date: Tue, 14 Jan 2020 14:17:10 +0100 Subject: [PATCH] Better handle invalid json --- .../massivecore/money/MoneyMixinVault.java | 6 +++--- src/com/massivecraft/massivecore/store/Coll.java | 6 +++++- .../massivecraft/massivecore/store/DriverFlatfile.java | 10 +++++++++- src/com/massivecraft/massivecore/store/MStore.java | 2 ++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/com/massivecraft/massivecore/money/MoneyMixinVault.java b/src/com/massivecraft/massivecore/money/MoneyMixinVault.java index 7477736f..95c35cf3 100644 --- a/src/com/massivecraft/massivecore/money/MoneyMixinVault.java +++ b/src/com/massivecraft/massivecore/money/MoneyMixinVault.java @@ -10,7 +10,7 @@ import org.bukkit.plugin.RegisteredServiceProvider; import java.util.Collection; -public class MoneyMixinVault extends MoneyMixinAbstract +public class MoneyMixinVault extends MoneyMixinAbstract { // -------------------------------------------- // // INSTANCE & CONSTRUCT @@ -255,8 +255,8 @@ public class MoneyMixinVault extends MoneyMixinAbstract public static Object getEconomyObject(String accountId) { if (null == accountId) return null; - // Because of Factions we have this crazy-workaround. - // Where offlineplayers will be used when possible + // Because of Factions we have this crazy-workaround + // where offlineplayers will be used when possible // but otherwise names will. OfflinePlayer ret = IdUtil.getOfflinePlayer(accountId); if (ret != null) return ret; diff --git a/src/com/massivecraft/massivecore/store/Coll.java b/src/com/massivecraft/massivecore/store/Coll.java index 620f0ca2..0ad225bd 100644 --- a/src/com/massivecraft/massivecore/store/Coll.java +++ b/src/com/massivecraft/massivecore/store/Coll.java @@ -14,7 +14,6 @@ import com.massivecraft.massivecore.mixin.MixinModification; import com.massivecraft.massivecore.store.migrator.MigratorUtil; import com.massivecraft.massivecore.util.MUtil; import com.massivecraft.massivecore.util.Txt; -import com.mongodb.DB; import org.apache.commons.lang.StringUtils; import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; @@ -384,6 +383,11 @@ public class Coll> extends CollAbstract this.logLoadError(id, "Raw data was JSON null. It seems you have a file containing just the word \"null\". Why would you do this?"); return false; } + if (raw == MStore.INVALID_JSON) + { + this.logLoadError(id, "Does not contain valid json."); + return false; + } return true; } diff --git a/src/com/massivecraft/massivecore/store/DriverFlatfile.java b/src/com/massivecraft/massivecore/store/DriverFlatfile.java index 9ed003d9..bc696934 100644 --- a/src/com/massivecraft/massivecore/store/DriverFlatfile.java +++ b/src/com/massivecraft/massivecore/store/DriverFlatfile.java @@ -3,6 +3,7 @@ package com.massivecraft.massivecore.store; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import com.google.gson.JsonSyntaxException; import com.massivecraft.massivecore.collections.MassiveMap; import com.massivecraft.massivecore.collections.MassiveSet; import com.massivecraft.massivecore.util.DiscUtil; @@ -172,7 +173,14 @@ public class DriverFlatfile extends DriverAbstract content = content.trim(); if (content.length() == 0) return null; - return new JsonParser().parse(content); + try + { + return new JsonParser().parse(content); + } + catch (JsonSyntaxException ex) + { + return MStore.INVALID_JSON; + } } @Override diff --git a/src/com/massivecraft/massivecore/store/MStore.java b/src/com/massivecraft/massivecore/store/MStore.java index eb7570a3..eab954f8 100644 --- a/src/com/massivecraft/massivecore/store/MStore.java +++ b/src/com/massivecraft/massivecore/store/MStore.java @@ -1,6 +1,7 @@ package com.massivecraft.massivecore.store; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.massivecraft.massivecore.ConfServer; import com.massivecraft.massivecore.entity.MassiveCoreMConf; @@ -18,6 +19,7 @@ public class MStore // This class also serves the purpose of containing database related constants. public static final boolean DEBUG_ENABLED = false; + public static final JsonObject INVALID_JSON = new JsonObject(); // -------------------------------------------- // // DRIVER REGISTRY