Skip to content

Commit b241943

Browse files
Multiple memory handling fixes
Correct memory clean-ups in multiple tests and a stub.
1 parent 723236f commit b241943

File tree

6 files changed

+25
-3
lines changed

6 files changed

+25
-3
lines changed

UNITTESTS/features/netsocket/DTLSSocketWrapper/test_DTLSSocketWrapper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class TestDTLSSocketWrapper : public testing::Test {
5858
stack.return_socketAddress = SocketAddress();
5959
eventFlagsStubNextRetval.clear();
6060
delete wrapper;
61+
delete transport;
6162
}
6263

6364
char *cert = "-----BEGIN CERTIFICATE-----\
@@ -103,6 +104,7 @@ TEST_F(TestDTLSSocketWrapper, constructor)
103104
TEST_F(TestDTLSSocketWrapper, constructor_hostname)
104105
{
105106
DTLSSocketWrapper *wrapper2 = new DTLSSocketWrapper(transport, "localhost");
107+
delete wrapper2;
106108
}
107109

108110
/* connect */
@@ -323,6 +325,8 @@ TEST_F(TestDTLSSocketWrapper, set_root_ca_cert_invalid)
323325
mbedtls_stub.counter = 0;
324326
mbedtls_stub.retArray[0] = 1; // mbedtls_x509_crt_parse error
325327
EXPECT_EQ(wrapper->set_root_ca_cert(cert, strlen(cert)), NSAPI_ERROR_PARAMETER);
328+
// We need to deallocate the crt pointer ourselves.
329+
delete (wrapper->get_ca_chain());
326330
}
327331

328332
TEST_F(TestDTLSSocketWrapper, set_client_cert_key)

UNITTESTS/features/netsocket/NetworkInterface/test_NetworkInterface.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,6 @@ TEST_F(TestNetworkInterface, correct_event_listener_per_interface)
216216

217217
iface->remove_event_listener(my_iface_callback);
218218
iface2->remove_event_listener(my_iface_callback2);
219+
220+
delete iface2;
219221
}

UNITTESTS/features/netsocket/TCPSocket/test_TCPSocket.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,12 @@ TEST_F(TestTCPSocket, accept)
272272
nsapi_error_t error;
273273
stack.return_value = NSAPI_ERROR_OK;
274274
socket->open((NetworkStack *)&stack);
275-
EXPECT_NE(socket->accept(&error), static_cast<TCPSocket *>(NULL));
275+
TCPSocket *sock = socket->accept(&error);
276+
EXPECT_NE(sock, static_cast<TCPSocket *>(NULL));
276277
EXPECT_EQ(error, NSAPI_ERROR_OK);
278+
if (sock) {
279+
sock->close();
280+
}
277281
}
278282

279283
TEST_F(TestTCPSocket, accept_would_block)

UNITTESTS/features/netsocket/TLSSocketWrapper/test_TLSSocketWrapper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class TestTLSSocketWrapper : public testing::Test {
5656
stack.return_values.clear();
5757
eventFlagsStubNextRetval.clear();
5858
delete wrapper;
59+
delete transport;
5960
}
6061

6162
char *cert = "-----BEGIN CERTIFICATE-----\
@@ -101,6 +102,7 @@ TEST_F(TestTLSSocketWrapper, constructor)
101102
TEST_F(TestTLSSocketWrapper, constructor_hostname)
102103
{
103104
TLSSocketWrapper *wrapper2 = new TLSSocketWrapper(transport, "localhost");
105+
delete wrapper2;
104106
}
105107

106108
/* connect */

UNITTESTS/features/netsocket/WiFiAccessPoint/test_WiFiAccessPoint.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ TEST_F(TestWiFiAccessPoint, set_data)
6262
EXPECT_EQ(testAp.channel, ap1->get_channel());
6363
EXPECT_EQ(testAp.rssi, ap1->get_rssi());
6464
EXPECT_EQ(testAp.security, ap1->get_security());
65+
delete ap1;
6566
}
6667

UNITTESTS/stubs/NetworkStack_stub.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ class NetworkStackstub : public NetworkStack {
2727
nsapi_error_t return_value;
2828
SocketAddress return_socketAddress;
2929

30-
NetworkStackstub()
30+
NetworkStackstub() :
31+
return_value(0),
32+
return_socketAddress()
3133
{
32-
return_value = 0;
3334
}
3435

3536
virtual const char *get_ip_address()
@@ -57,6 +58,8 @@ class NetworkStackstub : public NetworkStack {
5758
if (return_value == NSAPI_ERROR_OK && return_values.front() == NSAPI_ERROR_OK) {
5859
// Make sure a non-NULL value is returned if error is not expected
5960
*handle = reinterpret_cast<nsapi_socket_t *>(1234);
61+
} else {
62+
*handle = NULL;
6063
}
6164
return return_value;
6265
};
@@ -84,6 +87,12 @@ class NetworkStackstub : public NetworkStack {
8487
virtual nsapi_error_t socket_accept(nsapi_socket_t server,
8588
nsapi_socket_t *handle, SocketAddress *address = 0)
8689
{
90+
if (return_value == NSAPI_ERROR_OK && return_values.front() == NSAPI_ERROR_OK) {
91+
// Make sure a non-NULL value is returned if error is not expected
92+
*handle = reinterpret_cast<nsapi_socket_t *>(1234);
93+
} else {
94+
*handle = NULL;
95+
}
8796
return return_value;
8897
};
8998
virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,

0 commit comments

Comments
 (0)