fix(esp_tls): Fixed input argument checks for public APIs

This commit is contained in:
Aditya Patwardhan 2024-01-10 11:04:23 +05:30
parent e8dee79bb5
commit 4d8436b90c

View File

@ -1,5 +1,5 @@
/* /*
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
* *
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
@ -131,14 +131,18 @@ static ssize_t tcp_write(esp_tls_t *tls, const char *data, size_t datalen)
ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t datalen) ssize_t esp_tls_conn_read(esp_tls_t *tls, void *data, size_t datalen)
{ {
if (!tls || !data) {
return -1;
}
return tls->read(tls, (char *)data, datalen); return tls->read(tls, (char *)data, datalen);
} }
ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t datalen) ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t datalen)
{ {
if (!tls || !data) {
return -1;
}
return tls->write(tls, (char *)data, datalen); return tls->write(tls, (char *)data, datalen);
} }
/** /**
@ -154,6 +158,7 @@ int esp_tls_conn_destroy(esp_tls_t *tls)
} }
esp_tls_internal_event_tracker_destroy(tls->error_handle); esp_tls_internal_event_tracker_destroy(tls->error_handle);
free(tls); free(tls);
tls = NULL;
return ret; return ret;
} }
return -1; // invalid argument return -1; // invalid argument
@ -436,10 +441,7 @@ err:
static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls) static int esp_tls_low_level_conn(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
{ {
if (!tls) {
ESP_LOGE(TAG, "empty esp_tls parameter");
return -1;
}
esp_err_t esp_ret; esp_err_t esp_ret;
/* These states are used to keep a tab on connection progress in case of non-blocking connect, /* These states are used to keep a tab on connection progress in case of non-blocking connect,
and in case of blocking connect these cases will get executed one after the other */ and in case of blocking connect these cases will get executed one after the other */
@ -532,6 +534,9 @@ esp_err_t esp_tls_plain_tcp_connect(const char *host, int hostlen, int port, con
int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls) int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
{ {
if (!cfg || !tls || !hostname || hostlen < 0) {
return -1;
}
struct timeval time = {}; struct timeval time = {};
gettimeofday(&time, NULL); gettimeofday(&time, NULL);
uint32_t start_time_ms = (time.tv_sec * 1000) + (time.tv_usec / 1000); uint32_t start_time_ms = (time.tv_sec * 1000) + (time.tv_usec / 1000);
@ -561,6 +566,9 @@ int esp_tls_conn_new_sync(const char *hostname, int hostlen, int port, const esp
*/ */
int esp_tls_conn_new_async(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls) int esp_tls_conn_new_async(const char *hostname, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
{ {
if (!cfg || !tls || !hostname || hostlen < 0) {
return -1;
}
return esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls); return esp_tls_low_level_conn(hostname, hostlen, port, cfg, tls);
} }
@ -580,6 +588,10 @@ static int get_port(const char *url, struct http_parser_url *u)
esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg) esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg)
{ {
if (!url || !cfg) {
return NULL;
}
/* Parse URI */ /* Parse URI */
struct http_parser_url u; struct http_parser_url u;
http_parser_url_init(&u); http_parser_url_init(&u);
@ -602,6 +614,10 @@ esp_tls_t *esp_tls_conn_http_new(const char *url, const esp_tls_cfg_t *cfg)
*/ */
int esp_tls_conn_http_new_sync(const char *url, const esp_tls_cfg_t *cfg, esp_tls_t *tls) int esp_tls_conn_http_new_sync(const char *url, const esp_tls_cfg_t *cfg, esp_tls_t *tls)
{ {
if (!url || !cfg || !tls) {
return -1;
}
/* Parse URI */ /* Parse URI */
struct http_parser_url u; struct http_parser_url u;
http_parser_url_init(&u); http_parser_url_init(&u);