From 8c653212c0e6d15997e4217a514301f5682a41c4 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sun, 18 Feb 2018 10:38:49 -0300 Subject: [PATCH] Replace size_t with int/void* union I was occasionally passing negative values to size_t. Plus, this better reflects the intent of the variant argument. Reported by Coverity: https://scan8.coverity.com/reports.htm#v13253/p10402/fileInstanceId=22093891&defectInstanceId=7543346&mergedDefectId=174179&fileStart=251&fileEnd=500 --- Action.c | 6 +++--- InfoScreen.c | 5 +++-- MainPanel.c | 9 +++++++-- MainPanel.h | 9 +++++++-- OpenFilesScreen.c | 2 +- Process.c | 4 ++-- Process.h | 4 ++-- TraceScreen.c | 3 ++- linux/Platform.c | 2 +- 9 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Action.c b/Action.c index 50ab0bb9..1cb59aa0 100644 --- a/Action.c +++ b/Action.c @@ -115,7 +115,7 @@ static void Action_runSetup(Settings* settings, const Header* header, ProcessLis static bool changePriority(MainPanel* panel, int delta) { bool anyTagged; - bool ok = MainPanel_foreachProcess(panel, (MainPanel_ForeachProcessFn) Process_changePriorityBy, delta, &anyTagged); + bool ok = MainPanel_foreachProcess(panel, (MainPanel_ForeachProcessFn) Process_changePriorityBy, (Arg){ .i = delta }, &anyTagged); if (!ok) beep(); return anyTagged; @@ -285,7 +285,7 @@ static Htop_Reaction actionSetAffinity(State* st) { void* set = Action_pickFromVector(st, affinityPanel, 15); if (set) { Affinity* affinity = AffinityPanel_getAffinity(affinityPanel, st->pl); - bool ok = MainPanel_foreachProcess((MainPanel*)panel, (MainPanel_ForeachProcessFn) Affinity_set, (size_t) affinity, NULL); + bool ok = MainPanel_foreachProcess((MainPanel*)panel, (MainPanel_ForeachProcessFn) Affinity_set, (Arg){ .v = affinity }, NULL); if (!ok) beep(); Affinity_delete(affinity); } @@ -302,7 +302,7 @@ static Htop_Reaction actionKill(State* st) { Panel_setHeader(st->panel, "Sending..."); Panel_draw(st->panel, true); refresh(); - MainPanel_foreachProcess((MainPanel*)st->panel, (MainPanel_ForeachProcessFn) Process_sendSignal, (size_t) sgn->key, NULL); + MainPanel_foreachProcess((MainPanel*)st->panel, (MainPanel_ForeachProcessFn) Process_sendSignal, (Arg){ .i = sgn->key }, NULL); napms(500); } } diff --git a/InfoScreen.c b/InfoScreen.c index dd5095c7..fab8daea 100644 --- a/InfoScreen.c +++ b/InfoScreen.c @@ -115,8 +115,9 @@ void InfoScreen_run(InfoScreen* this) { Panel_draw(panel, true); - if (this->inc->active) - move(LINES-1, CRT_cursorX); + if (this->inc->active) { + (void) move(LINES-1, CRT_cursorX); + } set_escdelay(25); int ch = getch(); diff --git a/MainPanel.c b/MainPanel.c index b5a7e305..25023367 100644 --- a/MainPanel.c +++ b/MainPanel.c @@ -25,7 +25,12 @@ typedef struct MainPanel_ { pid_t pidSearch; } MainPanel; -typedef bool(*MainPanel_ForeachProcessFn)(Process*, size_t); +typedef union { + int i; + void* v; +} Arg; + +typedef bool(*MainPanel_ForeachProcessFn)(Process*, Arg); #define MainPanel_getFunctionBar(this_) (((Panel*)(this_))->defaultBar) @@ -148,7 +153,7 @@ const char* MainPanel_getValue(MainPanel* this, int i) { return ""; } -bool MainPanel_foreachProcess(MainPanel* this, MainPanel_ForeachProcessFn fn, size_t arg, bool* wasAnyTagged) { +bool MainPanel_foreachProcess(MainPanel* this, MainPanel_ForeachProcessFn fn, Arg arg, bool* wasAnyTagged) { Panel* super = (Panel*) this; bool ok = true; bool anyTagged = false; diff --git a/MainPanel.h b/MainPanel.h index f4671f33..88496597 100644 --- a/MainPanel.h +++ b/MainPanel.h @@ -21,7 +21,12 @@ typedef struct MainPanel_ { pid_t pidSearch; } MainPanel; -typedef bool(*MainPanel_ForeachProcessFn)(Process*, size_t); +typedef union { + int i; + void* v; +} Arg; + +typedef bool(*MainPanel_ForeachProcessFn)(Process*, Arg); #define MainPanel_getFunctionBar(this_) (((Panel*)(this_))->defaultBar) @@ -34,7 +39,7 @@ int MainPanel_selectedPid(MainPanel* this); const char* MainPanel_getValue(MainPanel* this, int i); -bool MainPanel_foreachProcess(MainPanel* this, MainPanel_ForeachProcessFn fn, size_t arg, bool* wasAnyTagged); +bool MainPanel_foreachProcess(MainPanel* this, MainPanel_ForeachProcessFn fn, Arg arg, bool* wasAnyTagged); extern PanelClass MainPanel_class; diff --git a/OpenFilesScreen.c b/OpenFilesScreen.c index a772bbac..f18511bb 100644 --- a/OpenFilesScreen.c +++ b/OpenFilesScreen.c @@ -130,7 +130,7 @@ void OpenFilesScreen_scan(InfoScreen* this) { char** data = fdata->data.data; int lenN = data['n'] ? strlen(data['n']) : 0; int sizeEntry = 5 + 7 + 10 + 10 + 10 + lenN + 5 /*spaces*/ + 1 /*null*/; - char* entry = xMalloc(sizeEntry); + char entry[sizeEntry]; xSnprintf(entry, sizeEntry, "%5.5s %7.7s %10.10s %10.10s %10.10s %s", data['f'] ? data['f'] : "", data['t'] ? data['t'] : "", diff --git a/Process.c b/Process.c index 18360802..2ff778df 100644 --- a/Process.c +++ b/Process.c @@ -536,11 +536,11 @@ bool Process_setPriority(Process* this, int priority) { return (err == 0); } -bool Process_changePriorityBy(Process* this, size_t delta) { +bool Process_changePriorityBy(Process* this, int delta) { return Process_setPriority(this, this->nice + delta); } -void Process_sendSignal(Process* this, size_t sgn) { +void Process_sendSignal(Process* this, int sgn) { CRT_dropPrivileges(); kill(this->pid, (int) sgn); CRT_restorePrivileges(); diff --git a/Process.h b/Process.h index 5179bb6f..6c41edc2 100644 --- a/Process.h +++ b/Process.h @@ -190,9 +190,9 @@ void Process_toggleTag(Process* this); bool Process_setPriority(Process* this, int priority); -bool Process_changePriorityBy(Process* this, size_t delta); +bool Process_changePriorityBy(Process* this, int delta); -void Process_sendSignal(Process* this, size_t sgn); +void Process_sendSignal(Process* this, int sgn); long Process_pidCompare(const void* v1, const void* v2); diff --git a/TraceScreen.c b/TraceScreen.c index abef7120..845ceaab 100644 --- a/TraceScreen.c +++ b/TraceScreen.c @@ -108,7 +108,8 @@ bool TraceScreen_forkTracer(TraceScreen* this) { (void) written; exit(1); } - fcntl(this->fdpair[0], F_SETFL, O_NONBLOCK); + int ok = fcntl(this->fdpair[0], F_SETFL, O_NONBLOCK); + if (ok == -1) return false; this->strace = fdopen(this->fdpair[0], "r"); this->fd_strace = fileno(this->strace); return true; diff --git a/linux/Platform.c b/linux/Platform.c index 025abff6..ab90ca74 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -96,7 +96,7 @@ static Htop_Reaction Platform_actionSetIOPriority(State* st) { void* set = Action_pickFromVector(st, ioprioPanel, 21); if (set) { IOPriority ioprio = IOPriorityPanel_getIOPriority(ioprioPanel); - bool ok = MainPanel_foreachProcess((MainPanel*)panel, (MainPanel_ForeachProcessFn) LinuxProcess_setIOPriority, (size_t) ioprio, NULL); + bool ok = MainPanel_foreachProcess((MainPanel*)panel, (MainPanel_ForeachProcessFn) LinuxProcess_setIOPriority, (Arg){ .i = ioprio }, NULL); if (!ok) beep(); }