]> granicus.if.org Git - nethack/commitdiff
new static analyzer fix - nhlua.c
authorPatR <rankin@nethack.org>
Sat, 21 Jan 2023 01:18:27 +0000 (17:18 -0800)
committerPatR <rankin@nethack.org>
Sat, 21 Jan 2023 01:18:27 +0000 (17:18 -0800)
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

index 1d4224eb211df8dab91c3a9afa2122fe56551b4a..0832ea210756005f74cf08b445b41f21736ea0c8 100644 (file)
@@ -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)