From 47670d1ba6b5a583a7b4940b0167a576b1a6a430 Mon Sep 17 00:00:00 2001 From: Chen Peng1 Date: Wed, 8 Sep 2021 11:12:17 +0800 Subject: [PATCH] tests: benchmarks: latency_measure: fix potential div by zero If the count is zero in the heap_malloc_free test, a div by zero would happen, so add a condition to handle this potential error. Signed-off-by: Chen Peng1 --- .../latency_measure/src/heap_malloc_free.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/benchmarks/latency_measure/src/heap_malloc_free.c b/tests/benchmarks/latency_measure/src/heap_malloc_free.c index bd73bbc165b..f21a90b79a0 100644 --- a/tests/benchmarks/latency_measure/src/heap_malloc_free.c +++ b/tests/benchmarks/latency_measure/src/heap_malloc_free.c @@ -47,8 +47,18 @@ void heap_malloc_free(void) count++; } - PRINT_STATS_AVG("Average time for heap malloc", sum_malloc, count); - PRINT_STATS_AVG("Average time for heap free", sum_free, count); + /* if count is 0, it means that there is not enough memory heap + * to do k_malloc at least once, then it's meaningless to + * calculate average time of memory allocation and free. + */ + if (count == 0) { + printk("Error: there isn't enough memory heap to do " + "k_malloc at least once, please " + "increase heap size\n"); + } else { + PRINT_STATS_AVG("Average time for heap malloc", sum_malloc, count); + PRINT_STATS_AVG("Average time for heap free", sum_free, count); + } timing_stop(); }