lib/random.c fix undefined behavior on signed integer overflow.
PATCH v2 1/1 lib/random.c fix undefined behavior on signed integer overflow.
The random.c now expects that signed integer to overflow and triggers the ^MASK branch. But signed int overflow is undefined behavior and GCC can optimize this branch out with certain CFLAGS/LDFLAGS. And cleaning up the dead code.
Signed-off-by Lukas Herbolt <[email protected]>
--- lib/random.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)
lib/random.c +7 -27
@@ -162,38 +162,18 @@ static int32_t mt [128] = { 69069 }; -double -_random (int32_t is [2 ]) -{ -int32_t it, leh, nit; - - it = is [0 ]; - leh = is [1 ]; -if (it <=0 ) - it = (it + it) ^MASK ; -else - it = it + it; - nit = it -1 ; -/* to ensure all-ones pattern omitted */ - leh = leh * mt[nit &127 ] + nit; - is [0 ] = it; is [1 ] = leh; -if (leh <0 ) leh = ~leh; -return (SCALE * ((int32_t ) (leh |1 ))); -} - - -int32_t _irandm (int32_t is [2 ]) { -int32_t it, leh, nit; - +int32_t it, leh, nit, apply_mask; it = is [0 ]; leh = is [1 ]; -if (it <=0 ) - it = (it + it) ^MASK ; -else - it = it + it; + +/* evaluate on original value - no UB here */ + apply_mask = (it <=0 ); +/* double via unsigned shift - well-defined */ + it = (unsigned )it <<1 ; +if (apply_mask) it ^=MASK ; nit = it -1 ;/* to ensure all-ones pattern omitted */ leh = leh * mt[nit &127 ] + nit;
Signature · 1 line
2.56.0
Quoted 7 lines · Lukas Herbolt
The
random.cnow expects that signed integer to overflow and triggers the ^MASK branch. But signed int overflow is undefined behavior and GCC can optimize this branch out with certain CFLAGS/LDFLAGS. And cleaning up the dead code.Signed-off-by Lukas Herbolt <[email protected]>---
V2 looks good to me,
Reviewed-by Zorro Lang <[email protected]>
Quoted 56 lines
lib/random.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-)lib/random.c +7
-27@@ -162,38 +162,18 @@ static int32_t mt [128] = { 69069 }; -double -_random (int32_t is [2 ]) -{ -int32_t it, leh, nit; - - it = is [0 ]; - leh = is [1 ]; -if (it <=0 ) - it = (it + it) ^MASK ; -else - it = it + it; - nit = it -1 ; -/* to ensure all-ones pattern omitted */ - leh = leh * mt[nit &127 ] + nit; - is [0 ] = it; is [1 ] = leh; -if (leh <0 ) leh = ~leh; -return (SCALE * ((int32_t ) (leh |1 ))); -} - - -int32_t _irandm (int32_t is [2 ]) { -int32_t it, leh, nit; - +int32_t it, leh, nit, apply_mask; it = is [0 ]; leh = is [1 ]; -if (it <=0 ) - it = (it + it) ^MASK ; -else - it = it + it; + +/* evaluate on original value - no UB here */ + apply_mask = (it <=0 ); +/* double via unsigned shift - well-defined */ + it = (unsigned )it <<1 ; +if (apply_mask) it ^=MASK ; nit = it -1 ;/* to ensure all-ones pattern omitted */ leh = leh * mt[nit &127 ] + nit;Signature · 1 line
2.56.0