From c44b336e1b64337aa23584562aec3782d0fbc168 Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Wed, 28 Feb 2024 22:50:14 -0500 Subject: [PATCH] Use `std::vector` for SDCC object line buffer --- src/link/sdas_obj.cpp | 45 +++++++++++++++---------------------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/link/sdas_obj.cpp b/src/link/sdas_obj.cpp index 776084a2..fd2407fb 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -32,10 +32,10 @@ static void consumeLF(struct FileStackNode const *where, uint32_t lineNo, FILE * static char const *delim = " \f\n\r\t\v"; // Whitespace according to the C and POSIX locales -static int nextLine(char **restrict lineBuf, size_t *restrict bufLen, uint32_t *restrict lineNo, +static int nextLine(std::vector &lineBuf, uint32_t &lineNo, struct FileStackNode const *where, FILE *file) { retry: - ++*lineNo; + ++lineNo; int firstChar = getc(file); switch (firstChar) { @@ -50,36 +50,25 @@ retry: // fallthrough case '\r': if (firstChar == '\r' && getc(file) != '\n') - consumeLF(where, *lineNo, file); + consumeLF(where, lineNo, file); // fallthrough case '\n': goto retry; } - size_t i = 0; - for (;;) { - if (i >= *bufLen) { - assert(*bufLen != 0); - *bufLen *= 2; - *lineBuf = (char *)realloc(*lineBuf, *bufLen); - if (!*lineBuf) - fatal(where, *lineNo, "Failed to realloc: %s", strerror(errno)); - } - int c = getc(file); switch (c) { case '\r': - consumeLF(where, *lineNo, file); + consumeLF(where, lineNo, file); // fallthrough case '\n': case EOF: - (*lineBuf)[i] = '\0'; // Terminate the string (space was ensured above) + lineBuf.push_back('\0'); // Terminate the string (space was ensured above) return firstChar; } - (*lineBuf)[i] = c; - ++i; + lineBuf.push_back(c); } } @@ -145,8 +134,7 @@ enum RelocFlags { }; void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector &fileSymbols) { - size_t bufLen = 256; - char *line = (char *)malloc(bufLen); + std::vector line(256); char const *token; #define getToken(ptr, ...) do { \ @@ -166,10 +154,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector