From ab9945c1eefb4a3b64365e10ff6e8cf610f7c52a Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Tue, 19 Jul 2022 18:31:14 +0200 Subject: [PATCH] Avoid using `fscanf` to detect RGBDS object files This function is made for text, e.g. accepts spaces, leading zeros, etc. before `%u`. This way checks that the correct amount of bytes are read instead. --- include/linkdefs.h | 3 +-- src/asm/output.c | 2 +- src/link/object.c | 17 ++++++----------- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/include/linkdefs.h b/include/linkdefs.h index cb6ce489..bd9cb99b 100644 --- a/include/linkdefs.h +++ b/include/linkdefs.h @@ -13,8 +13,7 @@ #include #include -#define RGBDS_OBJECT_VERSION_STRING "RGB%1u" -#define RGBDS_OBJECT_VERSION_NUMBER 9U +#define RGBDS_OBJECT_VERSION_STRING "RGB9" #define RGBDS_OBJECT_REV 9U enum AssertionType { diff --git a/src/asm/output.c b/src/asm/output.c index 8b1404c5..4ee9150b 100644 --- a/src/asm/output.c +++ b/src/asm/output.c @@ -532,7 +532,7 @@ void out_WriteObject(void) /* Also write symbols that weren't written above */ sym_ForEach(registerUnregisteredSymbol, NULL); - fprintf(f, RGBDS_OBJECT_VERSION_STRING, RGBDS_OBJECT_VERSION_NUMBER); + fprintf(f, RGBDS_OBJECT_VERSION_STRING); putlong(RGBDS_OBJECT_REV, f); putlong(nbSymbols, f); diff --git a/src/link/object.c b/src/link/object.c index 85fef2b8..e9350305 100644 --- a/src/link/object.c +++ b/src/link/object.c @@ -500,20 +500,15 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) return; } - /* Begin by reading the magic bytes and version number */ - unsigned versionNumber; - int matchedElems = fscanf(file, RGBDS_OBJECT_VERSION_STRING, - &versionNumber); + /* Begin by reading the magic bytes */ + int matchedElems; - if (matchedElems != 1) + if (fscanf(file, RGBDS_OBJECT_VERSION_STRING "%n", &matchedElems) == 1 + && matchedElems != strlen(RGBDS_OBJECT_VERSION_STRING)) errx("\"%s\" is not a RGBDS object file", fileName); - verbosePrint("Reading object file %s, version %u\n", - fileName, versionNumber); - - if (versionNumber != RGBDS_OBJECT_VERSION_NUMBER) - errx("\"%s\" is an incompatible version %u object file", - fileName, versionNumber); + verbosePrint("Reading object file %s\n", + fileName); uint32_t revNum;