Commit b54d2dde authored by Hisham's avatar Hisham
Browse files

Check for failure in allocations.

parent a1f7f286
...@@ -46,7 +46,7 @@ typedef struct ScreenManager_ { ...@@ -46,7 +46,7 @@ typedef struct ScreenManager_ {
ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, const Settings* settings, bool owner) { ScreenManager* ScreenManager_new(int x1, int y1, int x2, int y2, Orientation orientation, const Header* header, const Settings* settings, bool owner) {
ScreenManager* this; ScreenManager* this;
this = malloc(sizeof(ScreenManager)); this = xMalloc(sizeof(ScreenManager));
this->x1 = x1; this->x1 = x1;
this->y1 = y1; this->y1 = y1;
this->x2 = x2; this->x2 = x2;
......
...@@ -96,7 +96,7 @@ static void Settings_readMeterModes(Settings* this, char* line, int column) { ...@@ -96,7 +96,7 @@ static void Settings_readMeterModes(Settings* this, char* line, int column) {
len++; len++;
} }
this->columns[column].len = len; this->columns[column].len = len;
int* modes = calloc(len, sizeof(int)); int* modes = xCalloc(len, sizeof(int));
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
modes[i] = atoi(ids[i]); modes[i] = atoi(ids[i]);
} }
...@@ -110,27 +110,27 @@ static void Settings_defaultMeters(Settings* this) { ...@@ -110,27 +110,27 @@ static void Settings_defaultMeters(Settings* this) {
sizes[1]++; sizes[1]++;
} }
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
this->columns[i].names = calloc(sizes[i] + 1, sizeof(char*)); this->columns[i].names = xCalloc(sizes[i] + 1, sizeof(char*));
this->columns[i].modes = calloc(sizes[i], sizeof(int)); this->columns[i].modes = xCalloc(sizes[i], sizeof(int));
this->columns[i].len = sizes[i]; this->columns[i].len = sizes[i];
} }
int r = 0; int r = 0;
if (this->cpuCount > 8) { if (this->cpuCount > 8) {
this->columns[0].names[0] = strdup("LeftCPUs2"); this->columns[0].names[0] = xStrdup("LeftCPUs2");
this->columns[1].names[r++] = strdup("RightCPUs2"); this->columns[1].names[r++] = xStrdup("RightCPUs2");
} else if (this->cpuCount > 4) { } else if (this->cpuCount > 4) {
this->columns[0].names[0] = strdup("LeftCPUs"); this->columns[0].names[0] = xStrdup("LeftCPUs");
this->columns[1].names[r++] = strdup("RightCPUs"); this->columns[1].names[r++] = xStrdup("RightCPUs");
} else { } else {
this->columns[0].names[0] = strdup("AllCPUs"); this->columns[0].names[0] = xStrdup("AllCPUs");
} }
this->columns[0].names[1] = strdup("Memory"); this->columns[0].names[1] = xStrdup("Memory");
this->columns[0].names[2] = strdup("Swap"); this->columns[0].names[2] = xStrdup("Swap");
this->columns[1].names[r++] = strdup("Tasks"); this->columns[1].names[r++] = xStrdup("Tasks");
this->columns[1].names[r++] = strdup("LoadAverage"); this->columns[1].names[r++] = xStrdup("LoadAverage");
this->columns[1].names[r++] = strdup("Uptime"); this->columns[1].names[r++] = xStrdup("Uptime");
} }
static void readFields(ProcessField* fields, int* flags, const char* line) { static void readFields(ProcessField* fields, int* flags, const char* line) {
...@@ -306,7 +306,7 @@ bool Settings_write(Settings* this) { ...@@ -306,7 +306,7 @@ bool Settings_write(Settings* this) {
Settings* Settings_new(int cpuCount) { Settings* Settings_new(int cpuCount) {
Settings* this = calloc(1, sizeof(Settings)); Settings* this = xCalloc(1, sizeof(Settings));
this->sortKey = PERCENT_CPU; this->sortKey = PERCENT_CPU;
this->direction = 1; this->direction = 1;
...@@ -324,7 +324,7 @@ Settings* Settings_new(int cpuCount) { ...@@ -324,7 +324,7 @@ Settings* Settings_new(int cpuCount) {
this->cpuCount = cpuCount; this->cpuCount = cpuCount;
this->showProgramPath = true; this->showProgramPath = true;
this->fields = calloc(Platform_numberOfFields+1, sizeof(ProcessField)); this->fields = xCalloc(Platform_numberOfFields+1, sizeof(ProcessField));
// TODO: turn 'fields' into a Vector, // TODO: turn 'fields' into a Vector,
// (and ProcessFields into proper objects). // (and ProcessFields into proper objects).
this->flags = 0; this->flags = 0;
...@@ -337,7 +337,7 @@ Settings* Settings_new(int cpuCount) { ...@@ -337,7 +337,7 @@ Settings* Settings_new(int cpuCount) {
char* legacyDotfile = NULL; char* legacyDotfile = NULL;
char* rcfile = getenv("HTOPRC"); char* rcfile = getenv("HTOPRC");
if (rcfile) { if (rcfile) {
this->filename = strdup(rcfile); this->filename = xStrdup(rcfile);
} else { } else {
const char* home = getenv("HOME"); const char* home = getenv("HOME");
if (!home) home = ""; if (!home) home = "";
...@@ -346,7 +346,7 @@ Settings* Settings_new(int cpuCount) { ...@@ -346,7 +346,7 @@ Settings* Settings_new(int cpuCount) {
char* htopDir = NULL; char* htopDir = NULL;
if (xdgConfigHome) { if (xdgConfigHome) {
this->filename = String_cat(xdgConfigHome, "/htop/htoprc"); this->filename = String_cat(xdgConfigHome, "/htop/htoprc");
configDir = strdup(xdgConfigHome); configDir = xStrdup(xdgConfigHome);
htopDir = String_cat(xdgConfigHome, "/htop"); htopDir = String_cat(xdgConfigHome, "/htop");
} else { } else {
this->filename = String_cat(home, "/.config/htop/htoprc"); this->filename = String_cat(home, "/.config/htop/htoprc");
......
...@@ -6,6 +6,7 @@ in the source distribution for its full text. ...@@ -6,6 +6,7 @@ in the source distribution for its full text.
*/ */
#include "StringUtils.h" #include "StringUtils.h"
#include "XAlloc.h"
#include "config.h" #include "config.h"
...@@ -22,7 +23,7 @@ in the source distribution for its full text. ...@@ -22,7 +23,7 @@ in the source distribution for its full text.
char* String_cat(const char* s1, const char* s2) { char* String_cat(const char* s1, const char* s2) {
int l1 = strlen(s1); int l1 = strlen(s1);
int l2 = strlen(s2); int l2 = strlen(s2);
char* out = malloc(l1 + l2 + 1); char* out = xMalloc(l1 + l2 + 1);
strncpy(out, s1, l1); strncpy(out, s1, l1);
strncpy(out+l1, s2, l2+1); strncpy(out+l1, s2, l2+1);
return out; return out;
...@@ -36,7 +37,7 @@ char* String_trim(const char* in) { ...@@ -36,7 +37,7 @@ char* String_trim(const char* in) {
while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) { while (len > 0 && (in[len-1] == ' ' || in[len-1] == '\t' || in[len-1] == '\n')) {
len--; len--;
} }
char* out = malloc(len+1); char* out = xMalloc(len+1);
strncpy(out, in, len); strncpy(out, in, len);
out[len] = '\0'; out[len] = '\0';
return out; return out;
...@@ -55,20 +56,20 @@ inline int String_eq(const char* s1, const char* s2) { ...@@ -55,20 +56,20 @@ inline int String_eq(const char* s1, const char* s2) {
char** String_split(const char* s, char sep, int* n) { char** String_split(const char* s, char sep, int* n) {
*n = 0; *n = 0;
const int rate = 10; const int rate = 10;
char** out = calloc(rate, sizeof(char*)); char** out = xCalloc(rate, sizeof(char*));
int ctr = 0; int ctr = 0;
int blocks = rate; int blocks = rate;
char* where; char* where;
while ((where = strchr(s, sep)) != NULL) { while ((where = strchr(s, sep)) != NULL) {
int size = where - s; int size = where - s;
char* token = malloc(size + 1); char* token = xMalloc(size + 1);
strncpy(token, s, size); strncpy(token, s, size);
token[size] = '\0'; token[size] = '\0';
out[ctr] = token; out[ctr] = token;
ctr++; ctr++;
if (ctr == blocks) { if (ctr == blocks) {
blocks += rate; blocks += rate;
char** newOut = (char**) realloc(out, sizeof(char*) * blocks); char** newOut = (char**) xRealloc(out, sizeof(char*) * blocks);
if (newOut) { if (newOut) {
out = newOut; out = newOut;
} else { } else {
...@@ -80,12 +81,12 @@ char** String_split(const char* s, char sep, int* n) { ...@@ -80,12 +81,12 @@ char** String_split(const char* s, char sep, int* n) {
} }
if (s[0] != '\0') { if (s[0] != '\0') {
int size = strlen(s); int size = strlen(s);
char* token = malloc(size + 1); char* token = xMalloc(size + 1);
strncpy(token, s, size + 1); strncpy(token, s, size + 1);
out[ctr] = token; out[ctr] = token;
ctr++; ctr++;
} }
char** newOut = realloc(out, sizeof(char*) * (ctr + 1)); char** newOut = xRealloc(out, sizeof(char*) * (ctr + 1));
if (newOut) { if (newOut) {
out = newOut; out = newOut;
} }
...@@ -125,5 +126,5 @@ char* String_getToken(const char* line, const unsigned short int numMatch) { ...@@ -125,5 +126,5 @@ char* String_getToken(const char* line, const unsigned short int numMatch) {
} }
match[foundCount] = '\0'; match[foundCount] = '\0';
return((char*)strdup(match)); return((char*)xStrdup(match));
} }
...@@ -59,7 +59,7 @@ InfoScreenClass TraceScreen_class = { ...@@ -59,7 +59,7 @@ InfoScreenClass TraceScreen_class = {
}; };
TraceScreen* TraceScreen_new(Process* process) { TraceScreen* TraceScreen_new(Process* process) {
TraceScreen* this = malloc(sizeof(TraceScreen)); TraceScreen* this = xMalloc(sizeof(TraceScreen));
Object_setClass(this, Class(TraceScreen)); Object_setClass(this, Class(TraceScreen));
this->tracing = true; this->tracing = true;
this->contLine = false; this->contLine = false;
......
...@@ -6,6 +6,7 @@ in the source distribution for its full text. ...@@ -6,6 +6,7 @@ in the source distribution for its full text.
*/ */
#include "UsersTable.h" #include "UsersTable.h"
#include "XAlloc.h"
#include "config.h" #include "config.h"
...@@ -27,7 +28,7 @@ typedef struct UsersTable_ { ...@@ -27,7 +28,7 @@ typedef struct UsersTable_ {
UsersTable* UsersTable_new() { UsersTable* UsersTable_new() {
UsersTable* this; UsersTable* this;
this = malloc(sizeof(UsersTable)); this = xMalloc(sizeof(UsersTable));
this->users = Hashtable_new(20, true); this->users = Hashtable_new(20, true);
return this; return this;
} }
...@@ -42,7 +43,7 @@ char* UsersTable_getRef(UsersTable* this, unsigned int uid) { ...@@ -42,7 +43,7 @@ char* UsersTable_getRef(UsersTable* this, unsigned int uid) {
if (name == NULL) { if (name == NULL) {
struct passwd* userData = getpwuid(uid); struct passwd* userData = getpwuid(uid);
if (userData != NULL) { if (userData != NULL) {
name = strdup(userData->pw_name); name = xStrdup(userData->pw_name);
Hashtable_put(this->users, uid, name); Hashtable_put(this->users, uid, name);
} }
} }
......
...@@ -37,9 +37,9 @@ Vector* Vector_new(ObjectClass* type, bool owner, int size) { ...@@ -37,9 +37,9 @@ Vector* Vector_new(ObjectClass* type, bool owner, int size) {
if (size == DEFAULT_SIZE) if (size == DEFAULT_SIZE)
size = 10; size = 10;
this = malloc(sizeof(Vector)); this = xMalloc(sizeof(Vector));
this->growthRate = size; this->growthRate = size;
this->array = (Object**) calloc(size, sizeof(Object*)); this->array = (Object**) xCalloc(size, sizeof(Object*));
this->arraySize = size; this->arraySize = size;
this->items = 0; this->items = 0;
this->type = type; this->type = type;
...@@ -179,7 +179,7 @@ static void Vector_checkArraySize(Vector* this) { ...@@ -179,7 +179,7 @@ static void Vector_checkArraySize(Vector* this) {
//int i; //int i;
//i = this->arraySize; //i = this->arraySize;
this->arraySize = this->items + this->growthRate; this->arraySize = this->items + this->growthRate;
this->array = (Object**) realloc(this->array, sizeof(Object*) * this->arraySize); this->array = (Object**) xRealloc(this->array, sizeof(Object*) * this->arraySize);
//for (; i < this->arraySize; i++) //for (; i < this->arraySize; i++)
// this->array[i] = NULL; // this->array[i] = NULL;
} }
......
...@@ -19,7 +19,7 @@ void* xMalloc(size_t size) { ...@@ -19,7 +19,7 @@ void* xMalloc(size_t size) {
if (!data && size > 0) { if (!data && size > 0) {
curs_set(1); curs_set(1);
endwin(); endwin();
write(2, oomMessage, sizeof oomMessage); write(2, oomMessage, sizeof oomMessage - 1);
} }
return data; return data;
} }
...@@ -29,7 +29,7 @@ void* xCalloc(size_t nmemb, size_t size) { ...@@ -29,7 +29,7 @@ void* xCalloc(size_t nmemb, size_t size) {
if (!data && nmemb > 0 && size > 0) { if (!data && nmemb > 0 && size > 0) {
curs_set(1); curs_set(1);
endwin(); endwin();
write(2, oomMessage, sizeof oomMessage); write(2, oomMessage, sizeof oomMessage - 1);
} }
return data; return data;
} }
...@@ -39,7 +39,7 @@ void* xRealloc(void* ptr, size_t size) { ...@@ -39,7 +39,7 @@ void* xRealloc(void* ptr, size_t size) {
if (!data && size > 0) { if (!data && size > 0) {
curs_set(1); curs_set(1);
endwin(); endwin();
write(2, oomMessage, sizeof oomMessage); write(2, oomMessage, sizeof oomMessage - 1);
} }
return data; return data;
} }
...@@ -49,7 +49,7 @@ char* xStrdup(const char* str) { ...@@ -49,7 +49,7 @@ char* xStrdup(const char* str) {
if (!data && str) { if (!data && str) {
curs_set(1); curs_set(1);
endwin(); endwin();
write(2, oomMessage, sizeof oomMessage); write(2, oomMessage, sizeof oomMessage - 1);
} }
return data; return data;
} }
...@@ -39,7 +39,7 @@ ProcessClass DarwinProcess_class = { ...@@ -39,7 +39,7 @@ ProcessClass DarwinProcess_class = {
}; };
DarwinProcess* DarwinProcess_new(Settings* settings) { DarwinProcess* DarwinProcess_new(Settings* settings) {
DarwinProcess* this = calloc(1, sizeof(DarwinProcess)); DarwinProcess* this = xCalloc(1, sizeof(DarwinProcess));
Object_setClass(this, Class(DarwinProcess)); Object_setClass(this, Class(DarwinProcess));
Process_init(&this->super, settings); Process_init(&this->super, settings);
...@@ -85,7 +85,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) { ...@@ -85,7 +85,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
} }
/* Allocate space for the arguments. */ /* Allocate space for the arguments. */
procargs = ( char * ) malloc( argmax ); procargs = ( char * ) xMalloc( argmax );
if ( procargs == NULL ) { if ( procargs == NULL ) {
goto ERROR_A; goto ERROR_A;
} }
...@@ -237,7 +237,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) { ...@@ -237,7 +237,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
} }
/* Make a copy of the string. */ /* Make a copy of the string. */
retval = strdup(sp); retval = xStrdup(sp);
/* Clean up. */ /* Clean up. */
free( procargs ); free( procargs );
...@@ -247,7 +247,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) { ...@@ -247,7 +247,7 @@ char *DarwinProcess_getCmdLine(struct kinfo_proc* k, int show_args ) {
ERROR_B: ERROR_B:
free( procargs ); free( procargs );
ERROR_A: ERROR_A:
retval = strdup(k->kp_proc.p_comm); retval = xStrdup(k->kp_proc.p_comm);
return retval; return retval;
} }
......
...@@ -87,7 +87,7 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) { ...@@ -87,7 +87,7 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
if (sysctl(mib, 4, NULL, count, NULL, 0) < 0) if (sysctl(mib, 4, NULL, count, NULL, 0) < 0)
CRT_fatalError("Unable to get size of kproc_infos"); CRT_fatalError("Unable to get size of kproc_infos");
processes = malloc(*count); processes = xMalloc(*count);
if (processes == NULL) if (processes == NULL)
CRT_fatalError("Out of memory for kproc_infos"); CRT_fatalError("Out of memory for kproc_infos");
...@@ -101,7 +101,7 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) { ...@@ -101,7 +101,7 @@ struct kinfo_proc *ProcessList_getKInfoProcs(size_t *count) {
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
DarwinProcessList* this = calloc(1, sizeof(DarwinProcessList)); DarwinProcessList* this = xCalloc(1, sizeof(DarwinProcessList));
ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId); ProcessList_init(&this->super, Class(Process), usersTable, pidWhiteList, userId);
......
...@@ -251,7 +251,7 @@ char* Platform_getProcessEnv(pid_t pid) { ...@@ -251,7 +251,7 @@ char* Platform_getProcessEnv(pid_t pid) {
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_ARGMAX; mib[1] = KERN_ARGMAX;
if (sysctl(mib, 2, &argmax, &bufsz, 0, 0) == 0) { if (sysctl(mib, 2, &argmax, &bufsz, 0, 0) == 0) {
char* buf = malloc(argmax); char* buf = xMalloc(argmax);
if (buf) { if (buf) {
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS2; mib[1] = KERN_PROCARGS2;
...@@ -279,7 +279,7 @@ char* Platform_getProcessEnv(pid_t pid) { ...@@ -279,7 +279,7 @@ char* Platform_getProcessEnv(pid_t pid) {
++p; ++p;
size_t size = endp - p; size_t size = endp - p;
env = malloc(size+2); env = xMalloc(size+2);
if (env) { if (env) {
memcpy(env, p, size); memcpy(env, p, size);
env[size] = 0; env[size] = 0;
......
...@@ -97,7 +97,7 @@ ProcessPidColumn Process_pidColumns[] = { ...@@ -97,7 +97,7 @@ ProcessPidColumn Process_pidColumns[] = {
}; };
FreeBSDProcess* FreeBSDProcess_new(Settings* settings) { FreeBSDProcess* FreeBSDProcess_new(Settings* settings) {
FreeBSDProcess* this = calloc(1, sizeof(FreeBSDProcess)); FreeBSDProcess* this = xCalloc(1, sizeof(FreeBSDProcess));
Object_setClass(this, Class(FreeBSDProcess)); Object_setClass(this, Class(FreeBSDProcess));
Process_init(&this->super, settings); Process_init(&this->super, settings);
return this; return this;
......
...@@ -86,7 +86,7 @@ static int kernelFScale; ...@@ -86,7 +86,7 @@ static int kernelFScale;
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
FreeBSDProcessList* fpl = calloc(1, sizeof(FreeBSDProcessList)); FreeBSDProcessList* fpl = xCalloc(1, sizeof(FreeBSDProcessList));
ProcessList* pl = (ProcessList*) fpl; ProcessList* pl = (ProcessList*) fpl;
ProcessList_init(pl, Class(FreeBSDProcess), usersTable, pidWhiteList, userId); ProcessList_init(pl, Class(FreeBSDProcess), usersTable, pidWhiteList, userId);
...@@ -146,8 +146,8 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui ...@@ -146,8 +146,8 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
size_t sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES; size_t sizeof_cp_time_array = sizeof(unsigned long) * CPUSTATES;
len = 2; sysctlnametomib("kern.cp_time", MIB_kern_cp_time, &len); len = 2; sysctlnametomib("kern.cp_time", MIB_kern_cp_time, &len);
fpl->cp_time_o = calloc(cpus, sizeof_cp_time_array); fpl->cp_time_o = xCalloc(cpus, sizeof_cp_time_array);
fpl->cp_time_n = calloc(cpus, sizeof_cp_time_array); fpl->cp_time_n = xCalloc(cpus, sizeof_cp_time_array);
len = sizeof_cp_time_array; len = sizeof_cp_time_array;
// fetch intial single (or average) CPU clicks from kernel // fetch intial single (or average) CPU clicks from kernel
...@@ -156,8 +156,8 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui ...@@ -156,8 +156,8 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
// on smp box, fetch rest of intial CPU's clicks // on smp box, fetch rest of intial CPU's clicks
if (cpus > 1) { if (cpus > 1) {
len = 2; sysctlnametomib("kern.cp_times", MIB_kern_cp_times, &len); len = 2; sysctlnametomib("kern.cp_times", MIB_kern_cp_times, &len);
fpl->cp_times_o = calloc(cpus, sizeof_cp_time_array); fpl->cp_times_o = xCalloc(cpus, sizeof_cp_time_array);
fpl->cp_times_n = calloc(cpus, sizeof_cp_time_array); fpl->cp_times_n = xCalloc(cpus, sizeof_cp_time_array);
len = cpus * sizeof_cp_time_array; len = cpus * sizeof_cp_time_array;
sysctl(MIB_kern_cp_times, 2, fpl->cp_times_o, &len, NULL, 0); sysctl(MIB_kern_cp_times, 2, fpl->cp_times_o, &len, NULL, 0);
} }
...@@ -165,10 +165,10 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui ...@@ -165,10 +165,10 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
pl->cpuCount = MAX(cpus, 1); pl->cpuCount = MAX(cpus, 1);
if (cpus == 1 ) { if (cpus == 1 ) {
fpl->cpus = realloc(fpl->cpus, sizeof(CPUData)); fpl->cpus = xRealloc(fpl->cpus, sizeof(CPUData));
} else { } else {
// on smp we need CPUs + 1 to store averages too (as kernel kindly provides that as well) // on smp we need CPUs + 1 to store averages too (as kernel kindly provides that as well)
fpl->cpus = realloc(fpl->cpus, (pl->cpuCount + 1) * sizeof(CPUData)); fpl->cpus = xRealloc(fpl->cpus, (pl->cpuCount + 1) * sizeof(CPUData));
} }
...@@ -349,13 +349,13 @@ static inline void FreeBSDProcessList_scanMemoryInfo(ProcessList* pl) { ...@@ -349,13 +349,13 @@ static inline void FreeBSDProcessList_scanMemoryInfo(ProcessList* pl) {
char* FreeBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) { char* FreeBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, int* basenameEnd) {
char** argv = kvm_getargv(kd, kproc, 0); char** argv = kvm_getargv(kd, kproc, 0);
if (!argv) { if (!argv) {
return strdup(kproc->ki_comm); return xStrdup(kproc->ki_comm);
} }
int len = 0; int len = 0;
for (int i = 0; argv[i]; i++) { for (int i = 0; argv[i]; i++) {
len += strlen(argv[i]) + 1; len += strlen(argv[i]) + 1;
} }
char* comm = malloc(len); char* comm = xMalloc(len);
char* at = comm; char* at = comm;
*basenameEnd = 0; *basenameEnd = 0;
for (int i = 0; argv[i]; i++) { for (int i = 0; argv[i]; i++) {
...@@ -398,7 +398,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) { ...@@ -398,7 +398,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) {
snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno)); snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno));
return NULL; return NULL;
} else if (jid == kproc->ki_jid) { } else if (jid == kproc->ki_jid) {
jname = strdup(jnamebuf); jname = xStrdup(jnamebuf);
if (jname == NULL) if (jname == NULL)
strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN);
return jname; return jname;
...@@ -408,7 +408,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) { ...@@ -408,7 +408,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) {
} else { } else {
jnamebuf[0]='-'; jnamebuf[0]='-';
jnamebuf[1]='\0'; jnamebuf[1]='\0';
jname = strdup(jnamebuf); jname = xStrdup(jnamebuf);
} }
return jname; return jname;
} }
......
...@@ -128,7 +128,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) { ...@@ -128,7 +128,7 @@ static CommandLineSettings parseArguments(int argc, char** argv) {
flags.useColors = false; flags.useColors = false;
break; break;
case 'p': { case 'p': {
char* argCopy = strdup(optarg); char* argCopy = xStrdup(optarg);
char* saveptr; char* saveptr;
char* pid = strtok_r(argCopy, ",", &saveptr); char* pid = strtok_r(argCopy, ",", &saveptr);
......
...@@ -50,7 +50,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short ...@@ -50,7 +50,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
char* entryName = dirEntry->d_name; char* entryName = dirEntry->d_name;
if (strncmp(entryName, "BAT", 3)) if (strncmp(entryName, "BAT", 3))
continue; continue;
batteries[nBatteries] = strdup(entryName); batteries[nBatteries] = xStrdup(entryName);
nBatteries++; nBatteries++;
} }
closedir(batteryDir); closedir(batteryDir);
......
...@@ -242,7 +242,7 @@ ProcessClass LinuxProcess_class = { ...@@ -242,7 +242,7 @@ ProcessClass LinuxProcess_class = {
}; };
LinuxProcess* LinuxProcess_new(Settings* settings) { LinuxProcess* LinuxProcess_new(Settings* settings) {
LinuxProcess* this = calloc(1, sizeof(LinuxProcess)); LinuxProcess* this = xCalloc(1, sizeof(LinuxProcess));
Object_setClass(this, Class(LinuxProcess)); Object_setClass(this, Class(LinuxProcess));
Process_init(&this->super, settings); Process_init(&this->super, settings);
return this; return this;
......
...@@ -89,7 +89,7 @@ typedef struct LinuxProcessList_ { ...@@ -89,7 +89,7 @@ typedef struct LinuxProcessList_ {
#endif #endif
ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) { ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, uid_t userId) {
LinuxProcessList* this = calloc(1, sizeof(LinuxProcessList)); LinuxProcessList* this = xCalloc(1, sizeof(LinuxProcessList));
ProcessList* pl = &(this->super); ProcessList* pl = &(this->super);
ProcessList_init(pl, Class(LinuxProcess), usersTable, pidWhiteList, userId); ProcessList_init(pl, Class(LinuxProcess), usersTable, pidWhiteList, userId);
...@@ -108,7 +108,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui ...@@ -108,7 +108,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
fclose(file); fclose(file);
pl->cpuCount = MAX(cpus - 1, 1); pl->cpuCount = MAX(cpus - 1, 1);
this->cpus = calloc(cpus, sizeof(CPUData)); this->cpus = xCalloc(cpus, sizeof(CPUData));
for (int i = 0; i < cpus; i++) { for (int i = 0; i < cpus; i++) {
this->cpus[i].totalTime = 1; this->cpus[i].totalTime = 1;
...@@ -366,7 +366,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d ...@@ -366,7 +366,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
snprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name); snprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name);
FILE* file = fopen(filename, "r"); FILE* file = fopen(filename, "r");
if (!file) { if (!file) {
process->cgroup = strdup(""); process->cgroup = xStrdup("");
return; return;
} }
char output[PROC_LINE_LENGTH + 1]; char output[PROC_LINE_LENGTH + 1];
...@@ -389,7 +389,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d ...@@ -389,7 +389,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
} }
fclose(file); fclose(file);
free(process->cgroup); free(process->cgroup);
process->cgroup = strdup(output); process->cgroup = xStrdup(output);
} }
#endif #endif
......
...@@ -220,16 +220,16 @@ char* Platform_getProcessEnv(pid_t pid) { ...@@ -220,16 +220,16 @@ char* Platform_getProcessEnv(pid_t pid) {
char *env = NULL; char *env = NULL;
if (fd) { if (fd) {
size_t capacity = 4096, size = 0, bytes; size_t capacity = 4096, size = 0, bytes;
env = malloc(capacity); env = xMalloc(capacity);
while (env && (bytes = fread(env+size, 1, capacity-size, fd)) > 0) { while (env && (bytes = fread(env+size, 1, capacity-size, fd)) > 0) {
size += bytes; size += bytes;
capacity *= 2; capacity *= 2;
env = realloc(env, capacity); env = xRealloc(env, capacity);
} }
fclose(fd); fclose(fd);
if (size < 2 || env[size-1] || env[size-2]) { if (size < 2 || env[size-1] || env[size-2]) {
if (size + 2 < capacity) { if (size + 2 < capacity) {
env = realloc(env, capacity+2); env = xRealloc(env, capacity+2);
} }
env[size] = 0; env[size] = 0;
env[size+1] = 0; env[size+1] = 0;
......
...@@ -186,7 +186,7 @@ ProcessPidColumn Process_pidColumns[] = { ...@@ -186,7 +186,7 @@ ProcessPidColumn Process_pidColumns[] = {
}; };
OpenBSDProcess* OpenBSDProcess_new(Settings* settings) { OpenBSDProcess* OpenBSDProcess_new(Settings* settings) {
OpenBSDProcess* this = calloc(sizeof(OpenBSDProcess), 1); OpenBSDProcess* this = xCalloc(sizeof(OpenBSDProcess), 1);
Object_setClass(this, Class(OpenBSDProcess)); Object_setClass(this, Class(OpenBSDProcess));
Process_init(&this->super, settings); Process_init(&this->super, settings);
return this; return this;
......
...@@ -53,7 +53,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui ...@@ -53,7 +53,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
int mib[] = { CTL_HW, HW_NCPU }; int mib[] = { CTL_HW, HW_NCPU };
int fmib[] = { CTL_KERN, KERN_FSCALE }; int fmib[] = { CTL_KERN, KERN_FSCALE };
int i, e; int i, e;
OpenBSDProcessList* opl = calloc(1, sizeof(OpenBSDProcessList)); OpenBSDProcessList* opl = xCalloc(1, sizeof(OpenBSDProcessList));
ProcessList* pl = (ProcessList*) opl; ProcessList* pl = (ProcessList*) opl;
size_t size = sizeof(pl->cpuCount); size_t size = sizeof(pl->cpuCount);
...@@ -62,7 +62,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui ...@@ -62,7 +62,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
if (e == -1 || pl->cpuCount < 1) { if (e == -1 || pl->cpuCount < 1) {
pl->cpuCount = 1; pl->cpuCount = 1;
} }
opl->cpus = realloc(opl->cpus, pl->cpuCount * sizeof(CPUData)); opl->cpus = xRealloc(opl->cpus, pl->cpuCount * sizeof(CPUData));
size = sizeof(fscale); size = sizeof(fscale);
if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0) if (sysctl(fmib, 2, &fscale, &size, NULL, 0) < 0)
...@@ -144,7 +144,7 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in ...@@ -144,7 +144,7 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
*/ */
arg = kvm_getargv(kd, kproc, 500); arg = kvm_getargv(kd, kproc, 500);
if (arg == NULL) { if (arg == NULL) {
if ((s = strdup(kproc->p_comm)) == NULL) { if ((s = xStrdup(kproc->p_comm)) == NULL) {
err(1, NULL); err(1, NULL);
} }
return s; return s;
...@@ -152,8 +152,8 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in ...@@ -152,8 +152,8 @@ char *OpenBSDProcessList_readProcessName(kvm_t* kd, struct kinfo_proc* kproc, in
for (i = 0; arg[i] != NULL; i++) { for (i = 0; arg[i] != NULL; i++) {
len += strlen(arg[i]) + 1; len += strlen(arg[i]) + 1;
} }
if ((buf = s = malloc(len)) == NULL) { if ((buf = s = xMalloc(len)) == NULL) {
if ((s = strdup(kproc->p_comm)) == NULL) { if ((s = xStrdup(kproc->p_comm)) == NULL) {
err(1, NULL); err(1, NULL);
} }
return s; return s;
......
...@@ -264,7 +264,7 @@ void Platform_setSwapValues(Meter* this) { ...@@ -264,7 +264,7 @@ void Platform_setSwapValues(Meter* this) {
return; return;
} }
swdev = calloc(nswap, sizeof(*swdev)); swdev = xCalloc(nswap, sizeof(*swdev));
if (swdev == NULL) { if (swdev == NULL) {
return; return;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment