From d81600d9447636d7777697b494dd7ffd64e700d5 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Fri, 26 May 2023 22:11:07 +0000 Subject: [PATCH] Fix more memory leaks in json. By doing a little reference tracking, we can make sure we don't miss any JsonValues that may be floating around by the time we're done. --- TODO.txt | 3 ++- tools/src/json.c | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/TODO.txt b/TODO.txt index 02be654..87202ab 100644 --- a/TODO.txt +++ b/TODO.txt @@ -11,7 +11,8 @@ Key: Milestone: v0.3.0 ----------------- -[ ] Fix leaks in json +[x] Fix leaks in json +[ ] Debug ARM64 Debian [ ] Cytoplasm [ ] Debug OpenSSL diff --git a/tools/src/json.c b/tools/src/json.c index e31dbf2..672ebcd 100644 --- a/tools/src/json.c +++ b/tools/src/json.c @@ -47,6 +47,10 @@ query(char *select, HashMap * json) char *key; JsonValue *rootVal = JsonValueObject(json); JsonValue *val = rootVal; + Array *cleanUp = ArrayCreate(); + size_t i; + + ArrayAdd(cleanUp, rootVal); key = strtok(select, "->"); @@ -67,9 +71,11 @@ query(char *select, HashMap * json) { case JSON_ARRAY: val = JsonValueInteger(ArraySize(JsonValueAsArray(val))); + ArrayAdd(cleanUp, val); break; case JSON_STRING: val = JsonValueInteger(strlen(JsonValueAsString(val))); + ArrayAdd(cleanUp, val); break; default: val = NULL; @@ -89,6 +95,7 @@ query(char *select, HashMap * json) } val = JsonValueArray(arr); + ArrayAdd(cleanUp, val); } else if (JsonValueType(val) == JSON_STRING && StrEquals(keyName + 1, "decode")) { @@ -167,7 +174,11 @@ query(char *select, HashMap * json) StreamPutc(StreamStdout(), '\n'); } - JsonValueFree(rootVal); + for (i = 0; i < ArraySize(cleanUp); i++) + { + JsonValueFree(ArrayGet(cleanUp, i)); + } + ArrayFree(cleanUp); } static void