From: Matthew Dillon Date: Sat, 11 Dec 2010 09:01:33 +0000 (-0800) Subject: utilities - Fix top -M when the window does not have sufficient rows X-Git-Tag: v2.11.0~471 X-Git-Url: https://gitweb.dragonflybsd.org/~nant/dragonfly.git/commitdiff_plain/fbdccef0beb4b5580012ceac295c49699658e39d utilities - Fix top -M when the window does not have sufficient rows * Fixes a seg-fault. --- diff --git a/contrib/top/display.c b/contrib/top/display.c index 4d1bc7ecfb..f909fdfbee 100644 --- a/contrib/top/display.c +++ b/contrib/top/display.c @@ -797,32 +797,32 @@ display_init(struct statics *statics) /* call resize to do the dirty work */ top_lines = display_resize(); - /* only do the rest if we need to */ - if (top_lines > -1) + /* + * save pointers and allocate space for names. Even if top_lines <= -1 + * the code will dereference many of these pointers and arrays. + */ + procstate_names = statics->procstate_names; + num_procstates = string_count(procstate_names); + + lprocstates = (int *)calloc(num_procstates, sizeof(int)); + + cpustate_names = statics->cpustate_names; + num_cpustates = string_count(cpustate_names); + lcpustates = (int *)calloc(num_cpustates, sizeof(int)); + cpustate_columns = (int *)calloc(num_cpustates, sizeof(int)); + memory_names = statics->memory_names; + num_memory = string_count(memory_names); + + /* calculate starting columns where needed */ + cpustate_total_length = 0; + pp = cpustate_names; + ip = cpustate_columns; + while (*pp != NULL) { - /* save pointers and allocate space for names */ - procstate_names = statics->procstate_names; - num_procstates = string_count(procstate_names); - lprocstates = (int *)calloc(num_procstates, sizeof(int)); - - cpustate_names = statics->cpustate_names; - num_cpustates = string_count(cpustate_names); - lcpustates = (int *)calloc(num_cpustates, sizeof(int)); - cpustate_columns = (int *)calloc(num_cpustates, sizeof(int)); - memory_names = statics->memory_names; - num_memory = string_count(memory_names); - - /* calculate starting columns where needed */ - cpustate_total_length = 0; - pp = cpustate_names; - ip = cpustate_columns; - while (*pp != NULL) + *ip++ = cpustate_total_length; + if ((i = strlen(*pp++)) > 0) { - *ip++ = cpustate_total_length; - if ((i = strlen(*pp++)) > 0) - { - cpustate_total_length += i + 8; - } + cpustate_total_length += i + 8; } }