diff --git a/indra/llcommon/llsdjson.cpp b/indra/llcommon/llsdjson.cpp index 5b49f247a..1a54a24e1 100644 --- a/indra/llcommon/llsdjson.cpp +++ b/indra/llcommon/llsdjson.cpp @@ -79,6 +79,17 @@ LLSD LlsdFromJson(const nlohmann::json &val) return result; } +LLSD LlsdFromJsonString(const std::string& str) +{ + auto json = nlohmann::json::parse(str, nullptr, false); + if (json.is_discarded()) + { + LL_WARNS() << "Cannot parse invalid json string:\n" << str << LL_ENDL; + return LLSD(); + } + return LlsdFromJson(json); +} + //========================================================================= nlohmann::json LlsdToJson(const LLSD &val) { diff --git a/indra/llcommon/llsdjson.h b/indra/llcommon/llsdjson.h index bc76acbc8..5c45f697e 100644 --- a/indra/llcommon/llsdjson.h +++ b/indra/llcommon/llsdjson.h @@ -54,6 +54,7 @@ /// For maps and arrays child entries will be converted and added to the structure. /// Order is preserved for an array but not for objects. LLSD LlsdFromJson(const nlohmann::json &val); +LLSD LlsdFromJsonString(const std::string& body); /// Convert an LLSD object into Parsed JSON object maintaining member names and /// array indexs. diff --git a/indra/newview/llmarketplacefunctions.cpp b/indra/newview/llmarketplacefunctions.cpp index 508a23229..55b7203cd 100644 --- a/indra/newview/llmarketplacefunctions.cpp +++ b/indra/newview/llmarketplacefunctions.cpp @@ -281,8 +281,12 @@ public: std::string body; decode_raw_body(channels, buffer, body); - auto json = nlohmann::json::parse(body); - LLSD result = LlsdFromJson(json);; + LLSD result = LlsdFromJsonString(body); + if (result.isUndefined()) + { + log_SLM_warning("Get /listings", getStatus(), getReason(), LLStringUtil::null, "Empty or Invalid JSON Response"); + return; + } if (!isGoodStatus(mStatus)) { @@ -345,8 +349,12 @@ public: std::string body; decode_raw_body(channels, buffer, body); - auto json = nlohmann::json::parse(body); - LLSD result = LlsdFromJson(json);; + LLSD result = LlsdFromJsonString(body); + if (result.isUndefined()) + { + log_SLM_warning("Post /listings", getStatus(), getReason(), LLStringUtil::null, "Empty or Invalid JSON Response"); + return; + } if (!isGoodStatus(mStatus)) { @@ -404,8 +412,12 @@ public: std::string body; decode_raw_body(channels, buffer, body); - auto json = nlohmann::json::parse(body); - LLSD result = LlsdFromJson(json); + LLSD result = LlsdFromJsonString(body); + if (result.isUndefined()) + { + log_SLM_warning("Get /listing", getStatus(), getReason(), LLStringUtil::null, "Empty or Invalid JSON Response"); + return; + } if (!isGoodStatus(mStatus)) { @@ -480,8 +492,12 @@ public: std::string body; decode_raw_body(channels, buffer, body); - auto json = nlohmann::json::parse(body); - LLSD result = LlsdFromJson(json); + LLSD result = LlsdFromJsonString(body); + if (result.isUndefined()) + { + log_SLM_warning("Put /listing", getStatus(), getReason(), LLStringUtil::null, "Empty or Invalid JSON Response"); + return; + } if (!isGoodStatus(mStatus)) { @@ -557,8 +573,12 @@ public: std::string body; decode_raw_body(channels, buffer, body); - auto json = nlohmann::json::parse(body); - LLSD result = LlsdFromJson(json); + LLSD result = LlsdFromJsonString(body); + if (result.isUndefined()) + { + log_SLM_warning("Put /associate_inventory", getStatus(), getReason(), LLStringUtil::null, "Empty or Invalid JSON Response"); + return; + } if (!isGoodStatus(mStatus)) { @@ -629,8 +649,12 @@ public: std::string body; decode_raw_body(channels, buffer, body); - auto json = nlohmann::json::parse(body); - LLSD result = LlsdFromJson(json); + LLSD result = LlsdFromJsonString(body); + if (result.isUndefined()) + { + log_SLM_warning("Delete /listing", getStatus(), getReason(), LLStringUtil::null, "Empty or Invalid JSON Response"); + return; + } if (!isGoodStatus(mStatus)) { diff --git a/indra/newview/llwebprofile.cpp b/indra/newview/llwebprofile.cpp index 92df639a6..02799cfd0 100644 --- a/indra/newview/llwebprofile.cpp +++ b/indra/newview/llwebprofile.cpp @@ -82,7 +82,13 @@ public: return; } - auto root = LlsdFromJson(nlohmann::json::parse(body)); + auto root = LlsdFromJsonString(body); + if (root.isUndefined()) + { + LL_WARNS() << "Failed to get valid json body" << LL_ENDL; + LLWebProfile::reportImageUploadStatus(false); + return; + } // *TODO: 404 = not supported by the grid // *TODO: increase timeout or handle HTTP_INTERNAL_ERROR_* time errors. diff --git a/indra/newview/shupdatechecker.cpp b/indra/newview/shupdatechecker.cpp index ba59fb530..e4582bbd4 100644 --- a/indra/newview/shupdatechecker.cpp +++ b/indra/newview/shupdatechecker.cpp @@ -48,7 +48,12 @@ public: return; } - auto root = nlohmann::json::parse(body); + auto root = nlohmann::json::parse(body, nullptr, false); + if (root.is_discarded()) + { + LL_WARNS() << "Failed to parse json string from body:\n" << body << LL_ENDL; + return; // TODO: Should we say something here for the user? + } std::string viewer_version = llformat("%s (%i)", LLVersionInfo::getShortVersion().c_str(), LLVersionInfo::getBuild());