From 36e8e504c238c99f43f614f9779f5e735f48e81a Mon Sep 17 00:00:00 2001 From: nhmall Date: Sun, 24 Dec 2023 15:38:06 -0500 Subject: [PATCH] another static analyzer bit for wield.c src/wield.c(745): warning: Dereferencing NULL pointer 'obj'. See line 685 for an earlier location where this can occur In wield_tool(), the comparisons against uwep were intended for when uwep wasn't null. gcc/clang analyzers now have some awareness of obj arg being notnull for wield_tool() since the extern.h prototypes were changed to declare that, but other compilers/analyzers do not necessarily have that information, and this: 'if (obj == uwep)' would be a match if both were NULL. --- src/wield.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wield.c b/src/wield.c index 11565e3bf..6920c9433 100644 --- a/src/wield.c +++ b/src/wield.c @@ -52,10 +52,10 @@ * No item may be in more than one of these slots. */ -static boolean cant_wield_corpse(struct obj *); -static int ready_weapon(struct obj *); -static int ready_ok(struct obj *); -static int wield_ok(struct obj *); +static boolean cant_wield_corpse(struct obj *) NONNULLARG1; +static int ready_weapon(struct obj *) NO_NNARGS; +static int ready_ok(struct obj *) NO_NNARGS; +static int wield_ok(struct obj *) NO_NNARGS; /* used by will_weld() */ /* probably should be renamed */ @@ -676,7 +676,7 @@ wield_tool(struct obj *obj, const char *what; boolean more_than_1; - if (obj == uwep) + if (uwep && obj == uwep) return TRUE; /* nothing to do if already wielding it */ if (!verb) @@ -690,7 +690,7 @@ wield_tool(struct obj *obj, more_than_1 ? "them" : "it"); return FALSE; } - if (welded(uwep)) { + if (uwep && welded(uwep)) { if (flags.verbose) { const char *hand = body_part(HAND); @@ -737,7 +737,7 @@ wield_tool(struct obj *obj, if (flags.pushweapon && oldwep && uwep != oldwep) setuswapwep(oldwep); } - if (uwep != obj) + if (uwep && uwep != obj) return FALSE; /* rewielded old object after dying */ /* applying weapon or tool that gets wielded ends two-weapon combat */ if (u.twoweap)