diff mbox series

ubus/lua: avoid changing key type to string

Message ID 20210831103931.6671-1-adrian.frances@netduma.com
State New
Headers show
Series ubus/lua: avoid changing key type to string | expand

Commit Message

adrian.frances@netduma.com Aug. 31, 2021, 10:39 a.m. UTC
From: Adrian Frances <adrian.frances@netduma.com>

According to the lua man page for lua_next, it is not
advised to call lua_tolstring when iterating through a
table with lua_next.

When iterating a table in ubus_lua_format_blob_array,
we end up calling lua_tostring, which inside calls
lua_tolstring, converting all non-string keys to a key
format, even for the caller.

This patch fixes the issue by restoring the original key

Signed-off-by: Adrian Frances <adrian.frances@netduma.com>
---
 lua/ubus.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/lua/ubus.c b/lua/ubus.c
index e2bb081..d949aea 100644
--- a/lua/ubus.c
+++ b/lua/ubus.c
@@ -184,7 +184,9 @@  ubus_lua_format_blob(lua_State *L, struct blob_buf *b, bool table)
 {
 	void *c;
 	bool rv = true;
-	const char *key = table ? lua_tostring(L, -2) : NULL;
+	lua_pushvalue(L,-2);
+	const char *key = table ? lua_tostring(L, -1) : NULL;
+	lua_pop(L,1);
 
 	switch (lua_type(L, -1))
 	{