Add missing check for return value and be more conservative in constructs to avoid future slips of this kind.

This commit is contained in:
Hisham Muhammad 2014-04-09 14:47:32 -03:00
parent 369bef279a
commit 1d9e1b0118
1 changed files with 17 additions and 6 deletions

23
htop.c
View File

@ -211,6 +211,14 @@ static bool changePriority(Panel* panel, int delta) {
return anyTagged; return anyTagged;
} }
static int selectedPid(Panel* panel) {
Process* p = (Process*) Panel_getSelected(panel);
if (p) {
return p->pid;
}
return -1;
}
static Object* pickFromVector(Panel* panel, Panel* list, int x, int y, const char** keyLabels, FunctionBar* prevBar, Header* header) { static Object* pickFromVector(Panel* panel, Panel* list, int x, int y, const char** keyLabels, FunctionBar* prevBar, Header* header) {
const char* fuKeys[] = {"Enter", "Esc", NULL}; const char* fuKeys[] = {"Enter", "Esc", NULL};
int fuEvents[] = {13, 27}; int fuEvents[] = {13, 27};
@ -221,8 +229,7 @@ static Object* pickFromVector(Panel* panel, Panel* list, int x, int y, const cha
Panel* panelFocus; Panel* panelFocus;
int ch; int ch;
bool unfollow = false; bool unfollow = false;
Process* p = (Process*)Panel_getSelected(panel); int pid = selectedPid(panel);
int pid = p ? p->pid : -1;
if (header->pl->following == -1) { if (header->pl->following == -1) {
header->pl->following = pid; header->pl->following = pid;
unfollow = true; unfollow = true;
@ -237,7 +244,7 @@ static Object* pickFromVector(Panel* panel, Panel* list, int x, int y, const cha
FunctionBar_draw(prevBar, NULL); FunctionBar_draw(prevBar, NULL);
if (panelFocus == list && ch == 13) { if (panelFocus == list && ch == 13) {
Process* selected = (Process*)Panel_getSelected(panel); Process* selected = (Process*)Panel_getSelected(panel);
if (selected->pid == pid) if (selected && selected->pid == pid)
return Panel_getSelected(list); return Panel_getSelected(list);
else else
beep(); beep();
@ -465,8 +472,7 @@ 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 > settings->delay); recalculate = (newTime - oldTime > settings->delay);
Process* p = (Process*)Panel_getSelected(panel); int following = follow ? selectedPid(panel) : -1;
int following = (follow && p) ? p->pid : -1;
if (recalculate) { if (recalculate) {
Header_draw(header); Header_draw(header);
oldTime = newTime; oldTime = newTime;
@ -551,8 +557,13 @@ int main(int argc, char** argv) {
if (isdigit((char)ch)) { if (isdigit((char)ch)) {
if (Panel_size(panel) == 0) continue; 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); i++) {
Panel_setSelected(panel, i); Panel_setSelected(panel, i);
Process* p = (Process*) Panel_getSelected(panel);
if (p && p->pid == pid) {
break;
}
}
acc = pid * 10; acc = pid * 10;
if (acc > 10000000) if (acc > 10000000)
acc = 0; acc = 0;