|
16 | 16 | */
|
17 | 17 |
|
18 | 18 | #include "netsocket/TLSSocketWrapper.h"
|
| 19 | +#include <new> |
19 | 20 | #include "platform/Callback.h"
|
20 | 21 | #include "drivers/Timer.h"
|
21 | 22 | #include "events/mbed_events.h"
|
@@ -134,7 +135,10 @@ nsapi_error_t TLSSocketWrapper::set_client_cert_key(const void *client_cert, siz
|
134 | 135 | #else
|
135 | 136 |
|
136 | 137 | int ret;
|
137 |
| - mbedtls_x509_crt *crt = new mbedtls_x509_crt; |
| 138 | + mbedtls_x509_crt *crt = new (std::nothrow) mbedtls_x509_crt; |
| 139 | + if (!crt) { |
| 140 | + return NSAPI_ERROR_NO_MEMORY; |
| 141 | + } |
138 | 142 | mbedtls_x509_crt_init(crt);
|
139 | 143 | if ((ret = mbedtls_x509_crt_parse(crt, static_cast<const unsigned char *>(client_cert),
|
140 | 144 | client_cert_len)) != 0) {
|
@@ -286,7 +290,11 @@ nsapi_error_t TLSSocketWrapper::continue_handshake()
|
286 | 290 | #if defined(MBEDTLS_X509_CRT_PARSE_C) && defined(FEA_TRACE_SUPPORT) && !defined(MBEDTLS_X509_REMOVE_INFO)
|
287 | 291 | /* Prints the server certificate and verify it. */
|
288 | 292 | const size_t buf_size = 1024;
|
289 |
| - char *buf = new char[buf_size]; |
| 293 | + char *buf = new (std::nothrow) char[buf_size]; |
| 294 | + if (!buf) { |
| 295 | + print_mbedtls_error("new (std::nothrow) char[buf_size] failed in continue_handshake", NSAPI_ERROR_NO_MEMORY); |
| 296 | + return NSAPI_ERROR_NO_MEMORY; |
| 297 | + } |
290 | 298 | mbedtls_x509_crt_info(buf, buf_size, "\r ",
|
291 | 299 | mbedtls_ssl_get_peer_cert(&_ssl));
|
292 | 300 | tr_debug("Server certificate:\r\n%s\r\n", buf);
|
@@ -427,10 +435,9 @@ void TLSSocketWrapper::print_mbedtls_error(MBED_UNUSED const char *name, MBED_UN
|
427 | 435 | {
|
428 | 436 | // Avoid pulling in mbedtls_strerror when trace is not enabled
|
429 | 437 | #if defined FEA_TRACE_SUPPORT && defined MBEDTLS_ERROR_C
|
430 |
| - char *buf = new char[128]; |
| 438 | + char buf[128]; |
431 | 439 | mbedtls_strerror(err, buf, 128);
|
432 | 440 | tr_err("%s() failed: -0x%04x (%d): %s", name, -err, err, buf);
|
433 |
| - delete[] buf; |
434 | 441 | #else
|
435 | 442 | tr_err("%s() failed: -0x%04x (%d)", name, -err, err);
|
436 | 443 | #endif
|
@@ -569,7 +576,10 @@ mbedtls_ssl_config *TLSSocketWrapper::get_ssl_config()
|
569 | 576 | {
|
570 | 577 | if (!_ssl_conf) {
|
571 | 578 | int ret;
|
572 |
| - _ssl_conf = new mbedtls_ssl_config; |
| 579 | + _ssl_conf = new (std::nothrow) mbedtls_ssl_config; |
| 580 | + if (!_ssl_conf) { |
| 581 | + return nullptr; |
| 582 | + } |
573 | 583 | mbedtls_ssl_config_init(_ssl_conf);
|
574 | 584 | _ssl_conf_allocated = true;
|
575 | 585 |
|
|
0 commit comments