From 09e241fb1271021e3615512debd3136891547562 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 27 Jul 2017 16:07:50 -0300 Subject: Security review: check results of snprintf. Calls marked with xSnprintf shouldn't fail. Abort program cleanly if any of them does. --- AffinityPanel.c | 2 +- AvailableColumnsPanel.c | 2 +- AvailableMetersPanel.c | 2 +- BatteryMeter.c | 8 ++--- CPUMeter.c | 28 ++++++++--------- Header.c | 6 ++-- ListItem.c | 2 +- LoadAverageMeter.c | 12 +++---- Meter.c | 8 ++--- OpenFilesScreen.c | 4 +-- Process.c | 64 +++++++++++++++++++------------------- SignalsPanel.c | 2 +- TasksMeter.c | 10 +++--- TraceScreen.c | 6 ++-- UptimeMeter.c | 12 +++---- XAlloc.c | 4 ++- XAlloc.h | 3 ++ dragonflybsd/DragonFlyBSDProcess.c | 6 ++-- freebsd/FreeBSDProcess.c | 4 +-- freebsd/FreeBSDProcessList.c | 2 +- linux/Battery.c | 8 ++--- linux/IOPriorityPanel.c | 2 +- linux/LinuxProcess.c | 24 +++++++------- linux/LinuxProcessList.c | 22 ++++++------- linux/Platform.c | 2 +- 25 files changed, 125 insertions(+), 120 deletions(-) diff --git a/AffinityPanel.c b/AffinityPanel.c index 03bfa753..d9f1612b 100644 --- a/AffinityPanel.c +++ b/AffinityPanel.c @@ -52,7 +52,7 @@ Panel* AffinityPanel_new(ProcessList* pl, Affinity* affinity) { int curCpu = 0; for (int i = 0; i < pl->cpuCount; i++) { char number[10]; - snprintf(number, 9, "%d", Settings_cpuId(pl->settings, i)); + xSnprintf(number, 9, "%d", Settings_cpuId(pl->settings, i)); bool mode; if (curCpu < affinity->used && affinity->cpus[curCpu] == i) { mode = true; diff --git a/AvailableColumnsPanel.c b/AvailableColumnsPanel.c index 503efabe..1e548dce 100644 --- a/AvailableColumnsPanel.c +++ b/AvailableColumnsPanel.c @@ -81,7 +81,7 @@ AvailableColumnsPanel* AvailableColumnsPanel_new(Panel* columns) { for (int i = 1; i < Platform_numberOfFields; i++) { if (i != COMM && Process_fields[i].description) { char description[256]; - snprintf(description, sizeof(description), "%s - %s", Process_fields[i].name, Process_fields[i].description); + xSnprintf(description, sizeof(description), "%s - %s", Process_fields[i].name, Process_fields[i].description); Panel_add(super, (Object*) ListItem_new(description, i)); } } diff --git a/AvailableMetersPanel.c b/AvailableMetersPanel.c index e68475f4..ddb55367 100644 --- a/AvailableMetersPanel.c +++ b/AvailableMetersPanel.c @@ -127,7 +127,7 @@ AvailableMetersPanel* AvailableMetersPanel_new(Settings* settings, Header* heade Panel_add(super, (Object*) ListItem_new("CPU average", 0)); for (int i = 1; i <= cpus; i++) { char buffer[50]; - snprintf(buffer, 50, "%s %d", type->uiName, i); + xSnprintf(buffer, 50, "%s %d", type->uiName, i); Panel_add(super, (Object*) ListItem_new(buffer, i)); } } else { diff --git a/BatteryMeter.c b/BatteryMeter.c index 0031050f..214248e2 100644 --- a/BatteryMeter.c +++ b/BatteryMeter.c @@ -40,7 +40,7 @@ static void BatteryMeter_updateValues(Meter * this, char *buffer, int len) { if (percent == -1) { this->values[0] = 0; - snprintf(buffer, len, "n/a"); + xSnprintf(buffer, len, "n/a"); return; } @@ -58,11 +58,11 @@ static void BatteryMeter_updateValues(Meter * this, char *buffer, int len) { } if (isOnAC == AC_PRESENT) { - snprintf(buffer, len, onAcText, percent); + xSnprintf(buffer, len, onAcText, percent); } else if (isOnAC == AC_ABSENT) { - snprintf(buffer, len, onBatteryText, percent); + xSnprintf(buffer, len, onBatteryText, percent); } else { - snprintf(buffer, len, unknownText, percent); + xSnprintf(buffer, len, unknownText, percent); } return; diff --git a/CPUMeter.c b/CPUMeter.c index 6b9b83db..de5490df 100644 --- a/CPUMeter.c +++ b/CPUMeter.c @@ -48,7 +48,7 @@ static void CPUMeter_init(Meter* this) { int cpu = this->param; if (this->pl->cpuCount > 1) { char caption[10]; - snprintf(caption, sizeof(caption), "%-3d", Settings_cpuId(this->pl->settings, cpu - 1)); + xSnprintf(caption, sizeof(caption), "%-3d", Settings_cpuId(this->pl->settings, cpu - 1)); Meter_setCaption(this, caption); } if (this->param == 0) @@ -58,12 +58,12 @@ static void CPUMeter_init(Meter* this) { static void CPUMeter_updateValues(Meter* this, char* buffer, int size) { int cpu = this->param; if (cpu > this->pl->cpuCount) { - snprintf(buffer, size, "absent"); + xSnprintf(buffer, size, "absent"); return; } memset(this->values, 0, sizeof(double) * CPU_METER_ITEMCOUNT); double percent = Platform_setCPUValues(this, cpu); - snprintf(buffer, size, "%5.1f%%", percent); + xSnprintf(buffer, size, "%5.1f%%", percent); } static void CPUMeter_display(Object* cast, RichString* out) { @@ -74,44 +74,44 @@ static void CPUMeter_display(Object* cast, RichString* out) { RichString_append(out, CRT_colors[METER_TEXT], "absent"); return; } - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_NORMAL]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_NORMAL]); RichString_append(out, CRT_colors[METER_TEXT], ":"); RichString_append(out, CRT_colors[CPU_NORMAL], buffer); if (this->pl->settings->detailedCPUTime) { - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_KERNEL]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_KERNEL]); RichString_append(out, CRT_colors[METER_TEXT], "sy:"); RichString_append(out, CRT_colors[CPU_KERNEL], buffer); - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_NICE]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_NICE]); RichString_append(out, CRT_colors[METER_TEXT], "ni:"); RichString_append(out, CRT_colors[CPU_NICE_TEXT], buffer); - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_IRQ]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_IRQ]); RichString_append(out, CRT_colors[METER_TEXT], "hi:"); RichString_append(out, CRT_colors[CPU_IRQ], buffer); - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_SOFTIRQ]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_SOFTIRQ]); RichString_append(out, CRT_colors[METER_TEXT], "si:"); RichString_append(out, CRT_colors[CPU_SOFTIRQ], buffer); if (this->values[CPU_METER_STEAL]) { - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_STEAL]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_STEAL]); RichString_append(out, CRT_colors[METER_TEXT], "st:"); RichString_append(out, CRT_colors[CPU_STEAL], buffer); } if (this->values[CPU_METER_GUEST]) { - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_GUEST]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_GUEST]); RichString_append(out, CRT_colors[METER_TEXT], "gu:"); RichString_append(out, CRT_colors[CPU_GUEST], buffer); } - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_IOWAIT]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_IOWAIT]); RichString_append(out, CRT_colors[METER_TEXT], "wa:"); RichString_append(out, CRT_colors[CPU_IOWAIT], buffer); } else { - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_KERNEL]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_KERNEL]); RichString_append(out, CRT_colors[METER_TEXT], "sys:"); RichString_append(out, CRT_colors[CPU_KERNEL], buffer); - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_NICE]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_NICE]); RichString_append(out, CRT_colors[METER_TEXT], "low:"); RichString_append(out, CRT_colors[CPU_NICE_TEXT], buffer); if (this->values[CPU_METER_IRQ]) { - snprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_IRQ]); + xSnprintf(buffer, sizeof(buffer), "%5.1f%% ", this->values[CPU_METER_IRQ]); RichString_append(out, CRT_colors[METER_TEXT], "vir:"); RichString_append(out, CRT_colors[CPU_GUEST], buffer); } diff --git a/Header.c b/Header.c index 07631058..e048ee55 100644 --- a/Header.c +++ b/Header.c @@ -91,9 +91,9 @@ void Header_writeBackToSettings(const Header* this) { Meter* meter = (Meter*) Vector_get(vec, i); char* name = xCalloc(64, sizeof(char)); if (meter->param) { - snprintf(name, 63, "%s(%d)", As_Meter(meter)->name, meter->param); + xSnprintf(name, 63, "%s(%d)", As_Meter(meter)->name, meter->param); } else { - snprintf(name, 63, "%s", As_Meter(meter)->name); + xSnprintf(name, 63, "%s", As_Meter(meter)->name); } colSettings->names[i] = name; colSettings->modes[i] = meter->mode; @@ -155,7 +155,7 @@ char* Header_readMeterName(Header* this, int i, int column) { strncpy(name, Meter_name(meter), nameLen); name[nameLen] = '\0'; if (meter->param) - snprintf(name + nameLen, len - nameLen, "(%d)", meter->param); + xSnprintf(name + nameLen, len - nameLen, "(%d)", meter->param); return name; } diff --git a/ListItem.c b/ListItem.c index c9e906fa..05c5c0b3 100644 --- a/ListItem.c +++ b/ListItem.c @@ -39,7 +39,7 @@ static void ListItem_display(Object* cast, RichString* out) { /* int len = strlen(this->value)+1; char buffer[len+1]; - snprintf(buffer, len, "%s", this->value); + xSnprintf(buffer, len, "%s", this->value); */ if (this->moving) { RichString_write(out, CRT_colors[DEFAULT_COLOR], diff --git a/LoadAverageMeter.c b/LoadAverageMeter.c index 517031dd..e29433f1 100644 --- a/LoadAverageMeter.c +++ b/LoadAverageMeter.c @@ -22,17 +22,17 @@ int LoadMeter_attributes[] = { LOAD }; static void LoadAverageMeter_updateValues(Meter* this, char* buffer, int size) { Platform_getLoadAverage(&this->values[0], &this->values[1], &this->values[2]); - snprintf(buffer, size, "%.2f/%.2f/%.2f", this->values[0], this->values[1], this->values[2]); + xSnprintf(buffer, size, "%.2f/%.2f/%.2f", this->values[0], this->values[1], this->values[2]); } static void LoadAverageMeter_display(Object* cast, RichString* out) { Meter* this = (Meter*)cast; char buffer[20]; - snprintf(buffer, sizeof(buffer), "%.2f ", this->values[0]); + xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[0]); RichString_write(out, CRT_colors[LOAD_AVERAGE_ONE], buffer); - snprintf(buffer, sizeof(buffer), "%.2f ", this->values[1]); + xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[1]); RichString_append(out, CRT_colors[LOAD_AVERAGE_FIVE], buffer); - snprintf(buffer, sizeof(buffer), "%.2f ", this->values[2]); + xSnprintf(buffer, sizeof(buffer), "%.2f ", this->values[2]); RichString_append(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer); } @@ -42,13 +42,13 @@ static void LoadMeter_updateValues(Meter* this, char* buffer, int size) { if (this->values[0] > this->total) { this->total = this->values[0]; } - snprintf(buffer, size, "%.2f", this->values[0]); + xSnprintf(buffer, size, "%.2f", this->values[0]); } static void LoadMeter_display(Object* cast, RichString* out) { Meter* this = (Meter*)cast; char buffer[20]; - snprintf(buffer, sizeof(buffer), "%.2f ", ((Meter*)this)->values[0]); + xSnprintf(buffer, sizeof(buffer), "%.2f ", ((Meter*)this)->values[0]); RichString_write(out, CRT_colors[LOAD], buffer); } diff --git a/Meter.c b/Meter.c index 8a18d86c..56a6025e 100644 --- a/Meter.c +++ b/Meter.c @@ -225,16 +225,16 @@ void Meter_setMode(Meter* this, int modeIndex) { ListItem* Meter_toListItem(Meter* this, bool moving) { char mode[21]; if (this->mode) - snprintf(mode, 20, " [%s]", Meter_modes[this->mode]->uiName); + xSnprintf(mode, 20, " [%s]", Meter_modes[this->mode]->uiName); else mode[0] = '\0'; char number[11]; if (this->param > 0) - snprintf(number, 10, " %d", this->param); + xSnprintf(number, 10, " %d", this->param); else number[0] = '\0'; char buffer[51]; - snprintf(buffer, 50, "%s%s%s", Meter_uiName(this), number, mode); + xSnprintf(buffer, 50, "%s%s%s", Meter_uiName(this), number, mode); ListItem* li = ListItem_new(buffer, 0); li->moving = moving; return li; @@ -287,7 +287,7 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { int blockSizes[10]; - snprintf(bar, w + 1, "%*s", w, buffer); + xSnprintf(bar, w + 1, "%*s", w, buffer); // First draw in the bar[] buffer... int offset = 0; diff --git a/OpenFilesScreen.c b/OpenFilesScreen.c index 75190fe1..a772bbac 100644 --- a/OpenFilesScreen.c +++ b/OpenFilesScreen.c @@ -77,7 +77,7 @@ void OpenFilesScreen_draw(InfoScreen* this) { static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) { char command[1025]; - snprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid); + xSnprintf(command, 1024, "lsof -P -p %d -F 2> /dev/null", pid); FILE* fd = popen(command, "r"); OpenFiles_ProcessData* pdata = xCalloc(1, sizeof(OpenFiles_ProcessData)); OpenFiles_FileData* fdata = NULL; @@ -131,7 +131,7 @@ void OpenFilesScreen_scan(InfoScreen* this) { int lenN = data['n'] ? strlen(data['n']) : 0; int sizeEntry = 5 + 7 + 10 + 10 + 10 + lenN + 5 /*spaces*/ + 1 /*null*/; char* entry = xMalloc(sizeEntry); - snprintf(entry, sizeEntry, "%5.5s %7.7s %10.10s %10.10s %10.10s %s", + xSnprintf(entry, sizeEntry, "%5.5s %7.7s %10.10s %10.10s %10.10s %s", data['f'] ? data['f'] : "", data['t'] ? data['t'] : "", data['D'] ? data['D'] : "", diff --git a/Process.c b/Process.c index b02761c3..394ca25d 100644 --- a/Process.c +++ b/Process.c @@ -195,10 +195,10 @@ void Process_setupColumnWidths() { assert(digits < 20); for (int i = 0; Process_pidColumns[i].label; i++) { assert(i < 20); - snprintf(Process_titleBuffer[i], 20, "%*s ", digits, Process_pidColumns[i].label); + xSnprintf(Process_titleBuffer[i], 20, "%*s ", digits, Process_pidColumns[i].label); Process_fields[Process_pidColumns[i].id].title = Process_titleBuffer[i]; } - snprintf(Process_pidFormat, sizeof(Process_pidFormat), "%%%dd ", digits); + xSnprintf(Process_pidFormat, sizeof(Process_pidFormat), "%%%dd ", digits); } void Process_humanNumber(RichString* str, unsigned long number, bool coloring) { @@ -266,12 +266,12 @@ void Process_colorNumber(RichString* str, unsigned long long number, bool colori int len = snprintf(buffer, 13, " no perm "); RichString_appendn(str, CRT_colors[PROCESS_SHADOW], buffer, len); } else if (number > 10000000000) { - snprintf(buffer, 13, "%11lld ", number / 1000); + xSnprintf(buffer, 13, "%11lld ", number / 1000); RichString_appendn(str, largeNumberColor, buffer, 5); RichString_appendn(str, processMegabytesColor, buffer+5, 3); RichString_appendn(str, processColor, buffer+8, 4); } else { - snprintf(buffer, 13, "%11llu ", number); + xSnprintf(buffer, 13, "%11llu ", number); RichString_appendn(str, largeNumberColor, buffer, 2); RichString_appendn(str, processMegabytesColor, buffer+2, 3); RichString_appendn(str, processColor, buffer+5, 3); @@ -288,15 +288,15 @@ void Process_printTime(RichString* str, unsigned long long totalHundredths) { int hundredths = totalHundredths - (totalSeconds * 100); char buffer[11]; if (hours >= 100) { - snprintf(buffer, 10, "%7lluh ", hours); + xSnprintf(buffer, 10, "%7lluh ", hours); RichString_append(str, CRT_colors[LARGE_NUMBER], buffer); } else { if (hours) { - snprintf(buffer, 10, "%2lluh", hours); + xSnprintf(buffer, 10, "%2lluh", hours); RichString_append(str, CRT_colors[LARGE_NUMBER], buffer); - snprintf(buffer, 10, "%02d:%02d ", minutes, seconds); + xSnprintf(buffer, 10, "%02d:%02d ", minutes, seconds); } else { - snprintf(buffer, 10, "%2d:%02d.%02d ", minutes, seconds, hundredths); + xSnprintf(buffer, 10, "%2d:%02d.%02d ", minutes, seconds, hundredths); } RichString_append(str, CRT_colors[DEFAULT_COLOR], buffer); } @@ -368,19 +368,19 @@ void Process_writeField(Process* this, RichString* str, ProcessField field) { switch (field) { case PERCENT_CPU: { if (this->percent_cpu > 999.9) { - snprintf(buffer, n, "%4d ", (unsigned int)this->percent_cpu); + xSnprintf(buffer, n, "%4d ", (unsigned int)this->percent_cpu); } else if (this->percent_cpu > 99.9) { - snprintf(buffer, n, "%3d. ", (unsigned int)this->percent_cpu); + xSnprintf(buffer, n, "%3d. ", (unsigned int)this->percent_cpu); } else { - snprintf(buffer, n, "%4.1f ", this->percent_cpu); + xSnprintf(buffer, n, "%4.1f ", this->percent_cpu); } break; } case PERCENT_MEM: { if (this->percent_mem > 99.9) { - snprintf(buffer, n, "100. "); + xSnprintf(buffer, n, "100. "); } else { - snprintf(buffer, n, "%4.1f ", this->percent_mem); + xSnprintf(buffer, n, "%4.1f ", this->percent_mem); } break; } @@ -411,7 +411,7 @@ void Process_writeField(Process* this, RichString* str, ProcessField field) { n -= written; } const char* draw = CRT_treeStr[lastItem ? (this->settings->direction == 1 ? TREE_STR_BEND : TREE_STR_TEND) : TREE_STR_RTEE]; - snprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] ); + xSnprintf(buf, n, "%s%s ", draw, this->showChildren ? CRT_treeStr[TREE_STR_SHUT] : CRT_treeStr[TREE_STR_OPEN] ); RichString_append(str, CRT_colors[PROCESS_TREE], buffer); Process_writeCommand(this, attr, baseattr, str); return; @@ -422,28 +422,28 @@ void Process_writeField(Process* this, RichString* str, ProcessField field) { case M_RESIDENT: Process_humanNumber(str, this->m_resident * PAGE_SIZE_KB, coloring); return; case M_SIZE: Process_humanNumber(str, this->m_size * PAGE_SIZE_KB, coloring); return; case NICE: { - snprintf(buffer, n, "%3ld ", this->nice); + xSnprintf(buffer, n, "%3ld ", this->nice); attr = this->nice < 0 ? CRT_colors[PROCESS_HIGH_PRIORITY] : this->nice > 0 ? CRT_colors[PROCESS_LOW_PRIORITY] : attr; break; } - case NLWP: snprintf(buffer, n, "%4ld ", this->nlwp); break; - case PGRP: snprintf(buffer, n, Process_pidFormat, this->pgrp); break; - case PID: snprintf(buffer, n, Process_pidFormat, this->pid); break; - case PPID: snprintf(buffer, n, Process_pidFormat, this->ppid); break; + case NLWP: xSnprintf(buffer, n, "%4ld ", this->nlwp); break; + case PGRP: xSnprintf(buffer, n, Process_pidFormat, this->pgrp); break; + case PID: xSnprintf(buffer, n, Process_pidFormat, this->pid); break; + case PPID: xSnprintf(buffer, n, Process_pidFormat, this->ppid); break; case PRIORITY: { if(this->priority <= -100) - snprintf(buffer, n, " RT "); + xSnprintf(buffer, n, " RT "); else - snprintf(buffer, n, "%3ld ", this->priority); + xSnprintf(buffer, n, "%3ld ", this->priority); break; } - case PROCESSOR: snprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break; - case SESSION: snprintf(buffer, n, Process_pidFormat, this->session); break; - case STARTTIME: snprintf(buffer, n, "%s", this->starttime_show); break; + case PROCESSOR: xSnprintf(buffer, n, "%3d ", Settings_cpuId(this->settings, this->processor)); break; + case SESSION: xSnprintf(buffer, n, Process_pidFormat, this->session); break; + case STARTTIME: xSnprintf(buffer, n, "%s", this->starttime_show); break; case STATE: { - snprintf(buffer, n, "%c ", this->state); + xSnprintf(buffer, n, "%c ", this->state); switch(this->state) { case 'R': attr = CRT_colors[PROCESS_R_STATE]; @@ -454,18 +454,18 @@ void Process_writeField(Process* this, RichString* str, ProcessField field) { } break; } - case ST_UID: snprintf(buffer, n, "%4d ", this->st_uid); break; + case ST_UID: xSnprintf(buffer, n, "%4d ", this->st_uid); break; case TIME: Process_printTime(str, this->time); return; - case TGID: snprintf(buffer, n, Process_pidFormat, this->tgid); break; - case TPGID: snprintf(buffer, n, Process_pidFormat, this->tpgid); break; - case TTY_NR: snprintf(buffer, n, "%3u:%3u ", major(this->tty_nr), minor(this->tty_nr)); break; + case TGID: xSnprintf(buffer, n, Process_pidFormat, this->tgid); break; + case TPGID: xSnprintf(buffer, n, Process_pidFormat, this->tpgid); break; + case TTY_NR: xSnprintf(buffer, n, "%3u:%3u ", major(this->tty_nr), minor(this->tty_nr)); break; case USER: { if (Process_getuid != (int) this->st_uid) attr = CRT_colors[PROCESS_SHADOW]; if (this->user) { - snprintf(buffer, n, "%-9s ", this->user); + xSnprintf(buffer, n, "%-9s ", this->user); } else { - snprintf(buffer, n, "%-9d ", this->st_uid); + xSnprintf(buffer, n, "%-9d ", this->st_uid); } if (buffer[9] != '\0') { buffer[9] = ' '; @@ -474,7 +474,7 @@ void Process_writeField(Process* this, RichString* str, ProcessField field) { break; } default: - snprintf(buffer, n, "- "); + xSnprintf(buffer, n, "- "); } RichString_append(str, attr, buffer); } diff --git a/SignalsPanel.c b/SignalsPanel.c index 34fef4b5..fea2ac72 100644 --- a/SignalsPanel.c +++ b/SignalsPanel.c @@ -44,7 +44,7 @@ Panel* SignalsPanel_new() { static char buf[15]; for (int sig = SIGRTMIN; sig <= SIGRTMAX; i++, sig++) { int n = sig - SIGRTMIN; - snprintf(buf, 15, "%2d SIGRTMIN%-+3d", sig, n); + xSnprintf(buf, 15, "%2d SIGRTMIN%-+3d", sig, n); if (n == 0) { buf[11] = '\0'; } diff --git a/TasksMeter.c b/TasksMeter.c index 3e695319..6a08d885 100644 --- a/TasksMeter.c +++ b/TasksMeter.c @@ -30,7 +30,7 @@ static void TasksMeter_updateValues(Meter* this, char* buffer, int len) { if (this->pl->settings->hideKernelThreads) { this->values[0] = 0; } - snprintf(buffer, len, "%d/%d", (int) this->values[3], (int) this->total); + xSnprintf(buffer, len, "%d/%d", (int) this->values[3], (int) this->total); } static void TasksMeter_display(Object* cast, RichString* out) { @@ -40,7 +40,7 @@ static void TasksMeter_display(Object* cast, RichString* out) { int processes = (int) this->values[2]; - snprintf(buffer, sizeof(buffer), "%d", processes); + xSnprintf(buffer, sizeof(buffer), "%d", processes); RichString_write(out, CRT_colors[METER_VALUE], buffer); int threadValueColor = CRT_colors[METER_VALUE]; int threadCaptionColor = CRT_colors[METER_TEXT]; @@ -50,18 +50,18 @@ static void TasksMeter_display(Object* cast, RichString* out) { } if (!settings->hideUserlandThreads) { RichString_append(out, CRT_colors[METER_TEXT], ", "); - snprintf(buffer, sizeof(buffer), "%d", (int)this->values[1]); + xSnprintf(buffer, sizeof(buffer), "%d", (int)this->values[1]); RichString_append(out, threadValueColor, buffer); RichString_append(out, threadCaptionColor, " thr"); } if (!settings->hideKernelThreads) { RichString_append(out, CRT_colors[METER_TEXT], ", "); - snprintf(buffer, sizeof(buffer), "%d", (int)this->values[0]); + xSnprintf(buffer, sizeof(buffer), "%d", (int)this->values[0]); RichString_append(out, threadValueColor, buffer); RichString_append(out, threadCaptionColor, " kthr"); } RichString_append(out, CRT_colors[METER_TEXT], "; "); - snprintf(buffer, sizeof(buffer), "%d", (int)this->values[3]); + xSnprintf(buffer, sizeof(buffer), "%d", (int)this->values[3]); RichString_append(out, CRT_colors[TASKS_RUNNING], buffer); RichString_append(out, CRT_colors[METER_TEXT], " running"); } diff --git a/TraceScreen.c b/TraceScreen.c index 5a49d5fd..abef7120 100644 --- a/TraceScreen.c +++ b/TraceScreen.c @@ -91,8 +91,8 @@ void TraceScreen_draw(InfoScreen* this) { bool TraceScreen_forkTracer(TraceScreen* this) { char buffer[1001]; - int err = pipe(this->fdpair); - if (err == -1) return false; + int error = pipe(this->fdpair); + if (error == -1) return false; this->child = fork(); if (this->child == -1) return false; if (this->child == 0) { @@ -100,7 +100,7 @@ bool TraceScreen_forkTracer(TraceScreen* this) { dup2(this->fdpair[1], STDERR_FILENO); int ok = fcntl(this->fdpair[1], F_SETFL, O_NONBLOCK); if (ok != -1) { - snprintf(buffer, sizeof(buffer), "%d", this->super.process->pid); + xSnprintf(buffer, sizeof(buffer), "%d", this->super.process->pid); execlp("strace", "strace", "-p", buffer, NULL); } const char* message = "Could not execute 'strace'. Please make sure it is available in your $PATH."; diff --git a/UptimeMeter.c b/UptimeMeter.c index 2fe603ae..61f60905 100644 --- a/UptimeMeter.c +++ b/UptimeMeter.c @@ -20,7 +20,7 @@ int UptimeMeter_attributes[] = { static void UptimeMeter_updateValues(Meter* this, char* buffer, int len) { int totalseconds = Platform_getUptime(); if (totalseconds == -1) { - snprintf(buffer, len, "(unknown)"); + xSnprintf(buffer, len, "(unknown)"); return; } int seconds = totalseconds % 60; @@ -31,17 +31,17 @@ static void UptimeMeter_updateValues(Meter* this, char* buffer, int len) { if (days > this->total) { this->total = days; } - char daysbuf[15]; + char daysbuf[32]; if (days > 100) { - snprintf(daysbuf, sizeof(daysbuf), "%d days(!), ", days); + xSnprintf(daysbuf, sizeof(daysbuf), "%d days(!), ", days); } else if (days > 1) { - snprintf(daysbuf, sizeof(daysbuf), "%d days, ", days); + xSnprintf(daysbuf, sizeof(daysbuf), "%d days, ", days); } else if (days == 1) { - snprintf(daysbuf, sizeof(daysbuf), "1 day, "); + xSnprintf(daysbuf, sizeof(daysbuf), "1 day, "); } else { daysbuf[0] = '\0'; } - snprintf(buffer, len, "%s%02d:%02d:%02d", daysbuf, hours, minutes, seconds); + xSnprintf(buffer, len, "%s%02d:%02d:%02d", daysbuf, hours, minutes, seconds); } MeterClass UptimeMeter_class = { diff --git a/XAlloc.c b/XAlloc.c index 5f2e9a4b..fbc642cc 100644 --- a/XAlloc.c +++ b/XAlloc.c @@ -5,11 +5,11 @@ #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif -#include #include #include /*{ +#include #include #include }*/ @@ -44,6 +44,8 @@ void* xRealloc(void* ptr, size_t size) { return data; } +#define xSnprintf(fmt, len, ...) do { int _l=len; int _n=snprintf(fmt, _l, __VA_ARGS__); if (!(_n > -1 && _n < _l)) { curs_set(1); endwin(); err(1, NULL); } } while(0) + #undef xStrdup #undef xStrdup_ #ifdef NDEBUG diff --git a/XAlloc.h b/XAlloc.h index eaebb087..2ebee1a8 100644 --- a/XAlloc.h +++ b/XAlloc.h @@ -7,6 +7,7 @@ #define _GNU_SOURCE #endif +#include #include #include @@ -16,6 +17,8 @@ void* xCalloc(size_t nmemb, size_t size); void* xRealloc(void* ptr, size_t size); +#define xSnprintf(fmt, len, ...) do { int _l=len; int _n=snprintf(fmt, _l, __VA_ARGS__); if (!(_n > -1 && _n < _l)) { curs_set(1); endwin(); err(1, NULL); } } while(0) + #undef xStrdup #undef xStrdup_ #ifdef NDEBUG diff --git a/dragonflybsd/DragonFlyBSDProcess.c b/dragonflybsd/DragonFlyBSDProcess.c index 77d709d8..dade106d 100644 --- a/dragonflybsd/DragonFlyBSDProcess.c +++ b/dragonflybsd/DragonFlyBSDProcess.c @@ -118,10 +118,10 @@ void DragonFlyBSDProcess_writeField(Process* this, RichString* str, ProcessField int n = sizeof(buffer) - 1; switch ((int) field) { // add Platform-specific fields here - case PID: snprintf(buffer, n, Process_pidFormat, (fp->kernel ? -1 : this->pid)); break; - case JID: snprintf(buffer, n, Process_pidFormat, fp->jid); break; + case PID: xSnprintf(buffer, n, Process_pidFormat, (fp->kernel ? -1 : this->pid)); break; + case JID: xSnprintf(buffer, n, Process_pidFormat, fp->jid); break; case JAIL:{ - snprintf(buffer, n, "%-11s ", fp->jname); break; + xSnprintf(buffer, n, "%-11s ", fp->jname); break; if (buffer[11] != '\0') { buffer[11] = ' '; buffer[12] = '\0'; diff --git a/freebsd/FreeBSDProcess.c b/freebsd/FreeBSDProcess.c index 851a0ef4..f81fadf5 100644 --- a/freebsd/FreeBSDProcess.c +++ b/freebsd/FreeBSDProcess.c @@ -117,9 +117,9 @@ void FreeBSDProcess_writeField(Process* this, RichString* str, ProcessField fiel int n = sizeof(buffer) - 1; switch ((int) field) { // add FreeBSD-specific fields here - case JID: snprintf(buffer, n, Process_pidFormat, fp->jid); break; + case JID: xSnprintf(buffer, n, Process_pidFormat, fp->jid); break; case JAIL:{ - snprintf(buffer, n, "%-11s ", fp->jname); break; + xSnprintf(buffer, n, "%-11s ", fp->jname); break; if (buffer[11] != '\0') { buffer[11] = ' '; buffer[12] = '\0'; diff --git a/freebsd/FreeBSDProcessList.c b/freebsd/FreeBSDProcessList.c index c0cb06b1..9fef324a 100644 --- a/freebsd/FreeBSDProcessList.c +++ b/freebsd/FreeBSDProcessList.c @@ -398,7 +398,7 @@ char* FreeBSDProcessList_readJailName(struct kinfo_proc* kproc) { jid = jail_get(jiov, 6, 0); if (jid < 0) { if (!jail_errmsg[0]) - snprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno)); + xSnprintf(jail_errmsg, JAIL_ERRMSGLEN, "jail_get: %s", strerror(errno)); return NULL; } else if (jid == kproc->ki_jid) { jname = xStrdup(jnamebuf); diff --git a/linux/Battery.c b/linux/Battery.c index e575b307..761b8ad7 100644 --- a/linux/Battery.c +++ b/linux/Battery.c @@ -56,7 +56,7 @@ static unsigned long int parseBatInfo(const char *fileName, const unsigned short unsigned long int total = 0; for (unsigned int i = 0; i < nBatteries; i++) { char infoPath[30]; - snprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName); + xSnprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName); FILE* file = fopen(infoPath, "r"); if (!file) { @@ -106,7 +106,7 @@ static ACPresence procAcpiCheck() { continue; char statePath[50]; - snprintf((char *) statePath, sizeof statePath, "%s/%s/state", power_supplyPath, entryName); + xSnprintf((char *) statePath, sizeof statePath, "%s/%s/state", power_supplyPath, entryName); FILE* file = fopen(statePath, "r"); if (!file) { @@ -196,7 +196,7 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { if (entryName[0] == 'B' && entryName[1] == 'A' && entryName[2] == 'T') { - snprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName); + xSnprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/uevent", entryName); int fd = open(filePath, O_RDONLY); if (fd == -1) { closedir(dir); @@ -249,7 +249,7 @@ static void Battery_getSysData(double* level, ACPresence* isOnAC) { continue; } - snprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName); + xSnprintf((char *) filePath, sizeof filePath, SYS_POWERSUPPLY_DIR "/%s/online", entryName); int fd = open(filePath, O_RDONLY); if (fd == -1) { closedir(dir); diff --git a/linux/IOPriorityPanel.c b/linux/IOPriorityPanel.c index 3085fb02..2b315b82 100644 --- a/linux/IOPriorityPanel.c +++ b/linux/IOPriorityPanel.c @@ -27,7 +27,7 @@ Panel* IOPriorityPanel_new(IOPriority currPrio) { for (int c = 0; classes[c].name; c++) { for (int i = 0; i < 8; i++) { char name[50]; - snprintf(name, sizeof(name)-1, "%s %d %s", classes[c].name, i, i == 0 ? "(High)" : (i == 7 ? "(Low)" : "")); + xSnprintf(name, sizeof(name)-1, "%s %d %s", classes[c].name, i, i == 0 ? "(High)" : (i == 7 ? "(Low)" : "")); IOPriority ioprio = IOPriority_tuple(classes[c].klass, i); Panel_add(this, (Object*) ListItem_new(name, ioprio)); if (currPrio == ioprio) Panel_setSelected(this, Panel_size(this) - 1); diff --git a/linux/LinuxProcess.c b/linux/LinuxProcess.c index afb3a7fb..bb9e99bb 100644 --- a/linux/LinuxProcess.c +++ b/linux/LinuxProcess.c @@ -296,10 +296,10 @@ void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field) switch ((int)field) { case TTY_NR: { if (lp->ttyDevice) { - snprintf(buffer, n, "%-9s", lp->ttyDevice + 5 /* skip "/dev/" */); + xSnprintf(buffer, n, "%-9s", lp->ttyDevice + 5 /* skip "/dev/" */); } else { attr = CRT_colors[PROCESS_SHADOW]; - snprintf(buffer, n, "? "); + xSnprintf(buffer, n, "? "); } break; } @@ -332,31 +332,31 @@ void LinuxProcess_writeField(Process* this, RichString* str, ProcessField field) } #endif #ifdef HAVE_OPENVZ - case CTID: snprintf(buffer, n, "%7u ", lp->ctid); break; - case VPID: snprintf(buffer, n, Process_pidFormat, lp->vpid); break; + case CTID: xSnprintf(buffer, n, "%7u ", lp->ctid); break; + case VPID: xSnprintf(buffer, n, Process_pidFormat, lp->vpid); break; #endif #ifdef HAVE_VSERVER - case VXID: snprintf(buffer, n, "%5u ", lp->vxid); break; + case VXID: xSnprintf(buffer, n, "%5u ", lp->vxid); break; #endif #ifdef HAVE_CGROUP - case CGROUP: snprintf(buffer, n, "%-10s ", lp->cgroup); break; + case CGROUP: xSnprintf(buffer, n, "%-10s ", lp->cgroup); break; #endif - case OOM: snprintf(buffer, n, Process_pidFormat, lp->oom); break; + case OOM: xSnprintf(buffer, n, Process_pidFormat, lp->oom); break; case IO_PRIORITY: { int klass = IOPriority_class(lp->ioPriority); if (klass == IOPRIO_CLASS_NONE) { // see note [1] above - snprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5); + xSnprintf(buffer, n, "B%1d ", (int) (this->nice + 20) / 5); } else if (klass == IOPRIO_CLASS_BE) { - snprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority)); + xSnprintf(buffer, n, "B%1d ", IOPriority_data(lp->ioPriority)); } else if (klass == IOPRIO_CLASS_RT) { attr = CRT_colors[PROCESS_HIGH_PRIORITY]; - snprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority)); + xSnprintf(buffer, n, "R%1d ", IOPriority_data(lp->ioPriority)); } else if (klass == IOPRIO_CLASS_IDLE) { attr = CRT_colors[PROCESS_LOW_PRIORITY]; - snprintf(buffer, n, "id "); + xSnprintf(buffer, n, "id "); } else { - snprintf(buffer, n, "?? "); + xSnprintf(buffer, n, "?? "); } break; } diff --git a/linux/LinuxProcessList.c b/linux/LinuxProcessList.c index 41d3b787..662aba87 100644 --- a/linux/LinuxProcessList.c +++ b/linux/LinuxProcessList.c @@ -248,7 +248,7 @@ static inline unsigned long long LinuxProcess_adjustTime(unsigned long long t) { static bool LinuxProcessList_readStatFile(Process *process, const char* dirname, const char* name, char* command, int* commLen) { LinuxProcess* lp = (LinuxProcess*) process; char filename[MAX_NAME+1]; - snprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name); + xSnprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name); int fd = open(filename, O_RDONLY); if (fd == -1) return false; @@ -326,7 +326,7 @@ static bool LinuxProcessList_statProcessDir(Process* process, const char* dirnam char filename[MAX_NAME+1]; filename[MAX_NAME] = '\0'; - snprintf(filename, MAX_NAME, "%s/%s", dirname, name); + xSnprintf(filename, MAX_NAME, "%s/%s", dirname, name); struct stat sstat; int statok = stat(filename, &sstat); if (statok == -1) @@ -348,7 +348,7 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirna char filename[MAX_NAME+1]; filename[MAX_NAME] = '\0'; - snprintf(filename, MAX_NAME, "%s/%s/io", dirname, name); + xSnprintf(filename, MAX_NAME, "%s/%s/io", dirname, name); int fd = open(filename, O_RDONLY); if (fd == -1) { process->io_rate_read_bps = -1; @@ -417,7 +417,7 @@ static void LinuxProcessList_readIoFile(LinuxProcess* process, const char* dirna static bool LinuxProcessList_readStatmFile(LinuxProcess* process, const char* dirname, const char* name) { char filename[MAX_NAME+1]; - snprintf(filename, MAX_NAME, "%s/%s/statm", dirname, name); + xSnprintf(filename, MAX_NAME, "%s/%s/statm", dirname, name); int fd = open(filename, O_RDONLY); if (fd == -1) return false; @@ -447,7 +447,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, const char* d return; } char filename[MAX_NAME+1]; - snprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name); + xSnprintf(filename, MAX_NAME, "%s/%s/stat", dirname, name); FILE* file = fopen(filename, "r"); if (!file) return; @@ -470,7 +470,7 @@ static void LinuxProcessList_readOpenVZData(LinuxProcess* process, const char* d static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* dirname, const char* name) { char filename[MAX_NAME+1]; - snprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name); + xSnprintf(filename, MAX_NAME, "%s/%s/cgroup", dirname, name); FILE* file = fopen(filename, "r"); if (!file) { process->cgroup = xStrdup(""); @@ -491,7 +491,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d at++; left--; } - int wrote = snprintf(at, left, "%s", group); + int wrote = xSnprintf(at, left, "%s", group); left -= wrote; } fclose(file); @@ -505,7 +505,7 @@ static void LinuxProcessList_readCGroupFile(LinuxProcess* process, const char* d static void LinuxProcessList_readVServerData(LinuxProcess* process, const char* dirname, const char* name) { char filename[MAX_NAME+1]; - snprintf(filename, MAX_NAME, "%s/%s/status", dirname, name); + xSnprintf(filename, MAX_NAME, "%s/%s/status", dirname, name); FILE* file = fopen(filename, "r"); if (!file) return; @@ -536,7 +536,7 @@ static void LinuxProcessList_readVServerData(LinuxProcess* process, const char* static void LinuxProcessList_readOomData(LinuxProcess* process, const char* dirname, const char* name) { char filename[MAX_NAME+1]; - snprintf(filename, MAX_NAME, "%s/%s/oom_score", dirname, name); + xSnprintf(filename, MAX_NAME, "%s/%s/oom_score", dirname, name); FILE* file = fopen(filename, "r"); if (!file) { return; @@ -567,7 +567,7 @@ static bool LinuxProcessList_readCmdlineFile(Process* process, const char* dirna return true; char filename[MAX_NAME+1]; - snprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name); + xSnprintf(filename, MAX_NAME, "%s/%s/cmdline", dirname, name); int fd = open(filename, O_RDONLY); if (fd == -1) return false; @@ -688,7 +688,7 @@ static bool LinuxProcessList_recurseProcTree(LinuxProcessList* this, const char* LinuxProcess* lp = (LinuxProcess*) proc; char subdirname[MAX_NAME+1]; - snprintf(subdirname, MAX_NAME, "%s/%s/task", dirname, name); + xSnprintf(subdirname, MAX_NAME, "%s/%s/task", dirname, name); LinuxProcessList_recurseProcTree(this, subdirname, proc, period, tv); #ifdef HAVE_TASKSTATS diff --git a/linux/Platform.c b/linux/Platform.c index de7bb84f..025abff6 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -215,7 +215,7 @@ void Platform_setSwapValues(Meter* this) { char* Platform_getProcessEnv(pid_t pid) { char procname[32+1]; - snprintf(procname, 32, "/proc/%d/environ", pid); + xSnprintf(procname, 32, "/proc/%d/environ", pid); FILE* fd = fopen(procname, "r"); char *env = NULL; if (fd) { -- cgit v1.2.3