Skip to content

Commit

Permalink
Merge pull request #7730 from davidsaada/david_stack_stats_fail_fix
Browse files Browse the repository at this point in the history
When stack stats enabled, prevent exceptions if memory allocations fail
  • Loading branch information
0xc0170 committed Aug 24, 2018
2 parents 05b2d6e + cfe7df2 commit 871d7e7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ MBED_UNUSED static void send_stack_info()

// Print info for all other threads
uint32_t thread_n = osThreadGetCount();
osThreadId_t *threads = new osThreadId_t[thread_n];
osThreadId_t *threads = new (std::nothrow) osThreadId_t[thread_n];
// Don't fail on lack of memory
if (!threads) {
goto end;
}
thread_n = osThreadEnumerate(threads, thread_n);

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

delete[] threads;

end:
mutex->unlock();
}

Expand Down
10 changes: 8 additions & 2 deletions platform/mbed_stats.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ void mbed_stats_stack_get(mbed_stats_stack_t *stats)
osThreadId_t *threads;

threads = malloc(sizeof(osThreadId_t) * thread_n);
MBED_ASSERT(threads != NULL);
// Don't fail on lack of memory
if (!threads) {
return;
}

osKernelLock();
thread_n = osThreadEnumerate(threads, thread_n);
Expand Down Expand Up @@ -69,7 +72,10 @@ size_t mbed_stats_stack_get_each(mbed_stats_stack_t *stats, size_t count)
osThreadId_t *threads;

threads = malloc(sizeof(osThreadId_t) * count);
MBED_ASSERT(threads != NULL);
// Don't fail on lack of memory
if (!threads) {
return 0;
}

osKernelLock();
count = osThreadEnumerate(threads, count);
Expand Down

0 comments on commit 871d7e7

Please sign in to comment.