mirror of https://github.com/xzeldon/htop.git
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) {
|
void Meter_setCaption(Meter* this, const char* caption) {
|
||||||
free(this->caption);
|
free_and_xStrdup(&this->caption, caption);
|
||||||
this->caption = xStrdup(caption);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void Meter_displayBuffer(const Meter* this, const char* buffer, RichString* out) {
|
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;
|
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* xStrndup(const char* str, size_t len) {
|
||||||
char* data = strndup(str, len);
|
char* data = strndup(str, len);
|
||||||
if (!data) {
|
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, ...);
|
int xSnprintf(char* buf, size_t len, const char* fmt, ...);
|
||||||
|
|
||||||
char* xStrdup(const char* str) ATTR_NONNULL;
|
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;
|
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': {
|
case 'F': {
|
||||||
assert(optarg);
|
assert(optarg);
|
||||||
free(flags.commFilter);
|
free_and_xStrdup(&flags.commFilter, optarg);
|
||||||
flags.commFilter = xStrdup(optarg);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 'H': {
|
case 'H': {
|
||||||
|
|
|
@ -718,10 +718,8 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, openat_arg_t
|
||||||
switch(field) {
|
switch(field) {
|
||||||
case 1:
|
case 1:
|
||||||
foundEnvID = true;
|
foundEnvID = true;
|
||||||
if (!String_eq(name_value_sep, process->ctid ? process->ctid : "")) {
|
if (!String_eq(name_value_sep, process->ctid ? process->ctid : ""))
|
||||||
free(process->ctid);
|
free_and_xStrdup(&process->ctid, name_value_sep);
|
||||||
process->ctid = xStrdup(name_value_sep);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
foundVPid = true;
|
foundVPid = true;
|
||||||
|
@ -779,8 +777,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, openat_arg_t
|
||||||
left -= wrote;
|
left -= wrote;
|
||||||
}
|
}
|
||||||
fclose(file);
|
fclose(file);
|
||||||
free(process->cgroup);
|
free_and_xStrdup(&process->cgroup, output);
|
||||||
process->cgroup = xStrdup(output);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_VSERVER
|
#ifdef HAVE_VSERVER
|
||||||
|
@ -881,8 +878,7 @@ static void LinuxProcessList_readSecattrData(LinuxProcess* process, openat_arg_t
|
||||||
if (process->secattr && String_eq(process->secattr, buffer)) {
|
if (process->secattr && String_eq(process->secattr, buffer)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
free(process->secattr);
|
free_and_xStrdup(&process->secattr, buffer);
|
||||||
process->secattr = xStrdup(buffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LinuxProcessList_readCwd(LinuxProcess* process, openat_arg_t procFd) {
|
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))
|
if (process->cwd && String_eq(process->cwd, pathBuffer))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
free(process->cwd);
|
free_and_xStrdup(&process->cwd, pathBuffer);
|
||||||
process->cwd = xStrdup(pathBuffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_DELAYACCT
|
#ifdef HAVE_DELAYACCT
|
||||||
|
@ -1142,8 +1137,7 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc
|
||||||
command[amtRead - 1] = '\0';
|
command[amtRead - 1] = '\0';
|
||||||
lp->mergedCommand.maxLen += amtRead - 1; /* accommodate comm */
|
lp->mergedCommand.maxLen += amtRead - 1; /* accommodate comm */
|
||||||
if (!lp->procComm || !String_eq(command, lp->procComm)) {
|
if (!lp->procComm || !String_eq(command, lp->procComm)) {
|
||||||
free(lp->procComm);
|
free_and_xStrdup(&lp->procComm, command);
|
||||||
lp->procComm = xStrdup(command);
|
|
||||||
lp->mergedCommand.commChanged = true;
|
lp->mergedCommand.commChanged = true;
|
||||||
}
|
}
|
||||||
} else if (lp->procComm) {
|
} else if (lp->procComm) {
|
||||||
|
@ -1166,8 +1160,7 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, openat_arg_t proc
|
||||||
filename[amtRead] = 0;
|
filename[amtRead] = 0;
|
||||||
lp->mergedCommand.maxLen += amtRead; /* accommodate exe */
|
lp->mergedCommand.maxLen += amtRead; /* accommodate exe */
|
||||||
if (!lp->procExe || !String_eq(filename, lp->procExe)) {
|
if (!lp->procExe || !String_eq(filename, lp->procExe)) {
|
||||||
free(lp->procExe);
|
free_and_xStrdup(&lp->procExe, filename);
|
||||||
lp->procExe = xStrdup(filename);
|
|
||||||
lp->procExeLen = amtRead;
|
lp->procExeLen = amtRead;
|
||||||
/* exe is guaranteed to contain at least one /, but validate anyway */
|
/* exe is guaranteed to contain at least one /, but validate anyway */
|
||||||
while (amtRead && filename[--amtRead] != '/')
|
while (amtRead && filename[--amtRead] != '/')
|
||||||
|
|
|
@ -213,8 +213,7 @@ static void updateViaExec(void) {
|
||||||
if (newline) {
|
if (newline) {
|
||||||
*newline = '\0';
|
*newline = '\0';
|
||||||
}
|
}
|
||||||
free(systemState);
|
free_and_xStrdup(&systemState, lineBuffer + strlen("SystemState="));
|
||||||
systemState = xStrdup(lineBuffer + strlen("SystemState="));
|
|
||||||
} else if (String_startsWith(lineBuffer, "NFailedUnits=")) {
|
} else if (String_startsWith(lineBuffer, "NFailedUnits=")) {
|
||||||
nFailedUnits = strtoul(lineBuffer + strlen("NFailedUnits="), NULL, 10);
|
nFailedUnits = strtoul(lineBuffer + strlen("NFailedUnits="), NULL, 10);
|
||||||
} else if (String_startsWith(lineBuffer, "NNames=")) {
|
} else if (String_startsWith(lineBuffer, "NNames=")) {
|
||||||
|
|
Loading…
Reference in New Issue