From 346193da00eade284efb537c964d0b43df6cba83 Mon Sep 17 00:00:00 2001 From: Stas Sergeev Date: Thu, 9 Nov 2023 23:52:51 +0500 Subject: [PATCH] del: use findnext() to avoid leaks [fixes #84] It appears findnext() needs to be called until the search is completed, and only in that case djgpp closes the findfirst handle. Call findnext() before removing the file to stay on a safe side. Perhaps glob() would be a better solution. Using only findfirst() was fine before LFNs were enabled, because the SFN's findfirst() doesn't need to close searches. But with LFNs this eventually overflows the buffer. --- src/command.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/command.c b/src/command.c index 6155036..f42a52c 100644 --- a/src/command.c +++ b/src/command.c @@ -2054,10 +2054,12 @@ static void perform_date(const char *arg) static void perform_delete(const char *arg) { + long fhandle; finddata_t ff; char filespec[MAXPATH] = ""; char full_filespec[MAXPATH] = ""; char drive[MAXDRIVE], dir[MAXDIR]; + int done = 0; while (*arg != '\0') { @@ -2084,18 +2086,17 @@ static void perform_delete(const char *arg) return; } - if (findfirst_f(filespec, &ff, 0, NULL) != 0) - { - printf("File(s) not found - %s\n", filespec); // informational msg; not an error - return; - } - _fixpath(filespec, full_filespec); fnsplit(full_filespec, drive, dir, NULL, NULL); conv_unix_path_to_ms_dos(drive); conv_unix_path_to_ms_dos(dir); - while (findfirst_f(full_filespec, &ff, 0, NULL) == 0) + if (findfirst_f(full_filespec, &ff, 0, &fhandle) != 0) + { + printf("File(s) not found - %s\n", filespec); // informational msg; not an error + return; + } + while (!done) { char individual_filespec[MAXPATH]; @@ -2104,6 +2105,7 @@ static void perform_delete(const char *arg) strcat(individual_filespec, dir); strcat(individual_filespec, FINDDATA_T_FILENAME(ff)); + done = (findnext_f(&ff, fhandle) != 0); if (remove(individual_filespec) == 0) printf("%s erased\n", individual_filespec); else