]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon/engine: safer json packing with mixed table
authorMarek Vavrusa <marek@vavrusa.com>
Wed, 8 Jun 2016 07:06:55 +0000 (00:06 -0700)
committerMarek Vavrusa <marek@vavrusa.com>
Wed, 6 Jul 2016 06:33:38 +0000 (23:33 -0700)
daemon/engine.c

index f9c6d4489ccbda6d080b3aeb0295f6761c981c56..3a6251cd07a0d91f8f16b9348a982923f52f17cf 100644 (file)
@@ -289,19 +289,24 @@ static JsonNode *l_pack_elem(lua_State *L, int top)
        JsonNode *node = NULL;
        lua_pushnil(L);
        while(lua_next(L, top) != 0) {
-               const bool is_array = lua_isnumber(L, top + 1);
+               bool is_array = false;
                if (!node) {
+                       is_array = lua_isnumber(L, top + 1);
                        node = is_array ? json_mkarray() : json_mkobject();
                        if (!node) {
                                return NULL;
                        }
+               } else {
+                       is_array = node->tag == JSON_ARRAY;
                }
+
                /* Insert to array/table. */
                JsonNode *val = l_pack_elem(L, top + 2);
                if (is_array) {
                        json_append_element(node, val);
                } else {
-                       json_append_member(node, lua_tostring(L, top + 1), val);
+                       const char *key = lua_tostring(L, top + 1);
+                       json_append_member(node, key, val);
                }
                lua_pop(L, 1);
        }
@@ -323,7 +328,7 @@ static char *l_pack_json(lua_State *L, int top)
 
 static int l_tojson(lua_State *L)
 {
-       auto_free char *json_str = l_pack_json(L, 1);
+       auto_free char *json_str = l_pack_json(L, lua_gettop(L));
        if (!json_str) {
                return 0;
        }