From: Adhemerval Zanella Date: Wed, 23 Apr 2025 19:23:36 +0000 (-0300) Subject: linux: Fix UB on tst-sched-setattr.c X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4b23aedb1b17a24c8da74c56a7d759248bf9e332;p=thirdparty%2Fglibc.git linux: Fix UB on tst-sched-setattr.c UBSAN: Undefined behaviour in ../sysdeps/unix/sysv/linux/tst-sched_setattr.c:86:5 null pointer passed as argument 2, nonnull attribute declared at unknown:0:0 --- diff --git a/sysdeps/unix/sysv/linux/tst-sched_setattr.c b/sysdeps/unix/sysv/linux/tst-sched_setattr.c index bf8d9afd34..5698d73e0a 100644 --- a/sysdeps/unix/sysv/linux/tst-sched_setattr.c +++ b/sysdeps/unix/sysv/linux/tst-sched_setattr.c @@ -33,6 +33,20 @@ union unsigned char padding[4096]; } u; +static void +__attribute_disable_ubsan__ +check_null_argument (volatile unsigned int size) +{ + /* Invalid buffer arguments result in EINVAL (not EFAULT). */ + errno = 0; + void *volatile null_pointer = NULL; /* compiler barrier. */ + TEST_COMPARE (sched_setattr (0, null_pointer, 0), -1); + TEST_COMPARE (errno, EINVAL); + errno = 0; + TEST_COMPARE (sched_getattr (0, null_pointer, size, 0), -1); + TEST_COMPARE (errno, EINVAL); +} + static int do_test (void) { @@ -79,16 +93,7 @@ do_test (void) TEST_COMPARE (u.attr.sched_policy, SCHED_OTHER); TEST_COMPARE (u.attr.sched_nice, 19); - /* Invalid buffer arguments result in EINVAL (not EFAULT). */ - { - errno = 0; - void *volatile null_pointer = NULL; /* compiler barrier. */ - TEST_COMPARE (sched_setattr (0, null_pointer, 0), -1); - TEST_COMPARE (errno, EINVAL); - errno = 0; - TEST_COMPARE (sched_getattr (0, null_pointer, size, 0), -1); - TEST_COMPARE (errno, EINVAL); - } + check_null_argument (size); return 0; }