]> granicus.if.org Git - nethack/commitdiff
eliminate static analysis complaints about eat.c NetHack-3.7
authorPatR <rankin@nethack.org>
Mon, 6 Mar 2023 00:19:32 +0000 (16:19 -0800)
committerPatR <rankin@nethack.org>
Mon, 6 Mar 2023 00:19:32 +0000 (16:19 -0800)
This is fairly ridiculous but it prevents the bogus complaints
when compiling eat.c with 'gcc -fanalyzer' about some fields in
gc.context.victual being used without having been initialized.

There's bound to be a better way to handle this and I'm curious
whether it will work with the 'onefile' testing.

include/extern.h
src/decl.c
src/eat.c

index ecd49a89fa0c74decacc84a49ef4d105c3703873..17584d0a7124b2616840b99135df9949a7359625 100644 (file)
@@ -332,6 +332,7 @@ extern void destroy_drawbridge(coordxy, coordxy);
 /* ### decl.c ### */
 
 extern void decl_globals_init(void);
+extern void sa_victual(volatile struct victual_info *);
 
 /* ### detect.c ### */
 
index 77955cdc0103cc69a5ca2805ccc5ab8ab2fca309..a0714791ff39eec72b28a50f8609a81c75f4d5e8 100644 (file)
@@ -1048,4 +1048,17 @@ decl_globals_init(void)
 long verbosity_suppressions[vb_elements] = { 0L, 0L, 0L, 0L, 0L, };
 #endif
 
+/* gcc 12.2's static analyzer thinks that some fields of gc.context.victual
+   are uninitialized when compiling 'bite(eat.c)' but that's impossible;
+   it is defined at global scope so guaranteed to be given implicit
+   initialization for fields that aren't explicitly initialized (all of
+   'context'); having bite() pass &gc.context.victual to this no-op
+   eliminates the analyzer's very verbose complaint */
+void
+sa_victual(
+    volatile struct victual_info *context_victual UNUSED)
+{
+    return;
+}
+
 /*decl.c*/
index 6b4086cfbb3faaa96375a4759c74fd055a82e556..bed27d182db24bb7f774f31590d12faceb6d24e0 100644 (file)
--- a/src/eat.c
+++ b/src/eat.c
@@ -2983,6 +2983,9 @@ use_tin_opener(struct obj *obj)
 static int
 bite(void)
 {
+    /* hack to pacify static analyzer incorporated into gcc 12.2 */
+    sa_victual(&gc.context.victual);
+
     if (gc.context.victual.canchoke && u.uhunger >= 2000) {
         choke(gc.context.victual.piece);
         return 1;