cJSON icon indicating copy to clipboard operation
cJSON copied to clipboard

cJSON_Parse API detected memory leaks

Open jjj19950710 opened this issue 2 years ago • 1 comments

for example:

char *buf = "{\"name\":\"liming\",\"id\":1}";
cJSON *json;  
json = cJSON_Parse(buf);
printf("json = %s\n",json);

the log is:

json = 
=================================================================
==62255==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 64 byte(s) in 1 object(s) allocated from:
    #0 0x49a7cd in malloc /root/clang-12.0.0/src/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3
    #1 0x7f640f121d47 in cJSON_New_Item /home/jbh/cJSON/cJSON.c:243:27
    #2 0x7f640f121d47 in cJSON_ParseWithLengthOpts /home/jbh/cJSON/cJSON.c:1114:12
    #3 0x7f640f12b0ac in cJSON_ParseWithOpts /home/jbh/cJSON/cJSON.c:1091:12
    #4 0x7f640f12b0ac in cJSON_Parse /home/jbh/cJSON/cJSON.c:1177:12
    #5 0x4cbb8f in cJSON_Parse_test /home/jbh/cJSON/test.c:262:9
    #6 0x4cbb8f in main /home/jbh/cJSON/test.c:281:5
    #7 0x4ccc35 in __wrap_main /home/jbh/xfuzz-sdk/driver/fast_mode/fast_mode_driver.c:288:20
    #8 0x7f640ed7e0b2 in __libc_start_main /build/glibc-sMfBJT/glibc-2.31/csu/../csu/libc-start.c:308:16

Indirect leak of 128 byte(s) in 2 object(s) allocated from:
    #0 0x49a7cd in malloc /root/clang-12.0.0/src/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3
    #1 0x7f640f12679e in cJSON_New_Item /home/jbh/cJSON/cJSON.c:243:27
    #2 0x7f640f12679e in parse_object /home/jbh/cJSON/cJSON.c:1637:27
    #3 0x7f640f12679e in parse_value /home/jbh/cJSON/cJSON.c:1360:16
    #4 0x7f640f121e83 in cJSON_ParseWithLengthOpts /home/jbh/cJSON/cJSON.c:1120:10
    #5 0x7f640f12b0ac in cJSON_ParseWithOpts /home/jbh/cJSON/cJSON.c:1091:12
    #6 0x7f640f12b0ac in cJSON_Parse /home/jbh/cJSON/cJSON.c:1177:12
    #7 0x4cbb8f in cJSON_Parse_test /home/jbh/cJSON/test.c:262:9
    #8 0x4cbb8f in main /home/jbh/cJSON/test.c:281:5
    #9 0x4ccc35 in __wrap_main /home/jbh/xfuzz-sdk/driver/fast_mode/fast_mode_driver.c:288:20
    #10 0x7f640ed7e0b2 in __libc_start_main /build/glibc-sMfBJT/glibc-2.31/csu/../csu/libc-start.c:308:16

Indirect leak of 10 byte(s) in 2 object(s) allocated from:
    #0 0x49a7cd in malloc /root/clang-12.0.0/src/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3
    #1 0x7f640f13fbea in parse_string /home/jbh/cJSON/cJSON.c:812:34
    #2 0x7f640f126938 in parse_object /home/jbh/cJSON/cJSON.c:1660:14
    #3 0x7f640f126938 in parse_value /home/jbh/cJSON/cJSON.c:1360:16
    #4 0x7f640f121e83 in cJSON_ParseWithLengthOpts /home/jbh/cJSON/cJSON.c:1120:10
    #5 0x7f640f12b0ac in cJSON_ParseWithOpts /home/jbh/cJSON/cJSON.c:1091:12
    #6 0x7f640f12b0ac in cJSON_Parse /home/jbh/cJSON/cJSON.c:1177:12
    #7 0x4cbb8f in cJSON_Parse_test /home/jbh/cJSON/test.c:262:9
    #8 0x4cbb8f in main /home/jbh/cJSON/test.c:281:5
    #9 0x4ccc35 in __wrap_main /home/jbh/xfuzz-sdk/driver/fast_mode/fast_mode_driver.c:288:20
    #10 0x7f640ed7e0b2 in __libc_start_main /build/glibc-sMfBJT/glibc-2.31/csu/../csu/libc-start.c:308:16

Indirect leak of 8 byte(s) in 1 object(s) allocated from:
    #0 0x49a7cd in malloc /root/clang-12.0.0/src/compiler-rt/lib/asan/asan_malloc_linux.cpp:145:3
    #1 0x7f640f13fbea in parse_string /home/jbh/cJSON/cJSON.c:812:34
    #2 0x7f640f123257 in parse_value /home/jbh/cJSON/cJSON.c:1345:16
    #3 0x7f640f126d0c in parse_object /home/jbh/cJSON/cJSON.c:1678:14
    #4 0x7f640f126d0c in parse_value /home/jbh/cJSON/cJSON.c:1360:16
    #5 0x7f640f121e83 in cJSON_ParseWithLengthOpts /home/jbh/cJSON/cJSON.c:1120:10
    #6 0x7f640f12b0ac in cJSON_ParseWithOpts /home/jbh/cJSON/cJSON.c:1091:12
    #7 0x7f640f12b0ac in cJSON_Parse /home/jbh/cJSON/cJSON.c:1177:12
    #8 0x4cbb8f in cJSON_Parse_test /home/jbh/cJSON/test.c:262:9
    #9 0x4cbb8f in main /home/jbh/cJSON/test.c:281:5
    #10 0x4ccc35 in __wrap_main /home/jbh/xfuzz-sdk/driver/fast_mode/fast_mode_driver.c:288:20
    #11 0x7f640ed7e0b2 in __libc_start_main /build/glibc-sMfBJT/glibc-2.31/csu/../csu/libc-start.c:308:16

SUMMARY: AddressSanitizer: 210 byte(s) leaked in 6 allocation(s).

Why?

jjj19950710 avatar Apr 08 '22 08:04 jjj19950710

Please free the memory after parsing the json.

char *buf = "{\"name\":\"liming\",\"id\":1}";
cJSON *json;  
json = cJSON_Parse(buf);
printf("json = %s\n",json);  // json is not char*, but cJSON*, so we shouldn't print it with %s

cJSON_Delete(json); // free the json

BTW, if you want to print the json object with string, you may use cJSON_Print function.

Alanscut avatar Apr 08 '22 08:04 Alanscut