From 01955792b33fa4e4fd8829d9403956f94b2fdceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Thu, 9 Apr 2020 10:11:17 +0200 Subject: [PATCH] Fix integer overflow in benchmark program MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When building with MBEDTLS_MEMORY_DEBUG enabled, and running the ecdh part, the benchmark program would start writing a very large number of space characters on stdout, and would have to be killed because it never seemed to terminate. This was due to an integer overflow in computing how many space to leave after the title in order to get memory measurements aligned, which resulted in up to SIZE_MAX spaces being printed. This commit just fixes the overflow, the next commit is going to fix the magic number (12). Signed-off-by: Manuel Pégourié-Gonnard --- programs/test/benchmark.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/programs/test/benchmark.c b/programs/test/benchmark.c index 9c06d07b0..2df900b1a 100644 --- a/programs/test/benchmark.c +++ b/programs/test/benchmark.c @@ -163,7 +163,8 @@ do { \ #define MEMORY_MEASURE_PRINT( title_len ) \ mbedtls_memory_buffer_alloc_max_get( &max_used, &max_blocks ); \ - for( ii = 12 - (title_len); ii != 0; ii-- ) mbedtls_printf( " " ); \ + for( ii = 12 > (title_len) ? 12 - (title_len) : 1; ii !=0; ii--) \ + mbedtls_printf( " " ); \ max_used -= prv_used; \ max_blocks -= prv_blocks; \ max_bytes = max_used + MEM_BLOCK_OVERHEAD * max_blocks; \