diff --git a/include/asm/fstack.hpp b/include/asm/fstack.hpp index a2815000..342664d7 100644 --- a/include/asm/fstack.hpp +++ b/include/asm/fstack.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include "asm/lexer.hpp" @@ -52,12 +53,9 @@ void fstk_AddIncludePath(char const *s); void fstk_SetPreIncludeFile(char const *s); /* * @param path The user-provided file name - * @param fullPath The address of a pointer, which will be made to point at the full path - * The pointer's value must be a valid argument to `realloc`, including NULL - * @param size Current size of the buffer, or 0 if the pointer is NULL - * @return True if the file was found, false if no path worked + * @return A pointer to the `new`-allocated full path, or NULL if no path worked */ -bool fstk_FindFile(char const *path, char **fullPath, size_t *size); +std::string *fstk_FindFile(char const *path); bool yywrap(void); void fstk_RunInclude(char const *path); diff --git a/src/asm/fstack.cpp b/src/asm/fstack.cpp index ab424fb5..979dea2e 100644 --- a/src/asm/fstack.cpp +++ b/src/asm/fstack.cpp @@ -4,9 +4,11 @@ #include #include #include +#include #include #include #include +#include #include "asm/fstack.hpp" #include "asm/macro.hpp" @@ -159,47 +161,20 @@ static bool isPathValid(char const *path) return !S_ISDIR(statbuf.st_mode); } -bool fstk_FindFile(char const *path, char **fullPath, size_t *size) +std::string *fstk_FindFile(char const *path) { - if (!*size) { - *size = 64; // This is arbitrary, really - *fullPath = (char *)realloc(*fullPath, *size); - if (!*fullPath) - error("realloc error during include path search: %s\n", - strerror(errno)); - } + std::string *fullPath = new(std::nothrow) std::string(); - if (*fullPath) { + if (!fullPath) { + error("Failed to allocate string during include path search: %s\n", strerror(errno)); + } else { for (size_t i = 0; i <= nbIncPaths; ++i) { - char const *incPath = i ? includePaths[i - 1] : ""; - int len = snprintf(*fullPath, *size, "%s%s", incPath, path); + *fullPath = i ? includePaths[i - 1] : ""; + *fullPath += path; - if (len < 0) { - error("snprintf error during include path search: %s\n", - strerror(errno)); - break; - } - - // Oh how I wish `asnprintf` was standard... - if ((size_t)len >= *size) { // `size` includes the terminator, `len` doesn't - *size = len + 1; - *fullPath = (char *)realloc(*fullPath, *size); - if (!*fullPath) { - error("realloc error during include path search: %s\n", - strerror(errno)); - break; - } - len = sprintf(*fullPath, "%s%s", incPath, path); - if (len < 0) { - error("sprintf error during include path search: %s\n", - strerror(errno)); - break; - } - } - - if (isPathValid(*fullPath)) { - printDep(*fullPath); - return true; + if (isPathValid(fullPath->c_str())) { + printDep(fullPath->c_str()); + return fullPath; } } } @@ -207,7 +182,7 @@ bool fstk_FindFile(char const *path, char **fullPath, size_t *size) errno = ENOENT; if (generatedMissingIncludes) printDep(path); - return false; + return NULL; } bool yywrap(void) @@ -320,11 +295,9 @@ static void newContext(struct FileStackNode *fileInfo) void fstk_RunInclude(char const *path) { - char *fullPath = NULL; - size_t size = 0; + std::string *fullPath = fstk_FindFile(path); - if (!fstk_FindFile(path, &fullPath, &size)) { - free(fullPath); + if (!fullPath) { if (generatedMissingIncludes) { if (verbose) printf("Aborting (-MG) on INCLUDE file '%s' (%s)\n", @@ -337,15 +310,15 @@ void fstk_RunInclude(char const *path) } struct FileStackNamedNode *fileInfo = - (struct FileStackNamedNode *)malloc(sizeof(*fileInfo) + size); + (struct FileStackNamedNode *)malloc(sizeof(*fileInfo) + fullPath->length() + 1); if (!fileInfo) { error("Failed to alloc file info for INCLUDE: %s\n", strerror(errno)); return; } fileInfo->node.type = NODE_FILE; - strcpy(fileInfo->name, fullPath); - free(fullPath); + strcpy(fileInfo->name, fullPath->c_str()); + delete fullPath; newContext((struct FileStackNode *)fileInfo); contextStack->lexerState = lexer_OpenFile(fileInfo->name); @@ -365,25 +338,23 @@ static void runPreIncludeFile(void) if (!preIncludeName) return; - char *fullPath = NULL; - size_t size = 0; + std::string *fullPath = fstk_FindFile(preIncludeName); - if (!fstk_FindFile(preIncludeName, &fullPath, &size)) { - free(fullPath); + if (!fullPath) { error("Unable to open included file '%s': %s\n", preIncludeName, strerror(errno)); return; } struct FileStackNamedNode *fileInfo = - (struct FileStackNamedNode *)malloc(sizeof(*fileInfo) + size); + (struct FileStackNamedNode *)malloc(sizeof(*fileInfo) + fullPath->length() + 1); if (!fileInfo) { error("Failed to alloc file info for pre-include: %s\n", strerror(errno)); return; } fileInfo->node.type = NODE_FILE; - strcpy(fileInfo->name, fullPath); - free(fullPath); + strcpy(fileInfo->name, fullPath->c_str()); + delete fullPath; newContext((struct FileStackNode *)fileInfo); contextStack->lexerState = lexer_OpenFile(fileInfo->name); diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 8796394d..bd3e18bd 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include "asm/fstack.hpp" @@ -807,13 +808,10 @@ void sect_BinaryFile(char const *s, int32_t startPos) if (!checkcodesection()) return; - char *fullPath = NULL; - size_t size = 0; - FILE *f = NULL; + std::string *fullPath = fstk_FindFile(s); + FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : NULL; - if (fstk_FindFile(s, &fullPath, &size)) - f = fopen(fullPath, "rb"); - free(fullPath); + delete fullPath; if (!f) { if (generatedMissingIncludes) { @@ -881,13 +879,10 @@ void sect_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length) if (!reserveSpace(length)) return; - char *fullPath = NULL; - size_t size = 0; - FILE *f = NULL; + std::string *fullPath = fstk_FindFile(s); + FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : NULL; - if (fstk_FindFile(s, &fullPath, &size)) - f = fopen(fullPath, "rb"); - free(fullPath); + delete fullPath; if (!f) { if (generatedMissingIncludes) {