diff --git a/Meter.c b/Meter.c index 2fe949ef..ed9571df 100644 --- a/Meter.c +++ b/Meter.c @@ -93,8 +93,7 @@ void Meter_delete(Object* cast) { } void Meter_setCaption(Meter* this, const char* caption) { - free(this->caption); - this->caption = xStrdup(caption); + free_and_xStrdup(&this->caption, caption); } static inline void Meter_displayBuffer(const Meter* this, const char* buffer, RichString* out) { diff --git a/XUtils.c b/XUtils.c index fe2d8b9a..9ad8c224 100644 --- a/XUtils.c +++ b/XUtils.c @@ -239,6 +239,14 @@ char* xStrdup(const char* str) { return data; } +void free_and_xStrdup(char** ptr, const char* str) { + if (*ptr && String_eq(*ptr, str)) + return; + + free(*ptr); + *ptr = xStrdup(str); +} + char* xStrndup(const char* str, size_t len) { char* data = strndup(str, len); if (!data) { diff --git a/XUtils.h b/XUtils.h index 6066aa95..5eb5d63d 100644 --- a/XUtils.h +++ b/XUtils.h @@ -69,6 +69,7 @@ ATTR_FORMAT(printf, 3, 4) int xSnprintf(char* buf, size_t len, const char* fmt, ...); char* xStrdup(const char* str) ATTR_NONNULL; +void free_and_xStrdup(char** ptr, const char* str); char* xStrndup(const char* str, size_t len) ATTR_NONNULL; diff --git a/htop.c b/htop.c index a8935f40..e59c0282 100644 --- a/htop.c +++ b/htop.c @@ -204,9 +204,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) { } case 'F': { assert(optarg); - free(flags.commFilter); - flags.commFilter = xStrdup(optarg); - + free_and_xStrdup(&flags.commFilter, optarg); break; } case 'H': { diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index bfd1cc99..fe6e500d 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -718,10 +718,8 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, openat_arg_t switch(field) { case 1: foundEnvID = true; - if (!String_eq(name_value_sep, process->ctid ? process->ctid : "")) { - free(process->ctid); - process->ctid = xStrdup(name_value_sep); - } + if (!String_eq(name_value_sep, process->ctid ? process->ctid : "")) + free_and_xStrdup(&process->ctid, name_value_sep); break; case 2: foundVPid = true; @@ -779,8 +777,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, openat_arg_t left -= wrote; } fclose(file); - free(process->cgroup); - process->cgroup = xStrdup(output); + free_and_xStrdup(&process->cgroup, output); } #ifdef HAVE_VSERVER @@ -881,8 +878,7 @@ static void LinuxProcessList_readSecattrData(LinuxProcess* process, openat_arg_t if (process->secattr && String_eq(process->secattr, buffer)) { return; } - free(process->secattr); - process->secattr = xStrdup(buffer); + free_and_xStrdup(&process->secattr, buffer); } static void LinuxProcessList_readCwd(LinuxProcess* process, openat_arg_t procFd) { @@ -907,8 +903,7 @@ static void LinuxProcessList_readCwd(LinuxProcess* process, openat_arg_t procFd) if (process->cwd && String_eq(process->cwd, pathBuffer)) return; - free(process->cwd); - process->cwd = xStrdup(pathBuffer); + free_and_xStrdup(&process->cwd, pathBuffer); } #ifdef HAVE_DELAYACCT @@ -1142,8 +1137,7 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc command[amtRead - 1] = '\0'; lp->mergedCommand.maxLen += amtRead - 1; /* accommodate comm */ if (!lp->procComm || !String_eq(command, lp->procComm)) { - free(lp->procComm); - lp->procComm = xStrdup(command); + free_and_xStrdup(&lp->procComm, command); lp->mergedCommand.commChanged = true; } } else if (lp->procComm) { @@ -1166,8 +1160,7 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc filename[amtRead] = 0; lp->mergedCommand.maxLen += amtRead; /* accommodate exe */ if (!lp->procExe || !String_eq(filename, lp->procExe)) { - free(lp->procExe); - lp->procExe = xStrdup(filename); + free_and_xStrdup(&lp->procExe, filename); lp->procExeLen = amtRead; /* exe is guaranteed to contain at least one /, but validate anyway */ while (amtRead && filename[--amtRead] != '/') diff --git a/linux/SystemdMeter.c b/linux/SystemdMeter.c index 61bb59b3..75c666f8 100644 --- a/linux/SystemdMeter.c +++ b/linux/SystemdMeter.c @@ -213,8 +213,7 @@ static void updateViaExec(void) { if (newline) { *newline = '\0'; } - free(systemState); - systemState = xStrdup(lineBuffer + strlen("SystemState=")); + free_and_xStrdup(&systemState, lineBuffer + strlen("SystemState=")); } else if (String_startsWith(lineBuffer, "NFailedUnits=")) { nFailedUnits = strtoul(lineBuffer + strlen("NFailedUnits="), NULL, 10); } else if (String_startsWith(lineBuffer, "NNames=")) {