Skip to content

Commit 922d42b

Browse files
Alex ChenMarkus Armbruster
Alex Chen
authored and
Markus Armbruster
committed
json: Fix a memleak in parse_pair()
In qobject_type(), NULL is returned when the 'QObject' returned from parse_value() is not of QString type, and this 'QObject' memory will leaked. So we need to first cache the 'QObject' returned from parse_value(), and finally free 'QObject' memory at the end of the function. Also, we add a testcast about invalid dict key. The memleak stack is as follows: Direct leak of 32 byte(s) in 1 object(s) allocated from: #0 0xfffe4b3c34fb in __interceptor_malloc (/lib64/libasan.so.4+0xd34fb) #1 0xfffe4ae48aa3 in g_malloc (/lib64/libglib-2.0.so.0+0x58aa3) #2 0xaaab3557d9f7 in qnum_from_int qemu/qobject/qnum.c:25 #3 0xaaab35584d23 in parse_literal qemu/qobject/json-parser.c:511 #4 0xaaab35584d23 in parse_value qemu/qobject/json-parser.c:554 #5 0xaaab35583d77 in parse_pair qemu/qobject/json-parser.c:270 #6 0xaaab355845db in parse_object qemu/qobject/json-parser.c:327 #7 0xaaab355845db in parse_value qemu/qobject/json-parser.c:546 #8 0xaaab35585b1b in json_parser_parse qemu/qobject/json-parser.c:580 #9 0xaaab35583703 in json_message_process_token qemu/qobject/json-streamer.c:92 #10 0xaaab355ddccf in json_lexer_feed_char qemu/qobject/json-lexer.c:313 #11 0xaaab355de0eb in json_lexer_feed qemu/qobject/json-lexer.c:350 #12 0xaaab354aff67 in tcp_chr_read qemu/chardev/char-socket.c:525 #13 0xfffe4ae429db in g_main_context_dispatch (/lib64/libglib-2.0.so.0+0x529db) #14 0xfffe4ae42d8f (/lib64/libglib-2.0.so.0+0x52d8f) #15 0xfffe4ae430df in g_main_loop_run (/lib64/libglib-2.0.so.0+0x530df) #16 0xaaab34d70bff in iothread_run qemu/iothread.c:82 #17 0xaaab3559d71b in qemu_thread_start qemu/util/qemu-thread-posix.c:519 Fixes: 532fb53 ("qapi: Make more of qobject_to()") Reported-by: Euler Robot <[email protected]> Signed-off-by: Alex Chen <[email protected]> Signed-off-by: Chen Qun <[email protected]> Signed-off-by: Markus Armbruster <[email protected]> Message-Id: <[email protected]> [Commit message tweaked]
1 parent 1c7ab09 commit 922d42b

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

qobject/json-parser.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,9 @@ static JSONToken *parser_context_peek_token(JSONParserContext *ctxt)
257257
*/
258258
static int parse_pair(JSONParserContext *ctxt, QDict *dict)
259259
{
260+
QObject *key_obj = NULL;
261+
QString *key;
260262
QObject *value;
261-
QString *key = NULL;
262263
JSONToken *peek, *token;
263264

264265
peek = parser_context_peek_token(ctxt);
@@ -267,7 +268,8 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict)
267268
goto out;
268269
}
269270

270-
key = qobject_to(QString, parse_value(ctxt));
271+
key_obj = parse_value(ctxt);
272+
key = qobject_to(QString, key_obj);
271273
if (!key) {
272274
parse_error(ctxt, peek, "key is not a string in object");
273275
goto out;
@@ -297,13 +299,11 @@ static int parse_pair(JSONParserContext *ctxt, QDict *dict)
297299

298300
qdict_put_obj(dict, qstring_get_str(key), value);
299301

300-
qobject_unref(key);
301-
302+
qobject_unref(key_obj);
302303
return 0;
303304

304305
out:
305-
qobject_unref(key);
306-
306+
qobject_unref(key_obj);
307307
return -1;
308308
}
309309

tests/check-qjson.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,14 @@ static void invalid_dict_comma(void)
14151415
g_assert(obj == NULL);
14161416
}
14171417

1418+
static void invalid_dict_key(void)
1419+
{
1420+
Error *err = NULL;
1421+
QObject *obj = qobject_from_json("{32:'abc'}", &err);
1422+
error_free_or_abort(&err);
1423+
g_assert(obj == NULL);
1424+
}
1425+
14181426
static void unterminated_literal(void)
14191427
{
14201428
Error *err = NULL;
@@ -1500,6 +1508,7 @@ int main(int argc, char **argv)
15001508
g_test_add_func("/errors/unterminated/dict_comma", unterminated_dict_comma);
15011509
g_test_add_func("/errors/invalid_array_comma", invalid_array_comma);
15021510
g_test_add_func("/errors/invalid_dict_comma", invalid_dict_comma);
1511+
g_test_add_func("/errors/invalid_dict_key", invalid_dict_key);
15031512
g_test_add_func("/errors/unterminated/literal", unterminated_literal);
15041513
g_test_add_func("/errors/limits/nesting", limits_nesting);
15051514
g_test_add_func("/errors/multiple_values", multiple_values);

0 commit comments

Comments
 (0)