From f61cfe7e486fe6d9d165e1343c7b3a5ae1c4ad32 Mon Sep 17 00:00:00 2001 From: PatR Date: Wed, 18 Jan 2023 11:57:49 -0800 Subject: [PATCH] pacify static analyzer - vision.c Include some assertions to convince the analyzer that some pointers can't be Null. They're Null if 'vis_func' is non-Null but only used when that function pointer is Null and they have values. If there's a macro that's defined when the analyzer is running and undefined when not--or vice versa--it could be used to control NDEBUG and avoid the assertion code when not analyzing. That's a bit like using fake code to pacify 'lint'; however, since the assertions should never fail, suppressing them isn't really switching to fake code. I reordered a couple of macros so that the set of them matches the comment which precedes them and refers to "the last three". It is referring to the three within the block comment rather than the macro defintions but putting those in the same order removes any ambiguity. --- src/vision.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/vision.c b/src/vision.c index 7167d9632..f48461f9d 100644 --- a/src/vision.c +++ b/src/vision.c @@ -3,6 +3,7 @@ /* NetHack may be freely redistributed. See license for details. */ #include "hack.h" +#include /* Circles * ==================================================================*/ @@ -1129,16 +1130,29 @@ static genericptr_t varg; * * The last three macros depend on having local pointers row_min, row_max, * and rowp being set correctly. + * + * The assertions are included to pacify a static source code analyzer. + * Compile with NDEBUG defined to suppress them. */ -#define set_cs(rowp, col) (rowp[col] = COULD_SEE) -#define good_row(z) ((z) >= 0 && (z) < ROWNO) -#define set_min(z) \ - if (*row_min > (z)) \ - *row_min = (z) -#define set_max(z) \ - if (*row_max < (z)) \ - *row_max = (z) #define is_clear(row, col) viz_clear_rows[row][col] +#define good_row(z) ((z) >= 0 && (z) < ROWNO) +#define set_cs(rowp, col) \ + do { \ + assert(rowp != NULL); \ + rowp[col] = COULD_SEE; \ + } while (0) +#define set_min(z) \ + do { \ + assert(row_min != NULL); \ + if (*row_min > (z)) \ + *row_min = (z); \ + } while (0) +#define set_max(z) \ + do { \ + assert(row_max != NULL); \ + if (*row_max < (z)) \ + *row_max = (z); \ + } while (0) /* * clear_path() expanded into 4 macros/functions: