luci-lib-jsonc: Fix memory leak in stringify()
[project/luci.git] / libs / luci-lib-jsonc / src / jsonc.c
index 49cb21f..b857c97 100644 (file)
@@ -106,6 +106,7 @@ static int json_stringify(lua_State *L)
                flags |= JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED;
 
        lua_pushstring(L, json_object_to_json_string_ext(obj, flags));
+       json_object_put(obj);
        return 1;
 }
 
@@ -222,7 +223,7 @@ static int _lua_test_array(lua_State *L, int index)
 
 out:
                lua_pop(L, 2);
-               return 0;
+               return -1;
        }
 
        /* check for holes */
@@ -254,7 +255,7 @@ static struct json_object * _lua_to_json(lua_State *L, int index)
        case LUA_TTABLE:
                max = _lua_test_array(L, index);
 
-               if (max > 0)
+               if (max >= 0)
                {
                        obj = json_object_new_array();
 
@@ -286,8 +287,9 @@ static struct json_object * _lua_to_json(lua_State *L, int index)
                        lua_pushvalue(L, -2);
                        key = lua_tostring(L, -1);
 
-                       json_object_object_add(obj, key,
-                                                                  _lua_to_json(L, lua_gettop(L) - 1));
+                       if (key)
+                               json_object_object_add(obj, key,
+                                                      _lua_to_json(L, lua_gettop(L) - 1));
 
                        lua_pop(L, 2);
                }