From 2658d0f7851945b2650641d2b5a0ef1653aef351 Mon Sep 17 00:00:00 2001 From: Tixx <83774803+WiserTixx@users.noreply.github.com> Date: Thu, 16 Jan 2025 14:21:43 +0100 Subject: [PATCH] Fix segfault by switching from rapidjson to nlohmann::json --- src/THeartbeatThread.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/THeartbeatThread.cpp b/src/THeartbeatThread.cpp index 34e0346..36cdde1 100644 --- a/src/THeartbeatThread.cpp +++ b/src/THeartbeatThread.cpp @@ -24,12 +24,8 @@ #include "Http.h" // #include "SocketIO.h" #include -#include -#include #include -namespace json = rapidjson; - void THeartbeatThread::operator()() { RegisterThread("Heartbeat"); std::string Body; @@ -63,7 +59,7 @@ void THeartbeatThread::operator()() { auto Target = "/heartbeat"; unsigned int ResponseCode = 0; - json::Document Doc; + nlohmann::json Doc; bool Ok = false; for (const auto& Url : Application::GetBackendUrlsInOrder()) { T = Http::POST(Url + Target, Body, "application/json", &ResponseCode, { { "api-v", "2" } }); @@ -72,8 +68,8 @@ void THeartbeatThread::operator()() { beammp_debug("Backend response was: `" + T + "`"); } - Doc.Parse(T.data(), T.size()); - if (Doc.HasParseError() || !Doc.IsObject()) { + Doc = nlohmann::json::parse(T, nullptr, false); + if (Doc.is_discarded() || !Doc.is_object()) { if (!Application::Settings.getAsBool(Settings::Key::General_Private)) { beammp_trace("Backend response failed to parse as valid json"); } @@ -94,18 +90,18 @@ void THeartbeatThread::operator()() { const auto MessageKey = "msg"; if (Ok) { - if (Doc.HasMember(StatusKey) && Doc[StatusKey].IsString()) { - Status = Doc[StatusKey].GetString(); + if (Doc.contains(StatusKey) && Doc[StatusKey].is_string()) { + Status = Doc[StatusKey]; } else { Ok = false; } - if (Doc.HasMember(CodeKey) && Doc[CodeKey].IsString()) { - Code = Doc[CodeKey].GetString(); + if (Doc.contains(CodeKey) && Doc[CodeKey].is_string()) { + Code = Doc[CodeKey]; } else { Ok = false; } - if (Doc.HasMember(MessageKey) && Doc[MessageKey].IsString()) { - Message = Doc[MessageKey].GetString(); + if (Doc.contains(MessageKey) && Doc[MessageKey].is_string()) { + Message = Doc[MessageKey]; } else { Ok = false; }