From 436d18dcaa1d34d9508be7242d4e0ca7398bbfe0 Mon Sep 17 00:00:00 2001 From: Jarno Lamsa Date: Thu, 3 Oct 2019 11:46:30 +0300 Subject: [PATCH] Prevent a 0-modulus If given range for a random is [0, 0), return 0. Modulus 0 is undefined behaviour. --- library/platform_util.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/library/platform_util.c b/library/platform_util.c index 6ba4112fb..9461a9c73 100644 --- a/library/platform_util.c +++ b/library/platform_util.c @@ -150,7 +150,17 @@ uint32_t mbedtls_platform_random_in_range( size_t num ) mbedtls_hardware_poll( NULL, (unsigned char *) &result, sizeof( result ), &olen ); - return( result % num ); + + if( num == 0 ) + { + result = 0; + } + else + { + result %= num; + } + + return( result ); #endif }