Fix crash (#413)

This PR fixes the segfault caused by rapidjson on the first heartbeat,
and the memory leak that happens during mod hashing.

---

By creating this pull request, I understand that code that is AI
generated or otherwise automatically generated may be rejected without
further discussion.
I declare that I fully understand all code I pushed into this PR, and
wrote all this code myself and own the rights to this code.
This commit is contained in:
Tixx 2025-01-18 21:36:54 +01:00 committed by GitHub
commit e90f1af109
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 14 deletions

View File

@ -84,7 +84,7 @@ std::string Http::POST(const std::string& url, const std::string& body, const st
list = curl_slist_append(list, ("Content-Type: " + ContentType).c_str()); list = curl_slist_append(list, ("Content-Type: " + ContentType).c_str());
for (auto [header, value] : headers) { for (auto [header, value] : headers) {
list = curl_slist_append(list, (header + value).c_str()); list = curl_slist_append(list, (header + ": " + value).c_str());
} }
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);

View File

@ -24,12 +24,8 @@
#include "Http.h" #include "Http.h"
// #include "SocketIO.h" // #include "SocketIO.h"
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <rapidjson/document.h>
#include <rapidjson/rapidjson.h>
#include <sstream> #include <sstream>
namespace json = rapidjson;
void THeartbeatThread::operator()() { void THeartbeatThread::operator()() {
RegisterThread("Heartbeat"); RegisterThread("Heartbeat");
std::string Body; std::string Body;
@ -63,7 +59,7 @@ void THeartbeatThread::operator()() {
auto Target = "/heartbeat"; auto Target = "/heartbeat";
unsigned int ResponseCode = 0; unsigned int ResponseCode = 0;
json::Document Doc; nlohmann::json Doc;
bool Ok = false; bool Ok = false;
for (const auto& Url : Application::GetBackendUrlsInOrder()) { for (const auto& Url : Application::GetBackendUrlsInOrder()) {
T = Http::POST(Url + Target, Body, "application/json", &ResponseCode, { { "api-v", "2" } }); 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 + "`"); beammp_debug("Backend response was: `" + T + "`");
} }
Doc.Parse(T.data(), T.size()); Doc = nlohmann::json::parse(T, nullptr, false);
if (Doc.HasParseError() || !Doc.IsObject()) { if (Doc.is_discarded() || !Doc.is_object()) {
if (!Application::Settings.getAsBool(Settings::Key::General_Private)) { if (!Application::Settings.getAsBool(Settings::Key::General_Private)) {
beammp_trace("Backend response failed to parse as valid json"); beammp_trace("Backend response failed to parse as valid json");
} }
@ -94,18 +90,18 @@ void THeartbeatThread::operator()() {
const auto MessageKey = "msg"; const auto MessageKey = "msg";
if (Ok) { if (Ok) {
if (Doc.HasMember(StatusKey) && Doc[StatusKey].IsString()) { if (Doc.contains(StatusKey) && Doc[StatusKey].is_string()) {
Status = Doc[StatusKey].GetString(); Status = Doc[StatusKey];
} else { } else {
Ok = false; Ok = false;
} }
if (Doc.HasMember(CodeKey) && Doc[CodeKey].IsString()) { if (Doc.contains(CodeKey) && Doc[CodeKey].is_string()) {
Code = Doc[CodeKey].GetString(); Code = Doc[CodeKey];
} else { } else {
Ok = false; Ok = false;
} }
if (Doc.HasMember(MessageKey) && Doc[MessageKey].IsString()) { if (Doc.contains(MessageKey) && Doc[MessageKey].is_string()) {
Message = Doc[MessageKey].GetString(); Message = Doc[MessageKey];
} else { } else {
Ok = false; Ok = false;
} }

View File

@ -121,6 +121,8 @@ void TResourceManager::RefreshFiles() {
} }
EVP_MD_CTX_free(mdctx); EVP_MD_CTX_free(mdctx);
stream.close();
std::string result; std::string result;
for (size_t i = 0; i < sha256_len; i++) { for (size_t i = 0; i < sha256_len; i++) {
result += fmt::format("{:02x}", sha256_value[i]); result += fmt::format("{:02x}", sha256_value[i]);