Skip to content

Commit 0b6bae2

Browse files
cursoragentmyungjoo
andcommitted
Fix JSON node leak on early return paths
- add centralized cleanup branches for error/early-return paths in _resolve_rpk_path_in_json() - ensure app_info_node is reset per loop iteration and freed exactly once - release root JSON node on out_err path to prevent leaked JsonNode allocations Signed-off-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: MyungJoo Ham <myungjoo.ham@samsung.com>
1 parent 67a1037 commit 0b6bae2

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

daemon/mlops-agent-interface.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ _resolve_rpk_path_in_json (const char *json_str)
5858

5959
if (n == 0U) {
6060
ml_loge ("No data found in the given json string.");
61-
return NULL;
61+
goto out_err;
6262
}
6363

6464
for (i = 0; i < n; ++i) {
65+
app_info_node = NULL;
66+
6567
if (array) {
6668
object = json_array_get_object_element (array, i);
6769
} else {
@@ -70,7 +72,7 @@ _resolve_rpk_path_in_json (const char *json_str)
7072

7173
if (!object) {
7274
ml_loge ("Failed to parse given json string.");
73-
return NULL;
75+
goto out_err;
7476
}
7577

7678
app_info = json_object_get_string_member (object, "app_info");
@@ -88,7 +90,6 @@ _resolve_rpk_path_in_json (const char *json_str)
8890
app_info_object = json_node_get_object (app_info_node);
8991
if (!app_info_object) {
9092
ml_loge ("Failed to get `app_info` object.");
91-
json_node_free (app_info_node);
9293
goto done;
9394
}
9495

@@ -103,7 +104,6 @@ _resolve_rpk_path_in_json (const char *json_str)
103104
if (app_get_res_control_global_resource_path (res_type,
104105
&global_resource_path) != APP_ERROR_NONE) {
105106
ml_loge ("failed to get global resource path.");
106-
json_node_free (app_info_node);
107107
goto done;
108108
}
109109

@@ -113,13 +113,25 @@ _resolve_rpk_path_in_json (const char *json_str)
113113
}
114114

115115
json_node_free (app_info_node);
116+
app_info_node = NULL;
116117
}
117118

118119
done:
120+
if (app_info_node)
121+
json_node_free (app_info_node);
122+
119123
ret_json_str = json_to_string (node, TRUE);
120124
json_node_free (node);
121125

122126
return ret_json_str;
127+
128+
out_err:
129+
if (app_info_node)
130+
json_node_free (app_info_node);
131+
if (node)
132+
json_node_free (node);
133+
134+
return NULL;
123135
}
124136
#else
125137
static char *

0 commit comments

Comments
 (0)