mirror of
https://github.com/xzeldon/htop.git
synced 2025-07-12 12:14:36 +03:00
Check for failure in allocations.
This commit is contained in:
@ -50,7 +50,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short
|
||||
char* entryName = dirEntry->d_name;
|
||||
if (strncmp(entryName, "BAT", 3))
|
||||
continue;
|
||||
batteries[nBatteries] = strdup(entryName);
|
||||
batteries[nBatteries] = xStrdup(entryName);
|
||||
nBatteries++;
|
||||
}
|
||||
closedir(batteryDir);
|
||||
|
@ -242,7 +242,7 @@ ProcessClass LinuxProcess_class = {
|
||||
};
|
||||
|
||||
LinuxProcess* LinuxProcess_new(Settings* settings) {
|
||||
LinuxProcess* this = calloc(1, sizeof(LinuxProcess));
|
||||
LinuxProcess* this = xCalloc(1, sizeof(LinuxProcess));
|
||||
Object_setClass(this, Class(LinuxProcess));
|
||||
Process_init(&this->super, settings);
|
||||
return this;
|
||||
|
@ -89,7 +89,7 @@ typedef struct LinuxProcessList_ {
|
||||
#endif
|
||||
|
||||
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_init(pl, Class(LinuxProcess), usersTable, pidWhiteList, userId);
|
||||
|
||||
@ -108,7 +108,7 @@ ProcessList* ProcessList_new(UsersTable* usersTable, Hashtable* pidWhiteList, ui
|
||||
fclose(file);
|
||||
|
||||
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++) {
|
||||
this->cpus[i].totalTime = 1;
|
||||
@ -366,7 +366,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
|
||||
snprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name);
|
||||
FILE* file = fopen(filename, "r");
|
||||
if (!file) {
|
||||
process->cgroup = strdup("");
|
||||
process->cgroup = xStrdup("");
|
||||
return;
|
||||
}
|
||||
char output[PROC_LINE_LENGTH + 1];
|
||||
@ -389,7 +389,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d
|
||||
}
|
||||
fclose(file);
|
||||
free(process->cgroup);
|
||||
process->cgroup = strdup(output);
|
||||
process->cgroup = xStrdup(output);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -220,16 +220,16 @@ char* Platform_getProcessEnv(pid_t pid) {
|
||||
char *env = NULL;
|
||||
if (fd) {
|
||||
size_t capacity = 4096, size = 0, bytes;
|
||||
env = malloc(capacity);
|
||||
env = xMalloc(capacity);
|
||||
while (env && (bytes = fread(env+size, 1, capacity-size, fd)) > 0) {
|
||||
size += bytes;
|
||||
capacity *= 2;
|
||||
env = realloc(env, capacity);
|
||||
env = xRealloc(env, capacity);
|
||||
}
|
||||
fclose(fd);
|
||||
if (size < 2 || env[size-1] || env[size-2]) {
|
||||
if (size + 2 < capacity) {
|
||||
env = realloc(env, capacity+2);
|
||||
env = xRealloc(env, capacity+2);
|
||||
}
|
||||
env[size] = 0;
|
||||
env[size+1] = 0;
|
||||
|
Reference in New Issue
Block a user