spdk_top: Add asserts and checks for NULL pointers.

There are several memory allocations and funtion calls
that could theoretically return NULL in spdk_top. In
practice, these things are not likely to happen, but it
is necessary to add asserts and checks for them just in
case and to silence several KW warnings.

Signed-off-by: Seth Howell <seth.howell@intel.com>
Change-Id: I5f2f1a9050e47d22b725727bff156a128f1525c9
Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/2027
Community-CI: Mellanox Build Bot
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Tomasz Zawadzki <tomasz.zawadzki@intel.com>
Reviewed-by: Maciej Szwed <maciej.szwed@intel.com>
Reviewed-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-by: Aleksey Marchuk <alexeymar@mellanox.com>
Reviewed-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
This commit is contained in:
Seth Howell 2020-04-24 10:47:24 -07:00 committed by Tomasz Zawadzki
parent 597a7d08b5
commit be62a4c359

View File

@ -944,6 +944,10 @@ store_last_run_counter(const char *poller_name, uint64_t thread_id, uint64_t las
} }
history = calloc(1, sizeof(*history)); history = calloc(1, sizeof(*history));
if (history == NULL) {
fprintf(stderr, "Unable to allocate a history object in store_last_run_counter.\n");
return;
}
history->poller_name = strdup(poller_name); history->poller_name = strdup(poller_name);
history->thread_id = thread_id; history->thread_id = thread_id;
history->last_run_counter = last_run_counter; history->last_run_counter = last_run_counter;
@ -983,8 +987,10 @@ sort_pollers(const void *p1, const void *p2, void *arg)
return strcmp(poller1->thread_name, poller2->thread_name); return strcmp(poller1->thread_name, poller2->thread_name);
case 3: /* Sort by run counter */ case 3: /* Sort by run counter */
last_run_counter = get_last_run_counter(poller1->name, poller1->thread_id); last_run_counter = get_last_run_counter(poller1->name, poller1->thread_id);
assert(last_run_counter != NULL);
count1 = poller1->run_count - *last_run_counter; count1 = poller1->run_count - *last_run_counter;
last_run_counter = get_last_run_counter(poller2->name, poller2->thread_id); last_run_counter = get_last_run_counter(poller2->name, poller2->thread_id);
assert(last_run_counter != NULL);
count2 = poller2->run_count - *last_run_counter; count2 = poller2->run_count - *last_run_counter;
break; break;
case 4: /* Sort by period */ case 4: /* Sort by period */
@ -1021,6 +1027,7 @@ copy_pollers(struct rpc_pollers *pollers, uint64_t pollers_count, enum spdk_poll
last_run_counter = get_last_run_counter(pollers->pollers[i].name, thread->id); last_run_counter = get_last_run_counter(pollers->pollers[i].name, thread->id);
} }
assert(last_run_counter != NULL);
*last_run_counter = pollers->pollers[i].run_count; *last_run_counter = pollers->pollers[i].run_count;
} }
pollers_info[*current_count] = &pollers->pollers[i]; pollers_info[*current_count] = &pollers->pollers[i];
@ -1113,6 +1120,7 @@ refresh_pollers_tab(uint8_t current_page)
if (!col_desc[3].disabled) { if (!col_desc[3].disabled) {
last_run_counter = get_last_run_counter(pollers[i]->name, pollers[i]->thread_id); last_run_counter = get_last_run_counter(pollers[i]->name, pollers[i]->thread_id);
assert(last_run_counter != NULL);
snprintf(run_count, MAX_TIME_STR_LEN, "%" PRIu64, pollers[i]->run_count - *last_run_counter); snprintf(run_count, MAX_TIME_STR_LEN, "%" PRIu64, pollers[i]->run_count - *last_run_counter);
print_max_len(g_tabs[POLLERS_TAB], TABS_DATA_START_ROW + item_index, col, print_max_len(g_tabs[POLLERS_TAB], TABS_DATA_START_ROW + item_index, col,
@ -1377,6 +1385,10 @@ draw_filtering_menu(uint8_t position, WINDOW *filter_win, uint8_t tab, MENU **my
elements = i; elements = i;
my_items = (ITEM **)calloc(elements * WINDOW_COLUMNS + ADDITIONAL_ELEMENTS, sizeof(ITEM *)); my_items = (ITEM **)calloc(elements * WINDOW_COLUMNS + ADDITIONAL_ELEMENTS, sizeof(ITEM *));
if (my_items == NULL) {
fprintf(stderr, "Unable to allocate an item list in draw_filtering_menu.\n");
return NULL;
}
for (i = 0; i < elements * 2; i++) { for (i = 0; i < elements * 2; i++) {
my_items[i] = new_item(col_desc[i / WINDOW_COLUMNS].name, NULL); my_items[i] = new_item(col_desc[i / WINDOW_COLUMNS].name, NULL);
@ -1441,7 +1453,7 @@ filter_columns(uint8_t tab)
PANEL *filter_panel; PANEL *filter_panel;
WINDOW *filter_win; WINDOW *filter_win;
ITEM **my_items; ITEM **my_items;
MENU *my_menu; MENU *my_menu = NULL;
int i, c, elements; int i, c, elements;
bool stop_loop = false; bool stop_loop = false;
ITEM *cur; ITEM *cur;
@ -1552,6 +1564,10 @@ change_sorting(uint8_t tab)
elements = i; elements = i;
my_items = (ITEM **)calloc(elements + 1, sizeof(ITEM *)); my_items = (ITEM **)calloc(elements + 1, sizeof(ITEM *));
if (my_items == NULL) {
fprintf(stderr, "Unable to allocate an item list in change_sorting.\n");
return;
}
for (i = 0; i < elements; ++i) { for (i = 0; i < elements; ++i) {
my_items[i] = new_item(g_col_desc[tab][i].name, NULL); my_items[i] = new_item(g_col_desc[tab][i].name, NULL);