From 0f257ec538242db8eccd41f92cf21a1a4c1d0f84 Mon Sep 17 00:00:00 2001 From: PatR Date: Fri, 20 Jan 2023 17:18:27 -0800 Subject: [PATCH] new static analyzer fix - nhlua.c Cope with get_nh_lua_variable() possibly returning Null. Either or both of the DISABLE_WARNING_UNREACHABLE_CODE and RESTORE_WARNING_UNREACHABLE_CODE in the vicinity looked misplaced so I took them out. They may need to be added back in. --- src/nhlua.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/nhlua.c b/src/nhlua.c index 1d4224eb2..0832ea210 100644 --- a/src/nhlua.c +++ b/src/nhlua.c @@ -1030,9 +1030,7 @@ nhl_dnum_name(lua_State *L) return 1; } -DISABLE_WARNING_UNREACHABLE_CODE - -/* set or get variables which are saved and restored along the game. +/* set or get variables which are saved and restored along with the game. nh.variable("test", 10); local ten = nh.variable("test"); */ static int @@ -1141,24 +1139,27 @@ get_nh_lua_variables(void) void save_luadata(NHFILE *nhfp) { - char *lua_data = get_nh_lua_variables(); - long lua_data_len = strlen(lua_data) + 1; + unsigned lua_data_len; + char *lua_data = get_nh_lua_variables(); /* note: '\0' terminated */ - bwrite(nhfp->fd, (genericptr_t) &lua_data_len, sizeof lua_data_len); - bwrite(nhfp->fd, (genericptr_t) lua_data, strlen(lua_data) + 1); + if (!lua_data) + lua_data = emptystr; + lua_data_len = Strlen(lua_data) + 1; /* +1: include the terminator */ + bwrite(nhfp->fd, (genericptr_t) &lua_data_len, + (unsigned) sizeof lua_data_len); + bwrite(nhfp->fd, (genericptr_t) lua_data, lua_data_len); free(lua_data); } -RESTORE_WARNING_UNREACHABLE_CODE - /* restore nh_lua_variables table from file */ void restore_luadata(NHFILE *nhfp) { - long lua_data_len; + unsigned lua_data_len; char *lua_data; - mread(nhfp->fd, (genericptr_t) &lua_data_len, sizeof lua_data_len); + mread(nhfp->fd, (genericptr_t) &lua_data_len, + (unsigned) sizeof lua_data_len); lua_data = (char *) alloc(lua_data_len); mread(nhfp->fd, (genericptr_t) lua_data, lua_data_len); if (!gl.luacore) -- 2.50.1