diff --git a/include/asm/fstack.hpp b/include/asm/fstack.hpp index 62157a5c..45888f17 100644 --- a/include/asm/fstack.hpp +++ b/include/asm/fstack.hpp @@ -59,11 +59,7 @@ char const *fstk_GetFileName(); void fstk_AddIncludePath(char const *s); void fstk_SetPreIncludeFile(char const *s); -/* - * @param path The user-provided file name - * @return A pointer to the `new`-allocated full path, or `nullptr` if no path worked - */ -std::string *fstk_FindFile(char const *path); +std::optional fstk_FindFile(char const *path); bool yywrap(); void fstk_RunInclude(char const *path); diff --git a/src/asm/fstack.cpp b/src/asm/fstack.cpp index 01a026a9..25874f6d 100644 --- a/src/asm/fstack.cpp +++ b/src/asm/fstack.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -159,25 +160,19 @@ static bool isPathValid(char const *path) { return !S_ISDIR(statbuf.st_mode); } -std::string *fstk_FindFile(char const *path) { - std::string *fullPath = new (std::nothrow) std::string(); - - if (!fullPath) { - error("Failed to allocate string during include path search: %s\n", strerror(errno)); - } else { - for (std::string &str : includePaths) { - *fullPath = str + path; - if (isPathValid(fullPath->c_str())) { - printDep(fullPath->c_str()); - return fullPath; - } +std::optional fstk_FindFile(char const *path) { + for (std::string &str : includePaths) { + std::string fullPath = str + path; + if (isPathValid(fullPath.c_str())) { + printDep(fullPath.c_str()); + return fullPath; } } errno = ENOENT; if (generatedMissingIncludes) printDep(path); - return nullptr; + return std::nullopt; } bool yywrap() { @@ -265,8 +260,7 @@ static Context &newContext(FileStackNode &fileInfo) { } void fstk_RunInclude(char const *path) { - std::string *fullPath = fstk_FindFile(path); - + std::optional fullPath = fstk_FindFile(path); if (!fullPath) { if (generatedMissingIncludes) { if (verbose) @@ -279,7 +273,6 @@ void fstk_RunInclude(char const *path) { } FileStackNode *fileInfo = new (std::nothrow) FileStackNode(NODE_FILE, *fullPath); - delete fullPath; if (!fileInfo) { error("Failed to alloc file info for INCLUDE: %s\n", strerror(errno)); return; @@ -303,15 +296,13 @@ static void runPreIncludeFile() { if (!preIncludeName) return; - std::string *fullPath = fstk_FindFile(preIncludeName); - + std::optional fullPath = fstk_FindFile(preIncludeName); if (!fullPath) { error("Unable to open included file '%s': %s\n", preIncludeName, strerror(errno)); return; } FileStackNode *fileInfo = new (std::nothrow) FileStackNode(NODE_FILE, *fullPath); - delete fullPath; if (!fileInfo) { error("Failed to alloc file info for pre-include: %s\n", strerror(errno)); return; diff --git a/src/asm/section.cpp b/src/asm/section.cpp index 0e50b1bd..003c10d3 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -846,11 +846,8 @@ void sect_BinaryFile(char const *s, int32_t startPos) { if (!checkcodesection()) return; - std::string *fullPath = fstk_FindFile(s); + std::optional fullPath = fstk_FindFile(s); FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr; - - delete fullPath; - if (!f) { if (generatedMissingIncludes) { if (verbose) @@ -915,11 +912,8 @@ void sect_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length) { if (!reserveSpace(length)) return; - std::string *fullPath = fstk_FindFile(s); + std::optional fullPath = fstk_FindFile(s); FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr; - - delete fullPath; - if (!f) { if (generatedMissingIncludes) { if (verbose)