Prevent a 0-modulus

If given range for a random is [0, 0), return 0.
Modulus 0 is undefined behaviour.
This commit is contained in:
Jarno Lamsa 2019-10-03 11:46:30 +03:00
parent e29e8a49b8
commit 436d18dcaa

View file

@ -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
}