Fix buffer overruns in update checker's network-facing code. (#888)

* fix buffer overrun when formatting remote version string

* fix buffer overrun when null-terminating the latest version string

* Update update_checker.c

---------

Co-authored-by: PeachyPeach <72323920+PeachyPeachSM64@users.noreply.github.com>
This commit is contained in:
Chase Bradley 2025-10-16 18:24:11 -04:00 committed by GitHub
parent b4c8f023d2
commit fc7a2f5557
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -46,11 +46,13 @@ size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) {
void parse_version(const char *data) { void parse_version(const char *data) {
const char *version = strstr(data, VERSION_IDENTIFIER); const char *version = strstr(data, VERSION_IDENTIFIER);
if (version == NULL) { return; } if (version == NULL) { return; }
u8 len = strlen(VERSION_IDENTIFIER); size_t len = strlen(VERSION_IDENTIFIER);
version += len; version += len;
const char *end = strchr(version, '"'); const char *end = strchr(version, '"');
memcpy(sRemoteVersion, version, end - version); size_t versionLength = (size_t)(end - version);
sRemoteVersion[end - version] = '\0'; if (versionLength > sizeof(sRemoteVersion) - 1) { return; }
memcpy(sRemoteVersion, version, versionLength);
sRemoteVersion[versionLength] = '\0';
} }
// function to download a text file from the internet // function to download a text file from the internet
@ -80,9 +82,9 @@ void get_version_remote(void) {
DWORD dwSize = sizeof(contentLength); DWORD dwSize = sizeof(contentLength);
HttpQueryInfo(hUrl, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &contentLength, &dwSize, NULL); HttpQueryInfo(hUrl, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &contentLength, &dwSize, NULL);
// read data from the URL // read data from the URL, making room in the buffer for the null-terminator
DWORD bytesRead; DWORD bytesRead;
if (!InternetReadFile(hUrl, buffer, sizeof(buffer), &bytesRead)) { if (!InternetReadFile(hUrl, buffer, sizeof(buffer) - 1, &bytesRead)) {
printf("Failed to check for updates!\n"); printf("Failed to check for updates!\n");
InternetCloseHandle(hInternet); InternetCloseHandle(hInternet);
InternetCloseHandle(hUrl); InternetCloseHandle(hUrl);