Fix potential error in the trim() function (#912)

String length returned at line #514 could be longer than 20, not good. Using strnlen instead, to avoid reading garbage bytes.
This commit is contained in:
Rikard Bengtsson
2024-08-27 12:30:39 +02:00
committed by GitHub
parent 7ba3d88a3b
commit f3736a4e13

View File

@@ -485,15 +485,15 @@ static uint8_t hex_to_dec(const char x) {
return 0;
}
static void trim(char* out, size_t len, const char* str)
static void trim(char* out, size_t max_len, const char* str)
{
if (!*str || !len) {
if (!*str || !max_len) {
*out = '\0';
return;
}
const char* end;
size_t out_size = len;
size_t out_size = max_len;
// Trim leading space
while (isspace(*str)) {
@@ -511,14 +511,14 @@ static void trim(char* out, size_t len, const char* str)
}
// Trim trailing space
end = str + strlen(str) - 1;
end = str + strnlen(str, max_len) - 1;
while (end > str && isspace(*end)) {
end--;
}
end++;
// Set output size to minimum of trimmed string length and buffer size minus 1
out_size = (size_t)(end - str) < (len - 1) ? (end - str) : len - 1;
out_size = (size_t)(end - str) < (max_len - 1) ? (end - str) : max_len - 1;
// Copy trimmed string and add null terminator
memcpy(out, str, out_size);
@@ -1725,4 +1725,4 @@ int n64_rom_tx(const char* name, unsigned char idx, uint32_t load_addr, uint32_t
#pragma pop_macro("NONE")
#pragma pop_macro("BIG_ENDIAN")
#pragma pop_macro("LITTLE_ENDIAN")
#pragma pop_macro("LITTLE_ENDIAN")