Multiple memory handling fixes

Based on valgrind reports running on unit tests following changes were introduced:
* TLSSocketWrapper frees allocated cert buffer in case of errors from mbedtls,
* nsapi_addr has a mem_init() function, initializing all of its memory during construction.
pull/9759/head
Michal Paszta 2019-02-20 10:52:41 +01:00
parent b241943c12
commit fa6a3f5604
4 changed files with 21 additions and 0 deletions

View File

@ -21,6 +21,7 @@ using namespace mbed;
InternetSocket::InternetSocket()
: _stack(0), _socket(0), _timeout(osWaitForever),
_remote_peer(),
_readers(0), _writers(0),
_factory_allocated(false)
{

View File

@ -26,6 +26,7 @@
SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
{
mem_init();
_ip_address = NULL;
set_addr(addr);
set_port(port);
@ -33,6 +34,7 @@ SocketAddress::SocketAddress(nsapi_addr_t addr, uint16_t port)
SocketAddress::SocketAddress(const char *addr, uint16_t port)
{
mem_init();
_ip_address = NULL;
set_ip_address(addr);
set_port(port);
@ -40,6 +42,7 @@ SocketAddress::SocketAddress(const char *addr, uint16_t port)
SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_t port)
{
mem_init();
_ip_address = NULL;
set_ip_bytes(bytes, version);
set_port(port);
@ -47,11 +50,19 @@ SocketAddress::SocketAddress(const void *bytes, nsapi_version_t version, uint16_
SocketAddress::SocketAddress(const SocketAddress &addr)
{
mem_init();
_ip_address = NULL;
set_addr(addr.get_addr());
set_port(addr.get_port());
}
void SocketAddress::mem_init(void)
{
_addr.version = NSAPI_UNSPEC;
memset(_addr.bytes, 0, NSAPI_IP_BYTES);
_port = 0;
}
bool SocketAddress::set_ip_address(const char *addr)
{
delete[] _ip_address;

View File

@ -178,6 +178,9 @@ public:
private:
void _SocketAddress(NetworkStack *iface, const char *host, uint16_t port);
/** Initialize memory */
void mem_init(void);
mutable char *_ip_address;
nsapi_addr_t _addr;
uint16_t _port;

View File

@ -99,6 +99,8 @@ nsapi_error_t TLSSocketWrapper::set_root_ca_cert(const void *root_ca, size_t len
if ((ret = mbedtls_x509_crt_parse(crt, static_cast<const unsigned char *>(root_ca),
len)) != 0) {
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
mbedtls_x509_crt_free(crt);
delete crt;
return NSAPI_ERROR_PARAMETER;
}
set_ca_chain(crt);
@ -130,12 +132,16 @@ nsapi_error_t TLSSocketWrapper::set_client_cert_key(const void *client_cert, siz
if ((ret = mbedtls_x509_crt_parse(crt, static_cast<const unsigned char *>(client_cert),
client_cert_len)) != 0) {
print_mbedtls_error("mbedtls_x509_crt_parse", ret);
mbedtls_x509_crt_free(crt);
delete crt;
return NSAPI_ERROR_PARAMETER;
}
mbedtls_pk_init(&_pkctx);
if ((ret = mbedtls_pk_parse_key(&_pkctx, static_cast<const unsigned char *>(client_private_key_pem),
client_private_key_len, NULL, 0)) != 0) {
print_mbedtls_error("mbedtls_pk_parse_key", ret);
mbedtls_x509_crt_free(crt);
delete crt;
return NSAPI_ERROR_PARAMETER;
}
set_own_cert(crt);