[LTP] tst_assert: Use 'long long' to fix 32bit compat mode truncation
Petr Vorel
pvorel@suse.cz
Fri Sep 25 08:55:05 CEST 2026
Hi all,
> Hi Andrea,
> On Thu, 24 Sep 2026 13:25:12 +0200, Andrea Cervesato wrote:
> > [PATCH 1/3] tst_assert: Use 'long long' to fix 32bit compat mode truncation
> --- [PATCH 1/3] ---
> > @@ -25,16 +25,17 @@ void tst_assert_int(const char *file, const int lineno, const char *path, int va
> > void tst_assert_ulong(const char *file, const int lineno, const char *path, unsigned long val)
> > {
> > - unsigned long sys_val;
> > + unsigned long long sys_val;
> > - safe_file_scanf(file, lineno, NULL, path, "%lu", &sys_val);
> > + safe_file_scanf(file, lineno, NULL, path, "%llu", &sys_val);
> > - if (val == sys_val) {
> > + if (val == (unsigned long)sys_val) {
> > tst_res_(file, lineno, TPASS, "%s = %lu", path, val);
> > return;
> > }
> > - tst_res_(file, lineno, TFAIL, "%s != %lu got %lu", path, val, sys_val);
> > + tst_res_(file, lineno, TFAIL, "%s != %lu got %lu",
> > + path, val, (unsigned long)sys_val);
> > }
> Casting sys_val to (unsigned long) truncates values exceeding ULONG_MAX on
> 32-bit systems, causing false passes when (sys_val & 0xFFFFFFFF) == val and
> printing a truncated value on failure. Compare values without truncation
> using (unsigned long long)val == sys_val, and print sys_val with %llu on
> mismatch. Handling of 32-bit compat truncation for /proc/sys/kernel/shmall
> should be done in shmctl03.c instead of altering generic library assertions.
I wonder if it's safe to ignore this.
> --- [PATCH 2/3] ---
> > @@ -30,7 +30,14 @@ static void verify_ipcinfo(void)
> > else
> > tst_res(TPASS, "shmmin = 1");
> > - TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
> > + if (tst_is_compat_mode() && info.shmmax == INT_MAX) {
> > + unsigned long long shmmax;
> > + SAFE_FILE_SCANF(PATH_KERN_SHMMAX, "%llu", &shmmax);
> > + TST_EXP_LE_LU(INT_MAX, shmmax);
> > + } else {
> > + TST_ASSERT_ULONG(PATH_KERN_SHMMAX, info.shmmax);
> > + }
> > +
> Branching on info.shmmax == INT_MAX conditions on the result of the syscall
> under test, and TST_EXP_LE_LU(INT_MAX, shmmax) only asserts that the sysctl
> file is >= INT_MAX rather than verifying that info.shmmax was clamped.
I wonder if it's really important to verify it was clamped.
> Read PATH_KERN_SHMMAX into shmmax first, and if in compat mode with
> shmmax >= INT_MAX, assert TST_EXP_EQ_LU(info.shmmax, (unsigned long)INT_MAX);
> otherwise assert info.shmmax == shmmax.
OK, reading it twice is not optimal.
Kind regards,
Petr
More information about the ltp
mailing list