Skip to content

Commit

Permalink
fix a potential crash in PlaylistCounts
Browse files Browse the repository at this point in the history
  • Loading branch information
p0358 committed Jul 30, 2024
1 parent 932dac9 commit 5cb30f5
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions bmedll/PlaylistCounts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "TTFSDK.h"
#include "SquirrelManager.h"
#include "ConCommandManager.h"
#include "tier0.h"
#include "_version.h"

std::string g_playlistCounts_etag;
Expand Down Expand Up @@ -40,25 +41,27 @@ DWORD WINAPI loadPlaylistCounts(PVOID pThreadParameter)

if (!IsSDKReady())
{
spdlog::warn("Loading playlist counts failure, game is already shutting down or SDK not ready");
spdlog::warn("[PlaylistCounts] Loading playlist counts failure, game is already shutting down or SDK not ready");
curl_easy_cleanup(curl);
return 0;
}

long response_code;
long response_code = 0;
//double elapsed;
//char* url;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
//curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &elapsed);
//curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);

std::string content_type, etag;
struct curl_header* header;
curl_easy_header(curl, "Content-Type", 0, CURLH_HEADER, -1, &header);
if (header && header->value && header->value[0])
struct curl_header* header = nullptr;
CURLHcode hres;
hres = curl_easy_header(curl, "Content-Type", 0, CURLH_HEADER, -1, &header);
if (hres == CURLHE_OK && header && header->value && header->value[0])
content_type.assign(header->value);
curl_easy_header(curl, "Etag", 0, CURLH_HEADER, -1, &header);
if (header && header->value && header->value[0])
header = nullptr;
hres = curl_easy_header(curl, "Etag", 0, CURLH_HEADER, -1, &header);
if (hres == CURLHE_OK && header && header->value && header->value[0])
etag.assign(header->value);

curl_easy_cleanup(curl);
Expand All @@ -67,12 +70,12 @@ DWORD WINAPI loadPlaylistCounts(PVOID pThreadParameter)
{
if (content_type != "application/json")
{
spdlog::error("Wrong content-type for playlists ({}) despite response code 200", content_type);
spdlog::error("[PlaylistCounts] Wrong content-type for playlists ({}) despite response code 200", content_type);
return 0;
}
if (etag.size())
{
spdlog::info("Received new playlists with etag: `{}` (response length: {})", etag, response_string.size());
spdlog::info("[PlaylistCounts] Received new playlists with etag: `{}` (response length: {})", etag, response_string.size());
g_playlistCounts_etag.assign(etag);
}
std::shared_ptr<rapidjson::Document> newJson = std::make_shared<rapidjson::Document>();
Expand All @@ -86,19 +89,19 @@ DWORD WINAPI loadPlaylistCounts(PVOID pThreadParameter)
}
else
{
spdlog::error("Parse error for playlists JSON despite response code 200 and correct content-type. Contents: {}", response_string);
spdlog::error("[PlaylistCounts] Parse error for playlists JSON despite response code 200 and correct content-type. Contents: {}", response_string);
}
}
else if (response_code == 304)
{
spdlog::info("Playlists did not change since last refresh (304 Not Modified)");
spdlog::info("[PlaylistCounts] Playlists did not change since last refresh (304 Not Modified)");
}
else
{
if (res != CURLE_OK)
spdlog::error("curl_easy_perform() failed for playlists: {}", curl_easy_strerror(res));
spdlog::error("[PlaylistCounts] curl_easy_perform() failed for playlists: {}", curl_easy_strerror(res));
else
spdlog::error("Unexpected response code for playlists (expected 200 or 304): {}", response_code);
spdlog::error("[PlaylistCounts] Unexpected response code for playlists (expected 200 or 304): {}", response_code);
}
}

Expand All @@ -107,7 +110,7 @@ DWORD WINAPI loadPlaylistCounts(PVOID pThreadParameter)

SQInteger Script_BME_RefreshPlaylistCounts(HSQUIRRELVM v)
{
spdlog::info("Refreshing BME playlist counts...");
spdlog::info("[PlaylistCounts] Refreshing BME playlist counts...");
CreateThread(0, 0, loadPlaylistCounts, 0, 0, NULL);
return 0;
}
Expand Down

0 comments on commit 5cb30f5

Please sign in to comment.