Skip to content

Commit 6f45e96

Browse files
committed
SocketStats: tighten up
* Avoid undefined behaviour when copying/clearing `mbed_stats_socket_t`. As it contains a `SocketAddress`, memset and memcpy should not be used. * Avoid array overrun when `mbed_stats_socket_get_each` is passed a count greater than `nsapi.socket-stats-max-count`. * Remove `const` from internal `stats_new_socket_entry` method to avoid const-losing cast.
1 parent eb10cc1 commit 6f45e96

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

features/netsocket/SocketStats.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "rtos/Kernel.h"
2222
#endif
2323

24-
#include <string.h>
2524
#include <stdlib.h>
2625

2726
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
@@ -42,28 +41,24 @@ int SocketStats::get_entry_position(const Socket *const reference_id)
4241
size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
4342
{
4443
MBED_ASSERT(stats != NULL);
45-
size_t i = 0;
46-
memset(stats, 0, count * sizeof(mbed_stats_socket_t));
44+
size_t j;
4745
_mutex->lock();
48-
for (uint32_t j = 0; j < count; j++) {
49-
if (_stats[j].reference_id) {
50-
memcpy(&stats[i], &_stats[j], sizeof(mbed_stats_socket_t));
51-
i++;
52-
}
46+
for (j = 0; j < count && j < _size; j++) {
47+
stats[j] = _stats[j];
5348
}
5449
_mutex->unlock();
55-
return i;
50+
return j;
5651
}
5752

58-
void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
53+
void SocketStats::stats_new_socket_entry(Socket *const reference_id)
5954
{
6055
_mutex->lock();
6156
if (get_entry_position(reference_id) >= 0) {
6257
// Duplicate entry
6358
MBED_WARNING1(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_INVALID_INDEX), "Duplicate socket Reference ID ", reference_id);
6459
} else if (_size < MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT) {
6560
// Add new entry
66-
_stats[_size].reference_id = (Socket *)reference_id;
61+
_stats[_size].reference_id = reference_id;
6762
_size++;
6863
} else {
6964
int position = -1;
@@ -80,8 +75,8 @@ void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
8075
if (-1 == position) {
8176
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_OUT_OF_RESOURCES), "List full with all open sockets");
8277
}
83-
memset(&_stats[position], 0, sizeof(mbed_stats_socket_t));
84-
_stats[position].reference_id = (Socket *)reference_id;
78+
_stats[position] = {};
79+
_stats[position].reference_id = reference_id;
8580
}
8681
_mutex->unlock();
8782
}

features/netsocket/SocketStats.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class SocketStats {
9393
* the socket creation count exceeds `MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT`.
9494
*
9595
*/
96-
void stats_new_socket_entry(const Socket *reference_id);
96+
void stats_new_socket_entry(Socket *reference_id);
9797

9898
/** Updates the state of the socket and records `tick_last_change`.
9999
* API used by socket (TCP or UDP) layers only, not to be used by application.
@@ -162,7 +162,7 @@ inline size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *, siz
162162
return 0;
163163
}
164164

165-
inline void SocketStats::stats_new_socket_entry(const Socket *)
165+
inline void SocketStats::stats_new_socket_entry(Socket *)
166166
{
167167
}
168168

0 commit comments

Comments
 (0)