瀏覽代碼

Added test for NULL in string.format("%p")

ISO C states that standard library functions should not be called
with NULL arguments, unless stated otherwise. 'sprintf' does not
state otherwise, and it doesn't hurt to be on the safe side.
Roberto Ierusalimschy 5 年之前
父節點
當前提交
e0cbaa50fa
共有 1 個文件被更改,包括 2 次插入0 次删除
  1. 2 0
      lstrlib.c

+ 2 - 0
lstrlib.c

@@ -1271,6 +1271,8 @@ static int str_format (lua_State *L) {
         }
         }
         case 'p': {
         case 'p': {
           const void *p = lua_topointer(L, arg);
           const void *p = lua_topointer(L, arg);
+          if (p == NULL)
+            p = "(null)";  /* NULL not a valid parameter in ISO C 'printf' */
           nb = l_sprintf(buff, maxitem, form, p);
           nb = l_sprintf(buff, maxitem, form, p);
           break;
           break;
         }
         }