Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cf_SetSearchPath bad memory access (tested on macOS) #18

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading