From bf5b4c40e230f7fdf2d5fd025b5950f08bbbadd9 Mon Sep 17 00:00:00 2001 From: PatR Date: Fri, 15 Dec 2023 14:52:53 -0800 Subject: [PATCH] avoid 'show_transcient_light()' complaint The static analyzer complained about use of 'obj' maybe being Null when used in an impossible warning, but that warning will never appear for the case where obj is actually Null. Add an assert() that should let it figure that out, and move the impossible check inside the 'else' clause where the check matters. (Either of those by itself ought to be adequate to pacify the analyzer.) --- src/light.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/light.c b/src/light.c index 89a8d8baf..901a72aec 100644 --- a/src/light.c +++ b/src/light.c @@ -1,8 +1,9 @@ -/* NetHack 3.7 light.c $NHDT-Date: 1657918094 2022/07/15 20:48:14 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.57 $ */ +/* NetHack 3.7 light.c $NHDT-Date: 1702680171 2023/12/15 22:42:51 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.66 $ */ /* Copyright (c) Dean Luick, 1994 */ /* NetHack may be freely redistributed. See license for details. */ #include "hack.h" +#include /* * Mobile light sources. @@ -239,6 +240,9 @@ show_transient_light(struct obj *obj, coordxy x, coordxy y) cameraflash = cg.zeroany; /* radius 0 will just light ; cameraflash.a_obj is Null */ ls = new_light_core(x, y, 0, LS_OBJECT, &cameraflash); + /* pacify static analysis; 'ls' is never Null for + new_light_core(,,0,LS_OBJECT,&zeroany) */ + assert(ls != NULL); } else { /* thrown or kicked object which is emitting light; validate its light source to obtain its radius (for monster sightings) */ @@ -248,12 +252,14 @@ show_transient_light(struct obj *obj, coordxy x, coordxy y) if (ls->id.a_obj == obj) break; } - } - if (!ls || (obj && obj->where != OBJ_FREE)) { - impossible("transient light %s %s is not %s?", - obj->lamplit ? "lit" : "unlit", xname(obj), - !ls ? "a light source" : "free"); - return; + assert(obj != NULL); /* necessary condition to get into this 'else' */ + if (!ls || obj->where != OBJ_FREE) { + impossible("transient light %s %s %s not %s?", + obj->lamplit ? "lit" : "unlit", + simpleonames(obj), otense(obj, "are"), + !ls ? "a light source" : "free"); + return; + } } if (obj) /* put lit candle or lamp temporarily on the map */