akheron / jansson

C library for encoding, decoding and manipulating JSON data
http://www.digip.org/jansson/
Other
3.05k stars 808 forks source link

Possible run time memory leak with json_load_file API #614

Open Shobhit-T opened 2 years ago

Shobhit-T commented 2 years ago

Hi Team,

I'm seeing some runtime leaks (not visible in valgrind) when parsing a big json file with 10K-20K entries in array. I'm using the jansson version 2.14.

Sample code:

#include <stdio.h>
#include <unistd.h>
#include <jansson.h>

int main()
{
    json_t *full_file_cfg = NULL;
    json_error_t json_error;
    full_file_cfg = json_load_file("test.json", 0, &json_error);
    if (full_file_cfg != NULL) {
        json_decref(full_file_cfg);
        full_file_cfg = NULL;
    }   
    return 0;
}

The json file looks something like this:

{
  "id": 1,
  "ips": [
    {
      "id": 1,
      "exit_points": [
        {
          "addr_type": "ipv4",
          "address": "1.1.1.1"
        },
        {
          "addr_type": "ipv4",
          "address": "1.1.1.2"
        },
 ...... n entries
        {
          "addr_type": "ipv4",
          "address": "1.1.1.20000"
        }
      ]
    }
  ]
}

When checked with some other memory debugging tools, it reported some leaks as below.

CallStack[23]: may-leak=1000 (56086 bytes)
    expired=1000 (56086 bytes), free_expired=0 (0 bytes)
    alloc=22463 (1258015 bytes), free=0 (0 bytes)
    freed memory live time: min=0 max=0 average=0
    un-freed memory live time: max=10
    0x00007ff767c79020  libc-2.27.so  __libc_malloc()+0
    0x00007ff767fd66c9  libjansson.so
    0x00007ff767fdc219  libjansson.so  json_object_set_new_nocheck()+89
    0x00007ff767fd7cbe  libjansson.so
    0x00007ff767fd7e2e  libjansson.so
    0x00007ff767fd7ca7  libjansson.so
    0x00007ff767fd7e2e  libjansson.so
    0x00007ff767fd7ca7  libjansson.so
    0x00007ff767fd7f56  libjansson.so
    0x00007ff767fd8317  libjansson.so  json_loadf()+183
    0x00007ff767fd84d8  libjansson.so  json_load_file()+72
    0x000055c78e241870  a.out  main()+115

Thanks,

geoffreycs commented 1 year ago

I know it's been over a year, but any luck with this? I think I'm running into a similar issue in my own project, even though I'm loading the file into a buffer manually before using json_load() on it. My JSON file sucks up about 2.8 GB when parsing, and even though I free the buffer and do a json_decref() (which is working) on the JSON root once I'm done, most of the memory is still unaccounted for and I presume it's being eaten up by the load function.