From 9d3295661600d6132da0953a93a6e1f95c4d1ab9 Mon Sep 17 00:00:00 2001 From: nhmall Date: Fri, 29 Dec 2023 10:38:37 -0500 Subject: [PATCH] curses static analyzer bits This is mostly just adding some Null guards ahead of code that was already dereferencing pointers, so there should be no change in behavior. Also adds one validation of an array index that was drawing a complaint. --- win/curses/cursdial.c | 16 ++++++++-------- win/curses/cursmesg.c | 29 ++++++++++++++++------------- win/curses/cursmisc.c | 1 + 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/win/curses/cursdial.c b/win/curses/cursdial.c index 62532df6f..f67be7551 100644 --- a/win/curses/cursdial.c +++ b/win/curses/cursdial.c @@ -1078,16 +1078,16 @@ menu_win_size(nhmenu *menu) /* Possibly reduce height if only 1 page */ if (!menu_is_multipage(menu, maxwidth, maxheight)) { - menu_item_ptr = menu->entries; + if ((menu_item_ptr = menu->entries) != 0) { + while (menu_item_ptr->next_item != NULL) { + menu_item_ptr = menu_item_ptr->next_item; + } - while (menu_item_ptr->next_item != NULL) { - menu_item_ptr = menu_item_ptr->next_item; - } + lastline = (menu_item_ptr->line_num) + menu_item_ptr->num_lines; - lastline = (menu_item_ptr->line_num) + menu_item_ptr->num_lines; - - if (lastline < maxheight) { - maxheight = lastline; + if (lastline < maxheight) { + maxheight = lastline; + } } } diff --git a/win/curses/cursmesg.c b/win/curses/cursmesg.c index 259187a57..f241384ef 100644 --- a/win/curses/cursmesg.c +++ b/win/curses/cursmesg.c @@ -947,8 +947,8 @@ mesg_add_line(const char *mline) ++num_messages; } else { /* at capacity; old head is being removed */ - first_mesg = first_mesg->next_mesg; /* new head */ - first_mesg->prev_mesg = NULL; /* head has no prev_mesg */ + if ((first_mesg = first_mesg->next_mesg) != 0) /* new head */ + first_mesg->prev_mesg = NULL; /* head has no prev_mesg */ } /* since 'current_mesg' might be reusing 'first_mesg' and has now become 'last_mesg', this update must be after head replacement */ @@ -1056,7 +1056,8 @@ curses_putmsghistory(const char *msg, boolean restoring_msghist) however, we aren't only called when restoring history; core uses putmsghistory() for other stuff during play and those messages should have a normal turn value */ - last_mesg->turn = restoring_msghist ? (1L << 3) : gh.hero_seq; + if (last_mesg) /* appease static analyzer */ + last_mesg->turn = restoring_msghist ? (1L << 3) : gh.hero_seq; #ifdef DUMPLOG dumplogmsg(last_mesg->str); #endif @@ -1072,18 +1073,20 @@ curses_putmsghistory(const char *msg, boolean restoring_msghist) stashed messages as newly occurring ones is much simpler; we ignore the backlinks because the list is destroyed as it gets processed hence there can't be any other traversals */ - mesg = stash_head; - stash_head = mesg->next_mesg; - --stash_count; - mesg_turn = mesg->turn; - mesg_add_line(mesg->str); - /* added line became new tail */ - last_mesg->turn = mesg_turn; + if ((mesg = stash_head) != 0) { + stash_head = mesg->next_mesg; + --stash_count; + mesg_turn = mesg->turn; + mesg_add_line(mesg->str); + /* added line became new tail */ + if (last_mesg) /* appease static analyzer */ + last_mesg->turn = mesg_turn; #ifdef DUMPLOG - dumplogmsg(mesg->str); + dumplogmsg(mesg->str); #endif - free((genericptr_t) mesg->str); - free((genericptr_t) mesg); + free((genericptr_t) mesg->str); + free((genericptr_t) mesg); + } } initd = FALSE; /* reset */ } diff --git a/win/curses/cursmisc.c b/win/curses/cursmisc.c index f3d740e06..85e372373 100644 --- a/win/curses/cursmisc.c +++ b/win/curses/cursmisc.c @@ -397,6 +397,7 @@ curses_str_remainder(const char *str, int width, int line_num) if (last_space == 0) { /* No spaces found */ last_space = count - 1; } + assert(IndexOk(last_space, substr)); if (substr[last_space] == '\0') { break; }