From 3cfd579d370d5cb8079729cc029828f4c5363a5a Mon Sep 17 00:00:00 2001 From: nhmall Date: Wed, 13 Dec 2023 00:21:32 -0500 Subject: [PATCH] init NhRect, before passing to selection_getbounds selection_getbounds() has a check and early return. Initialization will ensure a known state if that early return were ever taken. This is an alternative approach to pr #1163. --- include/decl.h | 1 + src/decl.c | 1 + src/nhlsel.c | 14 +++++++------- src/sp_lev.c | 14 +++++++------- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/include/decl.h b/include/decl.h index c6c7ffa9d..b69a9c43d 100644 --- a/include/decl.h +++ b/include/decl.h @@ -1150,6 +1150,7 @@ struct const_globals { const struct obj zeroobj; /* used to zero out a struct obj */ const struct monst zeromonst; /* used to zero out a struct monst */ const anything zeroany; /* used to zero out union any */ + const NhRect zeroNhRect; /* used to zero out NhRect */ }; extern const struct const_globals cg; diff --git a/src/decl.c b/src/decl.c index 19ffbb53d..1eb3ece64 100644 --- a/src/decl.c +++ b/src/decl.c @@ -957,6 +957,7 @@ const struct const_globals cg = { DUMMY, /* zeroobj */ DUMMY, /* zeromonst */ DUMMY, /* zeroany */ + DUMMY, /* zeroNhRect */ }; #define ZERO(x) memset(&x, 0, sizeof(x)) diff --git a/src/nhlsel.c b/src/nhlsel.c index b4fdf5167..d627afec3 100644 --- a/src/nhlsel.c +++ b/src/nhlsel.c @@ -203,7 +203,7 @@ l_selection_numpoints(lua_State *L) struct selectionvar *sel = l_selection_check(L, 1); coordxy x, y; int ret = 0; - NhRect rect; + NhRect rect = cg.zeroNhRect; selection_getbounds(sel, &rect); @@ -281,7 +281,7 @@ l_selection_and(lua_State *L) struct selectionvar *sela = l_selection_check(L, 1); struct selectionvar *selb = l_selection_check(L, 2); struct selectionvar *selr = l_selection_push_new(L); - NhRect rect; + NhRect rect = cg.zeroNhRect; rect_bounds(sela->bounds, selb->bounds, &rect); @@ -304,7 +304,7 @@ l_selection_or(lua_State *L) struct selectionvar *sela = l_selection_check(L, 1); struct selectionvar *selb = l_selection_check(L, 2); struct selectionvar *selr = l_selection_push_new(L); - NhRect rect; + NhRect rect = cg.zeroNhRect; rect_bounds(sela->bounds, selb->bounds, &rect); @@ -328,7 +328,7 @@ l_selection_xor(lua_State *L) struct selectionvar *sela = l_selection_check(L, 1); struct selectionvar *selb = l_selection_check(L, 2); struct selectionvar *selr = l_selection_push_new(L); - NhRect rect; + NhRect rect = cg.zeroNhRect; rect_bounds(sela->bounds, selb->bounds, &rect); @@ -355,7 +355,7 @@ l_selection_sub(lua_State *L) struct selectionvar *sela = l_selection_check(L, 1); struct selectionvar *selb = l_selection_check(L, 2); struct selectionvar *selr = l_selection_push_new(L); - NhRect rect; + NhRect rect = cg.zeroNhRect; rect_bounds(sela->bounds, selb->bounds, &rect); @@ -445,7 +445,7 @@ static int l_selection_getbounds(lua_State *L) { struct selectionvar *sel = l_selection_check(L, 1); - NhRect rect; + NhRect rect = cg.zeroNhRect; selection_getbounds(sel, &rect); lua_settop(L, 0); @@ -904,7 +904,7 @@ l_selection_iterate(lua_State *L) int argc = lua_gettop(L); struct selectionvar *sel = (struct selectionvar *) 0; int x, y; - NhRect rect; + NhRect rect = cg.zeroNhRect; if (argc == 2 && lua_type(L, 2) == LUA_TFUNCTION) { sel = l_selection_check(L, 1); diff --git a/src/sp_lev.c b/src/sp_lev.c index 504cadd6e..4e96b52d9 100644 --- a/src/sp_lev.c +++ b/src/sp_lev.c @@ -4675,7 +4675,7 @@ struct selectionvar * selection_not(struct selectionvar *s) { int x, y; - NhRect tmprect; + NhRect tmprect = cg.zeroNhRect; for (x = 0; x < s->wid; x++) for (y = 0; y < s->hei; y++) @@ -4689,7 +4689,7 @@ selection_filter_mapchar(struct selectionvar *ov, xint16 typ, int lit) { int x, y; struct selectionvar *ret; - NhRect rect; + NhRect rect = cg.zeroNhRect; if (!ov) return NULL; @@ -4727,7 +4727,7 @@ selection_filter_percent( { int x, y; struct selectionvar *ret; - NhRect rect; + NhRect rect = cg.zeroNhRect; if (!ov) return NULL; @@ -4753,7 +4753,7 @@ selection_rndcoord( int idx = 0; int c; int dx, dy; - NhRect rect; + NhRect rect = cg.zeroNhRect; selection_getbounds(ov, &rect); @@ -4794,7 +4794,7 @@ selection_do_grow(struct selectionvar *ov, int dir) { coordxy x, y; struct selectionvar *tmp; - NhRect rect; + NhRect rect = cg.zeroNhRect; if (!ov) return; @@ -5222,7 +5222,7 @@ selection_iterate( genericptr_t arg) { coordxy x, y; - NhRect rect; + NhRect rect = cg.zeroNhRect; if (!ov) return; @@ -5645,7 +5645,7 @@ lspo_replace_terrain(lua_State *L) lua_Integer x1, y1, x2, y2; int chance; int tolit; - NhRect rect; + NhRect rect = cg.zeroNhRect; create_des_coder();