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

Commit 4d61b68

Browse files
author
HYChang
committed
fix(endpoint): Fix memory leak when http_open failed
Fix memory leak on http_open. The http_open will allocate the ssl object and the net connect objects. But never free them when the connection failed. Close #661
1 parent 1608b5d commit 4d61b68

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

utils/connectivity/conn_http.c

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,31 @@ void mbedtls_debug(void *ctx, int level, const char *file, int line, const char
2727
fflush((FILE *)ctx);
2828
}
2929

30+
static status_t free_info_context(connect_info_t *const info) {
31+
if (info->https) {
32+
mbedtls_ssl_close_notify(info->ssl_ctx);
33+
mbedtls_net_free(info->net_ctx);
34+
mbedtls_entropy_free(info->entropy);
35+
mbedtls_ctr_drbg_free(info->ctr_drbg);
36+
mbedtls_ssl_free(info->ssl_ctx);
37+
mbedtls_ssl_config_free(info->ssl_config);
38+
mbedtls_x509_crt_free(info->cacert);
39+
free(info->net_ctx);
40+
free(info->ssl_ctx);
41+
free(info->ssl_config);
42+
free(info->ctr_drbg);
43+
free(info->entropy);
44+
free(info->cacert);
45+
} else {
46+
mbedtls_net_free(info->net_ctx);
47+
free(info->net_ctx);
48+
}
49+
return SC_OK;
50+
}
51+
3052
status_t http_open(connect_info_t *const info, char const *const seed_nonce, char const *const host,
3153
char const *const port) {
32-
int ret;
54+
int ret = SC_OK;
3355

3456
if (info->https) {
3557
info->net_ctx = (mbedtls_net_context *)malloc(sizeof(mbedtls_net_context));
@@ -49,33 +71,31 @@ status_t http_open(connect_info_t *const info, char const *const seed_nonce, cha
4971
ret = mbedtls_ctr_drbg_seed(info->ctr_drbg, mbedtls_entropy_func, info->entropy, (const unsigned char *)seed_nonce,
5072
strlen(seed_nonce));
5173
if (ret != 0) {
52-
free(info->net_ctx);
53-
free(info->ssl_ctx);
54-
free(info->ssl_config);
55-
free(info->ctr_drbg);
56-
free(info->entropy);
57-
free(info->cacert);
58-
return SC_UTILS_HTTPS_INIT_ERROR;
74+
ret = SC_UTILS_HTTPS_INIT_ERROR;
75+
goto exit;
5976
}
6077

6178
ret = mbedtls_x509_crt_parse(info->cacert, ca_crt_pem, ca_crt_pem_len);
6279
if (ret < 0) {
6380
printf("error: mbedtls_x509_crt_parse returned -0x%x\n\n", -ret);
64-
return SC_UTILS_HTTPS_X509_ERROR;
81+
ret = SC_UTILS_HTTPS_X509_ERROR;
82+
goto exit;
6583
}
6684
}
6785

6886
ret = mbedtls_net_connect(info->net_ctx, host, port, MBEDTLS_NET_PROTO_TCP);
6987
if (ret != 0) {
7088
printf("error: mbedtls_net_connect returned %d\n\n", ret);
71-
return SC_UTILS_HTTPS_CONN_ERROR;
89+
ret = SC_UTILS_HTTPS_CONN_ERROR;
90+
goto exit;
7291
}
7392

7493
ret = mbedtls_ssl_config_defaults(info->ssl_config, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
7594
MBEDTLS_SSL_PRESET_DEFAULT);
7695
if (ret != 0) {
7796
printf("error: mbedtls_ssl_config_defaults returned %d\n\n", ret);
78-
return SC_UTILS_HTTPS_SSL_ERROR;
97+
ret = SC_UTILS_HTTPS_SSL_ERROR;
98+
goto exit;
7999
}
80100

81101
mbedtls_ssl_conf_ca_chain(info->ssl_config, info->cacert, NULL);
@@ -87,13 +107,15 @@ status_t http_open(connect_info_t *const info, char const *const seed_nonce, cha
87107
ret = mbedtls_ssl_setup(info->ssl_ctx, info->ssl_config);
88108
if (ret != 0) {
89109
printf("error: mbedtls_ssl_setup returned %d\n\n", ret);
90-
return SC_UTILS_HTTPS_SSL_ERROR;
110+
ret = SC_UTILS_HTTPS_SSL_ERROR;
111+
goto exit;
91112
}
92113

93114
ret = mbedtls_ssl_set_hostname(info->ssl_ctx, host);
94115
if (ret != 0) {
95116
printf("error: mbedtls_ssl_set_hostname returned %d\n\n", ret);
96-
return SC_UTILS_HTTPS_SSL_ERROR;
117+
ret = SC_UTILS_HTTPS_SSL_ERROR;
118+
goto exit;
97119
}
98120

99121
// Here is Blocking mode
@@ -104,7 +126,8 @@ status_t http_open(connect_info_t *const info, char const *const seed_nonce, cha
104126
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
105127
printf("error: mbedtls_ssl_handshake returned -0x%x\n\n", -ret);
106128
mbedtls_ssl_session_reset(info->ssl_ctx);
107-
return SC_UTILS_HTTPS_SSL_ERROR;
129+
ret = SC_UTILS_HTTPS_SSL_ERROR;
130+
goto exit;
108131
}
109132
}
110133

@@ -116,7 +139,10 @@ status_t http_open(connect_info_t *const info, char const *const seed_nonce, cha
116139
mbedtls_x509_crt_verify_info(vrfy_buf, sizeof(vrfy_buf), "", flags);
117140
printf("error: %s\n", vrfy_buf);
118141
}
119-
return SC_OK;
142+
143+
exit:
144+
free_info_context(info);
145+
return ret;
120146
}
121147

122148
status_t http_send_request(connect_info_t *const info, const char *req) {
@@ -166,26 +192,7 @@ status_t http_read_response(connect_info_t *const info, char *res, size_t res_le
166192
}
167193

168194
status_t http_close(connect_info_t *const info) {
169-
if (info->https) {
170-
mbedtls_ssl_close_notify(info->ssl_ctx);
171-
172-
mbedtls_net_free(info->net_ctx);
173-
mbedtls_entropy_free(info->entropy);
174-
mbedtls_ctr_drbg_free(info->ctr_drbg);
175-
mbedtls_ssl_free(info->ssl_ctx);
176-
mbedtls_ssl_config_free(info->ssl_config);
177-
mbedtls_x509_crt_free(info->cacert);
178-
free(info->net_ctx);
179-
free(info->ssl_ctx);
180-
free(info->ssl_config);
181-
free(info->ctr_drbg);
182-
free(info->entropy);
183-
free(info->cacert);
184-
} else {
185-
mbedtls_net_free(info->net_ctx);
186-
free(info->net_ctx);
187-
}
188-
195+
free_info_context(info);
189196
return SC_OK;
190197
}
191198

0 commit comments

Comments
 (0)