From aaefe86a2a1b3b719e90a8290e5364f8d798b84b Mon Sep 17 00:00:00 2001 From: Dan Raviv Date: Tue, 16 Apr 2024 15:19:40 -0700 Subject: [PATCH] Fix cf_SetSearchPath bad memory access (tested on macOS) --- cfile/CFILE.cpp | 17 +++++++++++------ lib/CFILE.H | 8 ++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cfile/CFILE.cpp b/cfile/CFILE.cpp index 142a7f452..7932b49e1 100644 --- a/cfile/CFILE.cpp +++ b/cfile/CFILE.cpp @@ -182,7 +182,7 @@ void cf_Close() { // NULL-terminated // list of file extensions, & the dir will only be searched // for files with a matching extension Returns: true if directory added, else false -int cf_SetSearchPath(const char *path, char *ext, ...) { +int cf_SetSearchPath(const char *path, ...) { if (strlen(path) >= _MAX_PATH) return 0; if (N_paths >= MAX_PATHS) @@ -190,22 +190,27 @@ int cf_SetSearchPath(const char *path, char *ext, ...) { // Get & store full path ddio_GetFullPath(paths[N_paths].path, path); // Set extenstions for this path + va_list exts; + va_start(exts, path); + const char *ext = va_arg(exts, const char *); if (ext == NULL) paths[N_paths].specific = 0; else { - char **ep = &ext; paths[N_paths].specific = 1; - while (*ep != NULL) { - if (N_extensions >= MAX_EXTENSIONS) + while (ext != NULL) { + if (N_extensions >= MAX_EXTENSIONS) { + va_end(exts); return 0; - strncpy(extensions[N_extensions].ext, *ep, _MAX_EXT); + } + strncpy(extensions[N_extensions].ext, ext, _MAX_EXT); extensions[N_extensions].pathnum = N_paths; N_extensions++; - ep++; + ext = va_arg(exts, const char *); } } // This path successfully set N_paths++; + va_end(exts); return 1; } diff --git a/lib/CFILE.H b/lib/CFILE.H index 1d4c5d966..6dd117656 100644 --- a/lib/CFILE.H +++ b/lib/CFILE.H @@ -123,11 +123,11 @@ int cf_OpenLibrary(const char *libname); void cf_CloseLibrary(int handle); // Specify a directory to look in for files -// if ext==NULL, look in this directory for all files. If ext is non-null, -// it is a NULL-terminated list of file extensions. If extensions are -// specifed, the directory will only be searched for files that match +// Variable arguments is a NULL-terminated list of extensions +// If no extensions are specified, look in this directory for all files. +// Otherwise, the directory will only be searched for files that match // one of the listed extensions. -int cf_SetSearchPath(const char *path, char *ext, ...); +int cf_SetSearchPath(const char *path, ...); // Removes all search paths that have been added by cf_SetSearchPath void cf_ClearAllSearchPaths(void);