Skip to content

Commit 871d7e7

Browse files
authored
Merge pull request #7730 from davidsaada/david_stack_stats_fail_fix
When stack stats enabled, prevent exceptions if memory allocations fail
2 parents 05b2d6e + cfe7df2 commit 871d7e7

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

features/frameworks/greentea-client/source/greentea_metrics.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ MBED_UNUSED static void send_stack_info()
107107

108108
// Print info for all other threads
109109
uint32_t thread_n = osThreadGetCount();
110-
osThreadId_t *threads = new osThreadId_t[thread_n];
110+
osThreadId_t *threads = new (std::nothrow) osThreadId_t[thread_n];
111+
// Don't fail on lack of memory
112+
if (!threads) {
113+
goto end;
114+
}
111115
thread_n = osThreadEnumerate(threads, thread_n);
112116

113117
for(size_t i = 0; i < thread_n; i++) {
@@ -117,6 +121,7 @@ MBED_UNUSED static void send_stack_info()
117121

118122
delete[] threads;
119123

124+
end:
120125
mutex->unlock();
121126
}
122127

platform/mbed_stats.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats)
4141
osThreadId_t *threads;
4242

4343
threads = malloc(sizeof(osThreadId_t) * thread_n);
44-
MBED_ASSERT(threads != NULL);
44+
// Don't fail on lack of memory
45+
if (!threads) {
46+
return;
47+
}
4548

4649
osKernelLock();
4750
thread_n = osThreadEnumerate(threads, thread_n);
@@ -69,7 +72,10 @@ size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
6972
osThreadId_t *threads;
7073

7174
threads = malloc(sizeof(osThreadId_t) * count);
72-
MBED_ASSERT(threads != NULL);
75+
// Don't fail on lack of memory
76+
if (!threads) {
77+
return 0;
78+
}
7379

7480
osKernelLock();
7581
count = osThreadEnumerate(threads, count);

0 commit comments

Comments
 (0)