mirror of https://github.com/ARMmbed/mbed-os.git
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.pull/12463/head
parent
eb10cc18c5
commit
6f45e969b0
|
@ -21,7 +21,6 @@
|
|||
#include "rtos/Kernel.h"
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
|
||||
|
@ -42,20 +41,16 @@ int SocketStats::get_entry_position(const Socket *const reference_id)
|
|||
size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
|
||||
{
|
||||
MBED_ASSERT(stats != NULL);
|
||||
size_t i = 0;
|
||||
memset(stats, 0, count * sizeof(mbed_stats_socket_t));
|
||||
size_t j;
|
||||
_mutex->lock();
|
||||
for (uint32_t j = 0; j < count; j++) {
|
||||
if (_stats[j].reference_id) {
|
||||
memcpy(&stats[i], &_stats[j], sizeof(mbed_stats_socket_t));
|
||||
i++;
|
||||
}
|
||||
for (j = 0; j < count && j < _size; j++) {
|
||||
stats[j] = _stats[j];
|
||||
}
|
||||
_mutex->unlock();
|
||||
return i;
|
||||
return j;
|
||||
}
|
||||
|
||||
void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
|
||||
void SocketStats::stats_new_socket_entry(Socket *const reference_id)
|
||||
{
|
||||
_mutex->lock();
|
||||
if (get_entry_position(reference_id) >= 0) {
|
||||
|
@ -63,7 +58,7 @@ void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
|
|||
MBED_WARNING1(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_INVALID_INDEX), "Duplicate socket Reference ID ", reference_id);
|
||||
} else if (_size < MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT) {
|
||||
// Add new entry
|
||||
_stats[_size].reference_id = (Socket *)reference_id;
|
||||
_stats[_size].reference_id = reference_id;
|
||||
_size++;
|
||||
} else {
|
||||
int position = -1;
|
||||
|
@ -80,8 +75,8 @@ void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
|
|||
if (-1 == position) {
|
||||
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_NETWORK_STATS, MBED_ERROR_CODE_OUT_OF_RESOURCES), "List full with all open sockets");
|
||||
}
|
||||
memset(&_stats[position], 0, sizeof(mbed_stats_socket_t));
|
||||
_stats[position].reference_id = (Socket *)reference_id;
|
||||
_stats[position] = {};
|
||||
_stats[position].reference_id = reference_id;
|
||||
}
|
||||
_mutex->unlock();
|
||||
}
|
||||
|
|
|
@ -93,7 +93,7 @@ public:
|
|||
* the socket creation count exceeds `MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT`.
|
||||
*
|
||||
*/
|
||||
void stats_new_socket_entry(const Socket *reference_id);
|
||||
void stats_new_socket_entry(Socket *reference_id);
|
||||
|
||||
/** Updates the state of the socket and records `tick_last_change`.
|
||||
* 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
|
|||
return 0;
|
||||
}
|
||||
|
||||
inline void SocketStats::stats_new_socket_entry(const Socket *)
|
||||
inline void SocketStats::stats_new_socket_entry(Socket *)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue