From 69b12d907eeceabbe6e7ce157a6709727c5998ce Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 26 Jun 2020 09:53:55 +0200 Subject: [PATCH] tweaks: avoid unneeded calls of free() by reallocating the full name --- src/files.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/files.c b/src/files.c index 90ff6ad6..2490f56a 100644 --- a/src/files.c +++ b/src/files.c @@ -2407,9 +2407,10 @@ char **filename_completion(const char *buf, size_t length, size_t *num_matches) char *dirname = copy_of(buf); char *slash, *filename; size_t filenamelen; + char *fullname = NULL; char **matches = NULL; - DIR *dir; const struct dirent *entry; + DIR *dir; /* If there's a / in the name, split out filename and directory parts. */ slash = strrchr(dirname, '/'); @@ -2448,22 +2449,17 @@ char **filename_completion(const char *buf, size_t length, size_t *num_matches) if (strncmp(entry->d_name, filename, filenamelen) == 0 && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { - char *fullname = charalloc(strlen(dirname) + strlen(entry->d_name) + 1); + fullname = charealloc(fullname, strlen(dirname) + + strlen(entry->d_name) + 1); sprintf(fullname, "%s%s", dirname, entry->d_name); #ifdef ENABLE_OPERATINGDIR - if (outside_of_confinement(fullname, TRUE)) { - free(fullname); + if (outside_of_confinement(fullname, TRUE)) continue; - } #endif - if (currmenu == MGOTODIR && !is_dir(fullname)) { - free(fullname); + if (currmenu == MGOTODIR && !is_dir(fullname)) continue; - } - - free(fullname); matches = (char **)nrealloc(matches, (*num_matches + 1) * sizeof(char *)); matches[*num_matches] = copy_of(entry->d_name); @@ -2474,6 +2470,7 @@ char **filename_completion(const char *buf, size_t length, size_t *num_matches) closedir(dir); free(dirname); free(filename); + free(fullname); return matches; }