Check for failure in allocations.

This commit is contained in:
Hisham
2016-02-02 15:53:02 +01:00
parent a1f7f2869e
commit b54d2dde40
42 changed files with 141 additions and 134 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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

View File

@ -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;