From ccf994643bc02fba0ceacbd00f4eeb822fdfbfa1 Mon Sep 17 00:00:00 2001 From: AZero13 <83477269+SiliconA-Z@users.noreply.github.com> Date: Thu, 2 Apr 2026 17:41:05 -0400 Subject: [PATCH] Fix off-by-one in gbagfx We malloc one byte too little. We need to account for the null terminator. --- tools/gbagfx/main.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/tools/gbagfx/main.c b/tools/gbagfx/main.c index 497a869e14..1f02df9198 100644 --- a/tools/gbagfx/main.c +++ b/tools/gbagfx/main.c @@ -641,22 +641,16 @@ int main(int argc, char **argv) if (*outputFileExtension == 0) FATAL_ERROR("Output file \"%s\" has no extension.\n", outputPath); - size_t newOutputPathSize = strlen(inputPath) - strlen(inputFileExtension) + strlen(outputFileExtension); + int prefixLen = inputFileExtension - inputPath; + size_t newOutputPathSize = prefixLen + strlen(outputFileExtension) + 1; + outputPath = malloc(newOutputPathSize); if (outputPath == NULL) FATAL_ERROR("Failed to allocate memory for new output path.\n"); - for (int i = 0; i < newOutputPathSize; i++) - { - outputPath[i] = inputPath[i]; - - if (outputPath[i] == '.') - { - strcpy(&outputPath[i + 1], outputFileExtension); - break; - } - } + if (snprintf(outputPath, newOutputPathSize, "%.*s%s", prefixLen, inputPath, outputFileExtension) >= (int)newOutputPathSize) + FATAL_ERROR("Failed to build output path.\n"); } for (int i = 0; handlers[i].function != NULL; i++)