From cbd1f30de95e0eea3b8ed9852af75bf846d1648d Mon Sep 17 00:00:00 2001 From: actondev Date: Thu, 29 May 2025 22:41:44 +0200 Subject: [PATCH 1/2] clang-format nfd_zenity.c --- src/nfd_zenity.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/nfd_zenity.c b/src/nfd_zenity.c index 43ccc6d..ea26a64 100644 --- a/src/nfd_zenity.c +++ b/src/nfd_zenity.c @@ -60,8 +60,7 @@ AddFiltersToCommandArgs(char** commandArgs, int commandArgsLen, const char* filt if (*p_filterList == ';' || *p_filterList == '\0') { /* end of filter -- add it to the dialog */ - for (i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++) - ; + for (i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++); commandArgs[i] = strdup(filterName); @@ -81,8 +80,7 @@ AddFiltersToCommandArgs(char** commandArgs, int commandArgsLen, const char* filt /* always append a wildcard option to the end*/ - for (i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++) - ; + for (i = 0; commandArgs[i] != NULL && i < commandArgsLen; i++); commandArgs[i] = strdup("--file-filter=*.*"); } @@ -103,8 +101,7 @@ ZenityCommon(char** command, strcat(tmp, defaultPath); int i; - for (i = 0; command[i] != NULL && i < commandLen; i++) - ; + for (i = 0; command[i] != NULL && i < commandLen; i++); command[i] = tmp; } @@ -278,7 +275,7 @@ NFD_PickFolder(const nfdchar_t* defaultPath, nfdchar_t** outPath) command[2] = strdup("--directory"); command[3] = strdup("--title=Select folder"); - char* stdOut = NULL; + char* stdOut = NULL; nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, "", &stdOut); if (stdOut != NULL) { From 8e08fb057334321e1f1f2f574fe6b100a50aee81 Mon Sep 17 00:00:00 2001 From: actondev Date: Thu, 29 May 2025 22:45:20 +0200 Subject: [PATCH 2/2] zenity: fix memory issues upon cancel If the user cancels the operation (ie hitting escape), stdOut is not null, but en empty string --- src/nfd_zenity.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/nfd_zenity.c b/src/nfd_zenity.c index ea26a64..a4fc433 100644 --- a/src/nfd_zenity.c +++ b/src/nfd_zenity.c @@ -186,17 +186,18 @@ NFD_OpenDialog(const char* filterList, const nfdchar_t* defaultPath, nfdchar_t** char* stdOut = NULL; nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, filterList, &stdOut); - - if (stdOut != NULL) { - size_t len = strlen(stdOut); + size_t len = (stdOut != NULL) ? strlen(stdOut) : 0; + if (len > 0) { *outPath = NFDi_Malloc(len); memcpy(*outPath, stdOut, len); (*outPath)[len - 1] = '\0'; // trim out the final \n with a null terminator - free(stdOut); } else { *outPath = NULL; } + if (stdOut != NULL) + free(stdOut); + return result; } @@ -219,18 +220,19 @@ NFD_OpenDialogMultiple(const nfdchar_t* filterList, nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, filterList, &stdOut); - if (stdOut != NULL) { - size_t len = strlen(stdOut); + size_t len = (stdOut != NULL) ? strlen(stdOut) : 0; + if (len > 0) { stdOut[len - 1] = '\0'; // remove trailing newline if (AllocPathSet(stdOut, outPaths) == NFD_ERROR) result = NFD_ERROR; - - free(stdOut); } else { result = NFD_ERROR; } + if (stdOut != NULL) + free(stdOut); + return result; } @@ -249,17 +251,18 @@ NFD_SaveDialog(const nfdchar_t* filterList, const nfdchar_t* defaultPath, nfdcha char* stdOut = NULL; nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, filterList, &stdOut); - - if (stdOut != NULL) { - size_t len = strlen(stdOut); + size_t len = (stdOut != NULL) ? strlen(stdOut) : 0; + if (len > 0) { *outPath = NFDi_Malloc(len); memcpy(*outPath, stdOut, len); (*outPath)[len - 1] = '\0'; // trim out the final \n with a null terminator - free(stdOut); } else { *outPath = NULL; } + if (stdOut != NULL) + free(stdOut); + return result; } @@ -278,15 +281,17 @@ NFD_PickFolder(const nfdchar_t* defaultPath, nfdchar_t** outPath) char* stdOut = NULL; nfdresult_t result = ZenityCommon(command, commandLen, defaultPath, "", &stdOut); - if (stdOut != NULL) { - size_t len = strlen(stdOut); + size_t len = (stdOut != NULL) ? strlen(stdOut) : 0; + if (len > 0) { *outPath = NFDi_Malloc(len); memcpy(*outPath, stdOut, len); (*outPath)[len - 1] = '\0'; // trim out the final \n with a null terminator - free(stdOut); } else { *outPath = NULL; } + if (stdOut != NULL) + free(stdOut); + return result; }