mirror of
https://github.com/xzeldon/htop.git
synced 2024-12-23 22:55:46 +00:00
Add wrapper function for free and strdup
Reduces code in callers and helps avoiding memory leaks.
This commit is contained in:
parent
958112c5a3
commit
70f48f1f44
3
Meter.c
3
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) {
|
||||
|
8
XUtils.c
8
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) {
|
||||
|
1
XUtils.h
1
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;
|
||||
|
||||
|
4
htop.c
4
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': {
|
||||
|
@ -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] != '/')
|
||||
|
@ -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=")) {
|
||||
|
Loading…
Reference in New Issue
Block a user