Skip to content

Commit

Permalink
Ensure we free the row.items object only when we have actually alloca…
Browse files Browse the repository at this point in the history
…ted it (not if we are merely using a copy)

Signed-off-by: DL6ER <[email protected]>
  • Loading branch information
DL6ER committed Sep 7, 2023
1 parent 506750e commit 06922d9
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/api/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ static int api_list_write(struct ftl_conn *api,
}

bool spaces_allowed = false;
bool allocated_json = false;
if(api->method == HTTP_POST)
{
// Extract domain/name/client/address from payload when using POST, all
Expand All @@ -188,7 +189,8 @@ static int api_list_write(struct ftl_conn *api,
if(cJSON_IsString(json_domain) && strlen(json_domain->valuestring) > 0)
{
row.items = cJSON_CreateArray();
cJSON_AddItemToArray(row.items, json_domain);
cJSON_AddItemToArray(row.items, cJSON_CreateStringReference(json_domain->valuestring));
allocated_json = true;
}
else if(cJSON_IsArray(json_domain) && cJSON_GetArraySize(json_domain) > 0)
row.items = json_domain;
Expand All @@ -209,7 +211,8 @@ static int api_list_write(struct ftl_conn *api,
if(cJSON_IsString(json_name) && strlen(json_name->valuestring) > 0)
{
row.items = cJSON_CreateArray();
cJSON_AddItemToArray(row.items, json_name);
cJSON_AddItemToArray(row.items, cJSON_CreateStringReference(json_name->valuestring));
allocated_json = true;
}
else if(cJSON_IsArray(json_name) && cJSON_GetArraySize(json_name) > 0)
row.items = json_name;
Expand All @@ -229,7 +232,8 @@ static int api_list_write(struct ftl_conn *api,
if(cJSON_IsString(json_client) && strlen(json_client->valuestring) > 0)
{
row.items = cJSON_CreateArray();
cJSON_AddItemToArray(row.items, json_client);
cJSON_AddItemToArray(row.items, cJSON_CreateStringReference(json_client->valuestring));
allocated_json = true;
}
else if(cJSON_IsArray(json_client) && cJSON_GetArraySize(json_client) > 0)
row.items = json_client;
Expand All @@ -249,7 +253,8 @@ static int api_list_write(struct ftl_conn *api,
if(cJSON_IsString(json_address) && strlen(json_address->valuestring) > 0)
{
row.items = cJSON_CreateArray();
cJSON_AddItemToArray(row.items, json_address);
cJSON_AddItemToArray(row.items, cJSON_CreateStringReference(json_address->valuestring));
allocated_json = true;
}
else if(cJSON_IsArray(json_address) && cJSON_GetArraySize(json_address) > 0)
row.items = json_address;
Expand Down Expand Up @@ -281,6 +286,7 @@ static int api_list_write(struct ftl_conn *api,
// PUT = Use URI item
row.items = cJSON_CreateArray();
cJSON_AddItemToArray(row.items, cJSON_CreateStringReference(item));
allocated_json = true;
}

cJSON *json_comment = cJSON_GetObjectItemCaseSensitive(api->payload.json, "comment");
Expand Down Expand Up @@ -424,7 +430,8 @@ static int api_list_write(struct ftl_conn *api,
const int ret = api_list_read(api, response_code, listtype, row.item);

// Free allocated memory
cJSON_free(row.items);
if(allocated_json)
cJSON_free(row.items);

return ret;
}
Expand Down

0 comments on commit 06922d9

Please sign in to comment.