fixed bunch of memory leaks in json constructor
authorDenis Pronin <dannftk@yandex.ru>
Sun, 10 Apr 2022 12:41:47 +0000 (15:41 +0300)
committerDenis Pronin <dannftk@yandex.ru>
Sun, 17 Apr 2022 18:33:17 +0000 (21:33 +0300)
fixed memory leak produced by not freed string given by 'strdup' in
'json_object_add_value_string' function

Signed-off-by: Denis Pronin <dannftk@yandex.ru>
json.h

diff --git a/json.h b/json.h
index d98242638d4a186b13fa6ba2da86639454dc47b8..66bb06b1f17024b6759b585dfc5d9b9afcfa0326 100644 (file)
--- a/json.h
+++ b/json.h
@@ -81,8 +81,13 @@ static inline int json_object_add_value_string(struct json_object *obj,
        struct json_value arg = {
                .type = JSON_TYPE_STRING,
        };
+       union {
+               const char *a;
+               char *b;
+       } string;
 
-       arg.string = strdup(val ? : "");
+       string.a = val ? val : "";
+       arg.string = string.b;
        return json_object_add_value_type(obj, name, &arg);
 }