diff mbox series

lua: BUG in LuaStackDump causes SEGV

Message ID 20200911103735.147943-1-sbabic@denx.de
State Accepted
Headers show
Series lua: BUG in LuaStackDump causes SEGV | expand

Commit Message

Stefano Babic Sept. 11, 2020, 10:37 a.m. UTC
Check if an entry on the Lua stack is really a table before trying to
traverse it.

Signed-off-by: Stefano Babic <sbabic@denx.de>
---
 corelib/lua_interface.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/corelib/lua_interface.c b/corelib/lua_interface.c
index 2025942..192563b 100644
--- a/corelib/lua_interface.c
+++ b/corelib/lua_interface.c
@@ -76,6 +76,9 @@  static void lua_dump_table(lua_State *L, char *str, struct img_type *img, const
 	/* Stack: table, ... */
 	lua_pushnil(L);
 	/* Stack: nil, table, ... */
+	if (!lua_istable(L, -2)) {
+		return;
+	}
 	while (lua_next(L, -2)) {
 		/* Stack: value, key, table, ... */
 		lua_pushvalue(L, -2);
@@ -161,7 +164,7 @@  void LUAstackDump(lua_State *L)
 				char *s;
 
 				if (asprintf(&s, "(%d) [table ]", i) != ENOMEM_ASPRINTF) {
-					lua_pushvalue(L, -1);
+					lua_pushvalue(L, i);
 					lua_dump_table(L, s, NULL, NULL);
 					lua_pop(L, 1);
 					free(s);