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.
This commit is contained in:
ISSOtm
2022-07-19 18:31:14 +02:00
parent 18e4f132a8
commit ab9945c1ee
3 changed files with 8 additions and 14 deletions

View File

@@ -13,8 +13,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#define RGBDS_OBJECT_VERSION_STRING "RGB%1u" #define RGBDS_OBJECT_VERSION_STRING "RGB9"
#define RGBDS_OBJECT_VERSION_NUMBER 9U
#define RGBDS_OBJECT_REV 9U #define RGBDS_OBJECT_REV 9U
enum AssertionType { enum AssertionType {

View File

@@ -532,7 +532,7 @@ void out_WriteObject(void)
/* Also write symbols that weren't written above */ /* Also write symbols that weren't written above */
sym_ForEach(registerUnregisteredSymbol, NULL); 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(RGBDS_OBJECT_REV, f);
putlong(nbSymbols, f); putlong(nbSymbols, f);

View File

@@ -500,20 +500,15 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
return; return;
} }
/* Begin by reading the magic bytes and version number */ /* Begin by reading the magic bytes */
unsigned versionNumber; int matchedElems;
int matchedElems = fscanf(file, RGBDS_OBJECT_VERSION_STRING,
&versionNumber);
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); errx("\"%s\" is not a RGBDS object file", fileName);
verbosePrint("Reading object file %s, version %u\n", verbosePrint("Reading object file %s\n",
fileName, versionNumber); fileName);
if (versionNumber != RGBDS_OBJECT_VERSION_NUMBER)
errx("\"%s\" is an incompatible version %u object file",
fileName, versionNumber);
uint32_t revNum; uint32_t revNum;