Skip to content

Commit

Permalink
Merge pull request #18 from danra/fix_SetSearchPath
Browse files Browse the repository at this point in the history
Fix cf_SetSearchPath bad memory access
  • Loading branch information
Lgt2x authored Apr 18, 2024
2 parents 91fbd35 + c6e79c1 commit 578b574
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 11 additions & 6 deletions cfile/CFILE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,30 +182,35 @@ 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)
return 0;
// 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;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/CFILE.H
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 578b574

Please sign in to comment.