Commit bca656c7 authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Fix crashes when process list is empty

parent 368cb1fe
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,
......
...@@ -199,8 +199,10 @@ Object* Panel_remove(Panel* this, int i) { ...@@ -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) {
......
...@@ -189,7 +189,7 @@ static bool changePriority(Panel* panel, int delta) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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) { ...@@ -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();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment