Skip to content

Commit 2219de9

Browse files
Darleleta-denoyelle
authored andcommitted
BUG/MINOR: hlua: fix unsafe hlua_pusherror() usage
Following previous commit's logic: hlua_pusherror() is mainly used from cleanup paths where the caller isn't protected against LJMPs. Caller was tempted to think that the function was safe because func prototype was lacking the __LJMP prefix. Let's make the function really LJMP-safe by wrapping the sensitive calls under lua_pcall(). This may be backported to all stable versions. (cherry picked from commit f0e5b82) Signed-off-by: Amaury Denoyelle <[email protected]>
1 parent 8e74276 commit 2219de9

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

src/hlua.c

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -919,16 +919,50 @@ __LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
919919

920920
/* This function pushes an error string prefixed by the file name
921921
* and the line number where the error is encountered.
922+
*
923+
* It returns 1 on success and 0 on failure (function won't LJMP)
922924
*/
925+
__LJMP static int _hlua_pusherror(lua_State *L)
926+
{
927+
const char *fmt = lua_touserdata(L, 1);
928+
va_list *argp = lua_touserdata(L, 2);
929+
930+
luaL_where(L, 2);
931+
lua_pushvfstring(L, fmt, *argp);
932+
lua_concat(L, 2);
933+
934+
return 1;
935+
}
923936
static int hlua_pusherror(lua_State *L, const char *fmt, ...)
924937
{
925938
va_list argp;
939+
int ret = 1;
940+
941+
if (!lua_checkstack(L, 3))
942+
return 0;
943+
926944
va_start(argp, fmt);
927-
luaL_where(L, 1);
928-
lua_pushvfstring(L, fmt, argp);
945+
946+
/* push our custom _hlua_pusherror() function on the stack, then
947+
* push fmt and arg list
948+
*/
949+
lua_pushcfunction(L, _hlua_pusherror);
950+
lua_pushlightuserdata(L, (void *)fmt); // 1st func argument = fmt
951+
lua_pushlightuserdata(L, &argp); // 2nd func argument = arg list
952+
953+
/* call our custom function with proper arguments using pcall() to catch
954+
* exceptions (if any)
955+
*/
956+
switch (lua_pcall(L, 2, 1, 0)) {
957+
case LUA_OK:
958+
break;
959+
default:
960+
ret = 0;
961+
}
962+
929963
va_end(argp);
930-
lua_concat(L, 2);
931-
return 1;
964+
965+
return ret;
932966
}
933967

934968
/* This functions is used with sample fetch and converters. It

0 commit comments

Comments
 (0)