fix(dir.c): Free allocations for path string

This commit is contained in:
Sau P 2024-12-02 22:15:34 +00:00 committed by agvxov
parent 498f4107d5
commit 2b7b6d8de2

View File

@ -278,15 +278,16 @@ static
void add_source_directory(char *dir) { void add_source_directory(char *dir) {
struct stat statstruct; struct stat statstruct;
char *compressed_path = compress_path(dir);
/* make sure it is a directory */ /* make sure it is a directory */
if(lstat(compress_path(dir), &statstruct) == 0 && S_ISDIR(statstruct.st_mode)) { if(lstat(compressed_path, &statstruct) == 0 && S_ISDIR(statstruct.st_mode)) {
/* note: there already is a source directory list */ /* note: there already is a source directory list */
if(nsrcdirs == msrcdirs) { if(nsrcdirs == msrcdirs) {
msrcdirs += DIRINC; msrcdirs += DIRINC;
srcdirs = realloc(srcdirs, msrcdirs * sizeof(*srcdirs)); srcdirs = realloc(srcdirs, msrcdirs * sizeof(*srcdirs));
} }
srcdirs[nsrcdirs++] = strdup(dir); srcdirs[nsrcdirs++] = compressed_path;
} }
} }
@ -295,8 +296,9 @@ static
void add_include_directory(char *name, char *path) { void add_include_directory(char *name, char *path) {
struct stat statstruct; struct stat statstruct;
char *compressed_path = compress_path(path);
/* make sure it is a directory */ /* make sure it is a directory */
if(lstat(compress_path(path), &statstruct) == 0 && S_ISDIR(statstruct.st_mode)) { if(lstat(compressed_path, &statstruct) == 0 && S_ISDIR(statstruct.st_mode)) {
if(incdirs == NULL) { if(incdirs == NULL) {
incdirs = malloc(mincdirs * sizeof(*incdirs)); incdirs = malloc(mincdirs * sizeof(*incdirs));
incnames = malloc(mincdirs * sizeof(*incnames)); incnames = malloc(mincdirs * sizeof(*incnames));
@ -305,7 +307,7 @@ void add_include_directory(char *name, char *path) {
incdirs = realloc(incdirs, mincdirs * sizeof(*incdirs)); incdirs = realloc(incdirs, mincdirs * sizeof(*incdirs));
incnames = realloc(incnames, mincdirs * sizeof(*incnames)); incnames = realloc(incnames, mincdirs * sizeof(*incnames));
} }
incdirs[nincdirs] = strdup(path); incdirs[nincdirs] = compressed_path;
incnames[nincdirs++] = strdup(name); incnames[nincdirs++] = strdup(name);
} }
} }
@ -354,10 +356,12 @@ void scan_dir(const char *adir, bool recurse_dir) {
* database */ * database */
static static
bool is_accessible_file(const char *file) { bool is_accessible_file(const char *file) {
if(access(compress_path(file), READ) == 0) {
struct stat stats;
if(lstat(file, &stats) == 0 && S_ISREG(stats.st_mode)) { return true; } if(access(file, READ) == 0) {
struct stat stats;
if(lstat(file, &stats) == 0 && S_ISREG(stats.st_mode)) {
return true;
}
} }
return false; return false;
} }
@ -597,7 +601,8 @@ void incfile(char *file, char *type) {
(int)(PATHLEN - 2 - file_len), (int)(PATHLEN - 2 - file_len),
incdirs[i], incdirs[i],
file); file);
if(access(compress_path(path), READ) == 0) {
if(access(path, READ) == 0) {
addsrcfile(path); addsrcfile(path);
break; break;
} }
@ -608,9 +613,17 @@ void incfile(char *file, char *type) {
bool infilelist(const char * path) { bool infilelist(const char * path) {
struct listitem *p; struct listitem *p;
for(p = srcnames[hash(compress_path(path)) % HASHMOD]; p != NULL; p = p->next) { char *dir_path = compress_path(path);
if(strequal(path, p->text)) { return (true); }
for(p = srcnames[hash(dir_path) % HASHMOD]; p != NULL; p = p->next) {
if(strequal(path, p->text)) {
free(dir_path);
return (true);
}
} }
free(dir_path);
return (false); return (false);
} }
@ -657,9 +670,11 @@ void addsrcfile(char *path) {
srcfiles = realloc(srcfiles, msrcfiles * sizeof(*srcfiles)); srcfiles = realloc(srcfiles, msrcfiles * sizeof(*srcfiles));
} }
/* add the file to the list */ /* add the file to the list */
srcfiles[nsrcfiles++] = strdup(compress_path(path)); char *dir_path = compress_path(path);
srcfiles[nsrcfiles++] = strdup(dir_path);
p = malloc(sizeof(*p)); p = malloc(sizeof(*p));
p->text = strdup(compress_path(path)); p->text = strdup(dir_path);
free(dir_path);
i = hash(p->text) % HASHMOD; i = hash(p->text) % HASHMOD;
p->next = srcnames[i]; p->next = srcnames[i];
srcnames[i] = p; srcnames[i] = p;