Fix crashes when process list is empty

This commit is contained in:
Hisham Muhammad 2012-03-05 11:18:27 +00:00
parent 368cb1fe20
commit bca656c79e
3 changed files with 30 additions and 11 deletions

View File

@ -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 What's new in version 1.0.1
* Move .htoprc to XDG-compliant path ~/.config/htop/htoprc, * Move .htoprc to XDG-compliant path ~/.config/htop/htoprc,

View File

@ -199,8 +199,10 @@ Object* Panel_remove(Panel* this, int i) {
Object* Panel_getSelected(Panel* this) { Object* Panel_getSelected(Panel* this) {
assert (this != NULL); assert (this != NULL);
if (Vector_size(this->items) > 0)
return Vector_get(this->items, this->selected); return Vector_get(this->items, this->selected);
else
return NULL;
} }
void Panel_moveSelectedUp(Panel* this) { void Panel_moveSelectedUp(Panel* this) {

30
htop.c
View File

@ -189,7 +189,7 @@ static bool changePriority(Panel* panel, int delta) {
} }
if (!anyTagged) { if (!anyTagged) {
Process* p = (Process*) Panel_getSelected(panel); 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) if (!ok)
beep(); beep();
@ -440,7 +440,8 @@ int main(int argc, char** argv) {
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000); newTime = ((double)tv.tv_sec * 10) + ((double)tv.tv_usec / 100000);
recalculate = (newTime - oldTime > CRT_delay); 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) if (recalculate)
oldTime = newTime; oldTime = newTime;
if (doRefresh) { if (doRefresh) {
@ -481,6 +482,7 @@ int main(int argc, char** argv) {
doRefresh = false; doRefresh = false;
int size = Panel_size(panel); int size = Panel_size(panel);
if (ch == KEY_F(3)) { if (ch == KEY_F(3)) {
if (Panel_size(panel) == 0) continue;
int here = Panel_getSelectedIndex(panel); int here = Panel_getSelectedIndex(panel);
int i = here+1; int i = here+1;
while (i != here) { while (i != here) {
@ -541,11 +543,12 @@ int main(int argc, char** argv) {
continue; continue;
} }
if (isdigit((char)ch)) { if (isdigit((char)ch)) {
if (Panel_size(panel) == 0) continue;
pid_t pid = ch-48 + acc; pid_t pid = ch-48 + acc;
for (int i = 0; i < ProcessList_size(pl) && ((Process*) Panel_getSelected(panel))->pid != pid; i++) for (int i = 0; i < ProcessList_size(pl) && ((Process*) Panel_getSelected(panel))->pid != pid; i++)
Panel_setSelected(panel, i); Panel_setSelected(panel, i);
acc = pid * 10; acc = pid * 10;
if (acc > 100000) if (acc > 10000000)
acc = 0; acc = 0;
continue; continue;
} else { } else {
@ -644,13 +647,16 @@ int main(int argc, char** argv) {
case ' ': case ' ':
{ {
Process* p = (Process*) Panel_getSelected(panel); Process* p = (Process*) Panel_getSelected(panel);
if (!p) break;
Process_toggleTag(p); Process_toggleTag(p);
Panel_onKey(panel, KEY_DOWN); Panel_onKey(panel, KEY_DOWN);
break; break;
} }
case 's': 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_run(ts);
TraceScreen_delete(ts); TraceScreen_delete(ts);
clear(); clear();
@ -661,7 +667,9 @@ int main(int argc, char** argv) {
} }
case 'l': 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_run(ts);
OpenFilesScreen_delete(ts); OpenFilesScreen_delete(ts);
clear(); clear();
@ -714,6 +722,7 @@ int main(int argc, char** argv) {
case '-': case '-':
{ {
Process* p = (Process*) Panel_getSelected(panel); Process* p = (Process*) Panel_getSelected(panel);
if (!p) break;
p->showChildren = !p->showChildren; p->showChildren = !p->showChildren;
refreshTimeout = 0; refreshTimeout = 0;
doRecalculate = true; doRecalculate = true;
@ -726,7 +735,7 @@ int main(int argc, char** argv) {
killPanel = (Panel*) SignalsPanel_new(0, 0, 0, 0); killPanel = (Panel*) SignalsPanel_new(0, 0, 0, 0);
} }
bool anyTagged = false; bool anyTagged = false;
pid_t selectedPid; pid_t selectedPid = 0;
for (int i = 0; i < Panel_size(panel); i++) { for (int i = 0; i < Panel_size(panel); i++) {
Process* p = (Process*) Panel_get(panel, i); Process* p = (Process*) Panel_get(panel, i);
if (p->tag) { if (p->tag) {
@ -736,8 +745,9 @@ int main(int argc, char** argv) {
} }
if (!anyTagged) { if (!anyTagged) {
Process* p = (Process*) Panel_getSelected(panel); Process* p = (Process*) Panel_getSelected(panel);
selectedPid = p->pid; if (p) selectedPid = p->pid;
} }
if (selectedPid == 0) break;
SignalsPanel_reset((SignalsPanel*) killPanel); SignalsPanel_reset((SignalsPanel*) killPanel);
const char* fuFunctions[] = {"Send ", "Cancel ", NULL}; const char* fuFunctions[] = {"Send ", "Cancel ", NULL};
ListItem* sgn = (ListItem*) pickFromVector(panel, killPanel, 15, headerHeight, fuFunctions, defaultBar, header); 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) if (pl->cpuCount == 1)
break; 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; if (!affinity) break;
Panel* affinityPanel = AffinityPanel_new(pl, affinity); Panel* affinityPanel = AffinityPanel_new(pl, affinity);
Affinity_delete(affinity); Affinity_delete(affinity);
@ -792,7 +804,7 @@ int main(int argc, char** argv) {
} }
if (!anyTagged) { if (!anyTagged) {
Process* p = (Process*) Panel_getSelected(panel); Process* p = (Process*) Panel_getSelected(panel);
ok = Process_setAffinity(p, affinity) && ok; if (p) ok = Process_setAffinity(p, affinity) && ok;
} }
if (!ok) if (!ok)
beep(); beep();