From bca656c79e184fb044ad9f71c323070d25623486 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 5 Mar 2012 11:18:27 +0000 Subject: Fix crashes when process list is empty --- ChangeLog | 5 +++++ Panel.c | 6 ++++-- htop.c | 30 +++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 568ec872..5185f7ad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,9 @@ +What's new in version 1.0.2 + +* Avoid deleting .htoprc if it is a symlink +* BUGFIX: Fix crashes when process list is empty + What's new in version 1.0.1 * Move .htoprc to XDG-compliant path ~/.config/htop/htoprc, diff --git a/Panel.c b/Panel.c index b090c525..e8de2b86 100644 --- a/Panel.c +++ b/Panel.c @@ -199,8 +199,10 @@ Object* Panel_remove(Panel* this, int i) { Object* Panel_getSelected(Panel* this) { assert (this != NULL); - - return Vector_get(this->items, this->selected); + if (Vector_size(this->items) > 0) + return Vector_get(this->items, this->selected); + else + return NULL; } void Panel_moveSelectedUp(Panel* this) { diff --git a/htop.c b/htop.c index dd760713..943a7241 100644 --- a/htop.c +++ b/htop.c @@ -189,7 +189,7 @@ static bool changePriority(Panel* panel, int delta) { } if (!anyTagged) { Process* p = (Process*) Panel_getSelected(panel); - ok = Process_setPriority(p, p->nice + delta) && ok; + if (p) ok = Process_setPriority(p, p->nice + delta) && ok; } if (!ok) beep(); @@ -440,7 +440,8 @@ int main(int argc, char** argv) { gettimeofday(&tv, NULL); newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000); recalculate = (newTime - oldTime > CRT_delay); - int following = follow ? ((Process*)Panel_getSelected(panel))->pid : -1; + Process* p = (Process*)Panel_getSelected(panel); + int following = (follow && p) ? p->pid : -1; if (recalculate) oldTime = newTime; if (doRefresh) { @@ -481,6 +482,7 @@ int main(int argc, char** argv) { doRefresh = false; int size = Panel_size(panel); if (ch == KEY_F(3)) { + if (Panel_size(panel) == 0) continue; int here = Panel_getSelectedIndex(panel); int i = here+1; while (i != here) { @@ -541,11 +543,12 @@ int main(int argc, char** argv) { continue; } if (isdigit((char)ch)) { + if (Panel_size(panel) == 0) continue; pid_t pid = ch-48 + acc; for (int i = 0; i < ProcessList_size(pl) && ((Process*) Panel_getSelected(panel))->pid != pid; i++) Panel_setSelected(panel, i); acc = pid * 10; - if (acc > 100000) + if (acc > 10000000) acc = 0; continue; } else { @@ -644,13 +647,16 @@ int main(int argc, char** argv) { case ' ': { Process* p = (Process*) Panel_getSelected(panel); + if (!p) break; Process_toggleTag(p); Panel_onKey(panel, KEY_DOWN); break; } case 's': { - TraceScreen* ts = TraceScreen_new((Process*) Panel_getSelected(panel)); + Process* p = (Process*) Panel_getSelected(panel); + if (!p) break; + TraceScreen* ts = TraceScreen_new(p); TraceScreen_run(ts); TraceScreen_delete(ts); clear(); @@ -661,7 +667,9 @@ int main(int argc, char** argv) { } case 'l': { - OpenFilesScreen* ts = OpenFilesScreen_new((Process*) Panel_getSelected(panel)); + Process* p = (Process*) Panel_getSelected(panel); + if (!p) break; + OpenFilesScreen* ts = OpenFilesScreen_new(p); OpenFilesScreen_run(ts); OpenFilesScreen_delete(ts); clear(); @@ -714,6 +722,7 @@ int main(int argc, char** argv) { case '-': { Process* p = (Process*) Panel_getSelected(panel); + if (!p) break; p->showChildren = !p->showChildren; refreshTimeout = 0; doRecalculate = true; @@ -726,7 +735,7 @@ int main(int argc, char** argv) { killPanel = (Panel*) SignalsPanel_new(0, 0, 0, 0); } bool anyTagged = false; - pid_t selectedPid; + pid_t selectedPid = 0; for (int i = 0; i < Panel_size(panel); i++) { Process* p = (Process*) Panel_get(panel, i); if (p->tag) { @@ -736,8 +745,9 @@ int main(int argc, char** argv) { } if (!anyTagged) { Process* p = (Process*) Panel_getSelected(panel); - selectedPid = p->pid; + if (p) selectedPid = p->pid; } + if (selectedPid == 0) break; SignalsPanel_reset((SignalsPanel*) killPanel); const char* fuFunctions[] = {"Send ", "Cancel ", NULL}; ListItem* sgn = (ListItem*) pickFromVector(panel, killPanel, 15, headerHeight, fuFunctions, defaultBar, header); @@ -772,7 +782,9 @@ int main(int argc, char** argv) { if (pl->cpuCount == 1) break; - Affinity* affinity = Process_getAffinity((Process*) Panel_getSelected(panel)); + Process* p = (Process*) Panel_getSelected(panel); + if (!p) break; + Affinity* affinity = Process_getAffinity(p); if (!affinity) break; Panel* affinityPanel = AffinityPanel_new(pl, affinity); Affinity_delete(affinity); @@ -792,7 +804,7 @@ int main(int argc, char** argv) { } if (!anyTagged) { Process* p = (Process*) Panel_getSelected(panel); - ok = Process_setAffinity(p, affinity) && ok; + if (p) ok = Process_setAffinity(p, affinity) && ok; } if (!ok) beep(); -- cgit v1.2.3