From: Lukas Herbolt <lukas@herbolt.com>
To: zlang@kernel.org
Cc: fstests@vger.kernel.org, linux-xfs@vger.kernel.org,
linux-ext4@vger.kernel.org, Lukas Herbolt <lukas@herbolt.com>
Subject: [PATCH 0/1] Fix random.c UB on int overflow
Date: Tue, 6 Oct 2026 15:51:51 +0200 [thread overview]
Message-ID: <20261006135205.400485-1-lukas@herbolt.com> (raw)
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 removes
This 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/007
The 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/188
and 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(-)
--
2.55.0
next reply other threads:[~2026-10-06 13:52 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-06 13:51 Lukas Herbolt [this message]
2026-10-06 13:51 ` [PATCH 1/1] lib/random.c fix undefined behavior on signed integer overflow Lukas Herbolt
2026-10-07 17:00 ` Darrick J. Wong
2026-10-08 8:23 ` Lukas Herbolt
2026-10-08 15:17 ` Darrick J. Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261006135205.400485-1-lukas@herbolt.com \
--to=lukas@herbolt.com \
--cc=fstests@vger.kernel.org \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-xfs@vger.kernel.org \
--cc=zlang@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox