Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

fix(http): remove redundant memory allocated for HTTP answering string #543

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions connectivity/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ status_t set_response_content(status_t ret, char **json_result) {
case SC_MAM_NOT_FOUND:
http_ret = SC_HTTP_NOT_FOUND;
ta_log_error("%s\n", "SC_HTTP_NOT_FOUND");
*json_result = strdup(STR_HTTP_NOT_FOUND);
*json_result = STR_HTTP_NOT_FOUND;
break;
case SC_CCLIENT_JSON_KEY:
case SC_MAM_NO_PAYLOAD:
http_ret = SC_HTTP_BAD_REQUEST;
ta_log_error("%s\n", "SC_HTTP_BAD_REQUEST");
*json_result = strdup(STR_HTTP_BAD_REQUEST);
*json_result = STR_HTTP_BAD_REQUEST;
break;
default:
http_ret = SC_HTTP_INTERNAL_SERVICE_ERROR;
ta_log_error("%s\n", "SC_HTTP_INTERNAL_SERVICE_ERROR");
*json_result = strdup(STR_HTTP_INTERNAL_SERVICE_ERROR);
*json_result = STR_HTTP_INTERNAL_SERVICE_ERROR;
break;
}
return http_ret;
Expand Down
7 changes: 3 additions & 4 deletions connectivity/http/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,13 +344,13 @@ static status_t build_request(ta_http_request_t *req, const char *data, size_t s
return SC_TA_NULL;
}
if (size + req->request_len >= MAX_REQUEST_LEN) {
req->answer_string = strdup(STR_HTTP_REQUEST_SIZE_EXCEED);
req->answer_string = STR_HTTP_REQUEST_SIZE_EXCEED;
req->answer_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
return SC_HTTP_INTERNAL_SERVICE_ERROR;
}
char *request = malloc(req->request_len + size + 1);
if (request == NULL) {
req->answer_string = strdup(STR_HTTP_INTERNAL_SERVICE_ERROR);
req->answer_string = STR_HTTP_INTERNAL_SERVICE_ERROR;
req->answer_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
return SC_TA_OOM;
}
Expand Down Expand Up @@ -433,7 +433,7 @@ static int ta_http_handler(void *cls, struct MHD_Connection *connection, const c
http_req->answer_code = ta_http_process_request(api, url, http_req->request, &http_req->answer_string, options);
}
response =
MHD_create_response_from_buffer(strlen(http_req->answer_string), http_req->answer_string, MHD_RESPMEM_MUST_COPY);
MHD_create_response_from_buffer(strlen(http_req->answer_string), http_req->answer_string, MHD_RESPMEM_PERSISTENT);
// Set response header
MHD_add_response_header(response, MHD_HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN, "*");
if (options) {
Expand All @@ -453,7 +453,6 @@ static int ta_http_handler(void *cls, struct MHD_Connection *connection, const c
printf(" \"%s %s\" %d\n", method, url, http_req->answer_code);

if (http_req) {
free(http_req->answer_string);
free(http_req->request);
free(http_req);
}
Expand Down
1 change: 0 additions & 1 deletion connectivity/mqtt/duplex_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ static status_t mqtt_request_handler(mosq_config_t *cfg, char *subscribe_topic,
}

done:
free(json_result);
return ret;
}

Expand Down