Fix random.c UB on int overflow
cover Fix random.c UB on int overflow
While building xfstests with default rpm CFLAGS/LDFLAGS, I noticed the generic/007 fails with:
--- /opt/xfstests-dev/tests/generic/007.out 2026-10-06 14:08:02.556220624 +0200
+++ /opt/xfstests-dev/results//generic/007.out.bad 2026-10-06 14:40:01.667593670 +0200
@@ -14,9 +14,9 @@
.........................................................................
.........................................................................
....................................................
-creates: 18736 OK, 18802 EEXIST ( 37538 total, 50% EEXIST)
-removes: 18675 OK, 19927 ENOENT ( 38602 total, 51% ENOENT)
-lookups: 12000 OK, 11860 ENOENT ( 23860 total, 49% ENOENT)
-total : 49411 OK, 50589 w/error (100000 total, 50% w/error)
+creates: 18745 OK, 19086 EEXIST ( 37831 total, 50% EEXIST)
+removes: 18685 OK, 19864 ENOENT ( 38549 total, 51% ENOENT)
+lookups: 12065 OK, 11555 ENOENT ( 23620 total, 48% ENOENT)
+total : 49495 OK, 50505 w/error (100000 total, 50% w/error)
-cleanup: 61 removes
+cleanup: 60 removesThis happens because because the lib/random.c has optimized out the negative branch check as overflow on signed int is undefined behavior.
if (it <= 0)
it = (it + it) ^ MASK;
else
it = it + it;Changing the if-else to single if based on the original it value prevents the compiler from optimizing out the code.
int apply_mask = (it <= 0);
it = (unsigned)it << 1;
if (apply_mask)
it ^= MASK;Steps to reproduce:
dnf install redhat-rpm-config -y
./configure CFLAGS="$(rpm --eval '%{optflags}')" \
LDFLAGS="$(rpm --eval '%{__global_ldflags}')"
./check generic/007The random.c is used in:
ext4/052
generic/007
generic/010
generic/011
generic/014
generic/094
generic/225
generic/311
generic/676
xfs/008
xfs/188and I did not noticed any regression with this change and the only one failing with the original implementation is the generic/007.
Lukas Herbolt (1):
lib/random.c fix undefined behavior on signed integer overflow.
lib/random.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
Signature · 1 line
2.55.0
PATCH 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.
--- lib/random.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
lib/random.c +7 -6
@@ -186,14 +186,15 @@ _random (int32_t is [2])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.55.0
Re: [PATCH 1/1] lib/random.c fix undefined behavior on signed integer overflow.
Quoted 23 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.Signed-off-by Lukas Herbolt <[email protected]>--- lib/random.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)lib/random.c +1
-2@@ -186,14 +186,15 @@ _random (int32_t is [2])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 ];
lib/random.c _random (int32_t is [2]) · line 197
Quoted 10 lines
lib/random.c +6
-4-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 ;
Doesn't _random suffer the same flaw?
Oh. It's dead code, maybe it should go away?
Also, what about converting all the variables to unsigned?
--D
Quoted 7 lines
lib/random.c +0
-0nit = it -1 ;/* to ensure all-ones pattern omitted */ leh = leh * mt[nit &127 ] + nit;Signature · 1 line
2.55.0
Re: [PATCH 1/1] lib/random.c fix undefined behavior on signed integer overflow.
Quoted 37 lines · Darrick J. Wong
Quoted 23 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.Signed-off-by Lukas Herbolt <[email protected]>--- lib/random.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)lib/random.c +1
-2@@ -186,14 +186,15 @@ _random (int32_t is [2])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 ];lib/random.c _random (int32_t is [2]) · line 197
Quoted 10 lines
lib/random.c +6
-4-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 ;Doesn't
_randomsuffer the same flaw?
Quoted 1 line
Oh. It's dead code, maybe it should go away?
Good point.
Quoted 2 lines
Also, what about converting all the variables to unsigned?
It would break two signed checks: it <= 0 (LFSR feedback - only fires at zero instead of when MSB set) and leh < 0 (output folding — never fires). The it register degenerates to 33 repeating values after 64 calls. Reducing the pool of random numbers.
Signature · 1 line
-lhe
Darrick J. Wong
Reviewed-byRe: [PATCH 1/1] lib/random.c fix undefined behavior on signed integer overflow.
Quoted 41 lines · Lukas Herbolt
Quoted 37 lines · Darrick J. Wong
Quoted 23 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.Signed-off-by Lukas Herbolt <[email protected]>--- lib/random.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)lib/random.c +1
-2@@ -186,14 +186,15 @@ _random (int32_t is [2])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 ];lib/random.c _random (int32_t is [2]) · line 197
Quoted 10 lines
lib/random.c +6
-4-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 ;Doesn't
_randomsuffer the same flaw?Quoted 1 line
Oh. It's dead code, maybe it should go away?
Good point.
Quoted 1 line
Quoted 7 lines
Quoted 1 line
Also, what about converting all the variables to unsigned?
It would break two signed checks:
it <= 0(LFSR feedback - only fires at zero instead of when MSB set) andleh < 0(output folding — never fires). Theitregister degenerates to 33 repeating values after 64 calls. Reducing the pool of random numbers.
Ah. Well in that case I'm satisfied, so
--D
Re: [PATCH 1/1] lib/random.c fix undefined behavior on signed integer overflow.
In reply to Lukas HerboltQuoted 38 lines · Lukas Herbolt
Quoted 37 lines · Darrick J. Wong
Quoted 23 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.Signed-off-by Lukas Herbolt <[email protected]>--- lib/random.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)lib/random.c +1
-2@@ -186,14 +186,15 @@ _random (int32_t is [2])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 ];lib/random.c _random (int32_t is [2]) · line 197
Quoted 10 lines
lib/random.c +6
-4-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 ;Doesn't
_randomsuffer the same flaw?
Quoted 2 lines
Quoted 1 line
Oh. It's dead code, maybe it should go away?
Good point.
If you'd like to do this in another patch, I'll merge this one at first (or I'll wait your V2).
Quoted 11 lines
Quoted 2 lines
Also, what about converting all the variables to unsigned?
It would break two signed checks:
it <= 0(LFSR feedback - only fires at zero instead of when MSB set) andleh < 0(output folding — never fires). Theitregister degenerates to 33 repeating values after 64 calls. Reducing the pool of random numbers.Signature · 1 line
-lhe
Re: [PATCH 1/1] lib/random.c fix undefined behavior on signed integer overflow.
In reply to Lukas HerboltQuoted 22 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.Signed-off-by Lukas Herbolt <[email protected]>--- lib/random.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)lib/random.c +1
-2@@ -186,14 +186,15 @@ _random (int32_t is [2])int32_t _irandm (int32_t is [2 ]) { -int32_t it, leh, nit; - +int32_t it, leh, nit, apply_mask; it = is [0 ];
lib/random.c _random (int32_t is [2]) · line 196
Quoted 10 lines
lib/random.c +5
-4leh = 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 ;
I'll explicitly change it to unsigned int when applying the patch.
lib/random.c _random (int32_t is [2]) · line 197
Quoted 1 line
lib/random.c +1
-0+if (apply_mask) it ^=MASK ;
Ahaha, this .c program is quite legacy. I'm not sure it's worth pulling in <stdint.h> and <stdbool.h> just to use *bool apply_mask* and *(uint32_t)*.
Quoted 6 lines
lib/random.c +0
-0nit = it -1 ;/* to ensure all-ones pattern omitted */ leh = leh * mt[nit &127 ] + nit;Signature · 1 line
2.55.0
Re: [PATCH 1/1] lib/random.c fix undefined behavior on signed integer overflow.
Quoted 36 lines · Zorro Lang
Quoted 22 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.Signed-off-by Lukas Herbolt <[email protected]>--- lib/random.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)lib/random.c +1
-2@@ -186,14 +186,15 @@ _random (int32_t is [2])int32_t _irandm (int32_t is [2 ]) { -int32_t it, leh, nit; - +int32_t it, leh, nit, apply_mask; it = is [0 ];lib/random.c _random (int32_t is [2]) · line 196
Quoted 10 lines
lib/random.c +5
-4leh = 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 ;I'll explicitly change it to unsigned int when applying the patch.
Quoted 7 lines
Reviewed-by Zorro Lang <[email protected]>lib/random.c _random (int32_t is [2]) · line 197
Quoted 1 line
lib/random.c +1
-0+if (apply_mask) it ^=MASK ;Ahaha, this .c program is quite legacy. I'm not sure it's worth pulling in <
stdint.h> and <stdbool.h> just to use *bool apply_mask* and *(uint32_t)*.
Yeah, I did't want to pull anything else into it.
I will send v2 cleaning up the dead code.
Signature · 1 line
-lhe
Re: [PATCH 1/1] lib/random.c fix undefined behavior on signed integer overflow.
Quoted 39 lines · Lukas Herbolt
Quoted 38 lines · Zorro Lang
Quoted 22 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.Signed-off-by Lukas Herbolt <[email protected]>--- lib/random.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)lib/random.c +1
-2@@ -186,14 +186,15 @@ _random (int32_t is [2])int32_t _irandm (int32_t is [2 ]) { -int32_t it, leh, nit; - +int32_t it, leh, nit, apply_mask; it = is [0 ];lib/random.c _random (int32_t is [2]) · line 196
Quoted 10 lines
lib/random.c +5
-4leh = 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 ;I'll explicitly change it to unsigned int when applying the patch.
Reviewed-by Zorro Lang <[email protected]>
Quoted 8 lines
Quoted 7 lines
lib/random.c _random (int32_t is [2]) · line 197
Quoted 1 line
lib/random.c +1
-0+if (apply_mask) it ^=MASK ;Ahaha, this .c program is quite legacy. I'm not sure it's worth pulling in
<stdint.h> and <stdbool.h> just to use *bool apply_mask* and *(uint32_t)*.Yeah, I did't want to pull anything else into it.
Makes sense :)
Quoted 1 line
I will send v2 cleaning up the dead code.
OK, If you plan to send a v2, I'll hold off on merging this one and wait for your next version.
Quoted 4 lines
Signature · 2 lines
-- -lhe