diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-11-24 16:08:55 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-11-24 16:08:55 -0300 |
commit | 842a83f09caa2ebd4bc03e0076420148ac07c808 (patch) | |
tree | 92eca69ee75b32156b14c1a9d0b5a31f16ef9118 | |
parent | 7923dbbf72da303ca1cca17efd24725668992f15 (diff) | |
download | lua-842a83f09caa2ebd4bc03e0076420148ac07c808.tar.gz lua-842a83f09caa2ebd4bc03e0076420148ac07c808.tar.bz2 lua-842a83f09caa2ebd4bc03e0076420148ac07c808.zip |
Panic functions should not raise errors
The standard panic function was using 'lua_tostring', which may raise
a memory-allocation error if error value is a number.
-rw-r--r-- | lauxlib.c | 9 | ||||
-rw-r--r-- | ltests.c | 5 | ||||
-rw-r--r-- | manual/manual.of | 4 |
3 files changed, 14 insertions, 4 deletions
@@ -1025,9 +1025,14 @@ static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { | |||
1025 | } | 1025 | } |
1026 | 1026 | ||
1027 | 1027 | ||
1028 | /* | ||
1029 | ** Standard panic funcion just prints an error message. The test | ||
1030 | ** with 'lua_type' avoids possible memory errors in 'lua_tostring'. | ||
1031 | */ | ||
1028 | static int panic (lua_State *L) { | 1032 | static int panic (lua_State *L) { |
1029 | const char *msg = lua_tostring(L, -1); | 1033 | const char *msg = (lua_type(L, -1) == LUA_TSTRING) |
1030 | if (msg == NULL) msg = "error object is not a string"; | 1034 | ? lua_tostring(L, -1) |
1035 | : "error object is not a string"; | ||
1031 | lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", | 1036 | lua_writestringerror("PANIC: unprotected error in call to Lua API (%s)\n", |
1032 | msg); | 1037 | msg); |
1033 | return 0; /* return to Lua to abort */ | 1038 | return 0; /* return to Lua to abort */ |
@@ -73,8 +73,9 @@ static void badexit (const char *fmt, const char *s1, const char *s2) { | |||
73 | 73 | ||
74 | 74 | ||
75 | static int tpanic (lua_State *L) { | 75 | static int tpanic (lua_State *L) { |
76 | const char *msg = lua_tostring(L, -1); | 76 | const char *msg = (lua_type(L, -1) == LUA_TSTRING) |
77 | if (msg == NULL) msg = "error object is not a string"; | 77 | ? lua_tostring(L, -1) |
78 | : "error object is not a string"; | ||
78 | return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", | 79 | return (badexit("PANIC: unprotected error in call to Lua API (%s)\n", |
79 | msg, NULL), | 80 | msg, NULL), |
80 | 0); /* do not return to Lua */ | 81 | 0); /* do not return to Lua */ |
diff --git a/manual/manual.of b/manual/manual.of index ad120f5e..cef3e22a 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -4486,6 +4486,10 @@ This string always has a zero (@Char{\0}) | |||
4486 | after its last character (as @N{in C}), | 4486 | after its last character (as @N{in C}), |
4487 | but can contain other zeros in its body. | 4487 | but can contain other zeros in its body. |
4488 | 4488 | ||
4489 | This function can raise memory errors only | ||
4490 | when converting a number to a string | ||
4491 | (as then it has to create a new string). | ||
4492 | |||
4489 | } | 4493 | } |
4490 | 4494 | ||
4491 | @APIEntry{lua_Number lua_tonumber (lua_State *L, int index);| | 4495 | @APIEntry{lua_Number lua_tonumber (lua_State *L, int index);| |