From e863ff24df3332c57030366bb505189a28edf1ea Mon Sep 17 00:00:00 2001 From: Li Xiaoming Date: Wed, 13 Jan 2021 19:42:37 +0800 Subject: Fix potential memory leak It is better to init jresp as NULL, and allocate new memory when it is used, this will avoid the memory leak if the function return during the process. jobj should be freed before function returns. Bug-AGL: SPEC-3584 Change-Id: I4b917f7ecd06a1800d453ec43ffeccee89d923c6 Signed-off-by: Li Xiaoming --- binding/network-api.c | 3 ++- test/agl-service-network-ctl.c | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/binding/network-api.c b/binding/network-api.c index 0206973..d48b5ff 100644 --- a/binding/network-api.c +++ b/binding/network-api.c @@ -1065,7 +1065,7 @@ static void network_subscribe_unsubscribe(afb_req_t request, gboolean unsub) { struct network_state *ns = network_get_userdata(request); - json_object *jresp = json_object_new_object(); + json_object *jresp = NULL; const char *value; afb_event_t event; int rc; @@ -1096,6 +1096,7 @@ static void network_subscribe_unsubscribe(afb_req_t request, return; } + jresp = json_object_new_object(); afb_req_success_f(request, jresp, "Network %s to event \"%s\"", !unsub ? "subscribed" : "unsubscribed", value); diff --git a/test/agl-service-network-ctl.c b/test/agl-service-network-ctl.c index 6808b8d..4be427a 100644 --- a/test/agl-service-network-ctl.c +++ b/test/agl-service-network-ctl.c @@ -405,11 +405,13 @@ bool add_property_value(json_object *jparent, const char *key, int argc, char *a span = next_span(i + 1, argc, argv); if (span < 0) { + json_object_put(jobj); fprintf(stderr, "bad nesting\n"); return NULL; } if (!add_property_value(jobj, key2, span, argv + i + 1)) { + json_object_put(jobj); fprintf(stderr, "error adding object\n"); return false; } -- cgit 1.2.3-korg