Skip to content

Commit f7d5bcd

Browse files
nathanchanceKAGA-KOKO
authored andcommitted
selftests: kselftest: Mark functions that unconditionally call exit() as __noreturn
After commit 6d029c2 ("selftests/timers/posix_timers: Reimplement check_timer_distribution()"), clang warns: tools/testing/selftests/timers/../kselftest.h:398:6: warning: variable 'major' is used uninitialized whenever '||' condition is true [-Wsometimes-uninitialized] 398 | if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2) | ^~~~~~~~~~~~ tools/testing/selftests/timers/../kselftest.h:401:9: note: uninitialized use occurs here 401 | return major > min_major || (major == min_major && minor >= min_minor); | ^~~~~ tools/testing/selftests/timers/../kselftest.h:398:6: note: remove the '||' if its condition is always false 398 | if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2) | ^~~~~~~~~~~~~~~ tools/testing/selftests/timers/../kselftest.h:395:20: note: initialize the variable 'major' to silence this warning 395 | unsigned int major, minor; | ^ | = 0 This is a false positive because if uname() fails, ksft_exit_fail_msg() will be called, which unconditionally calls exit(), a noreturn function. However, clang does not know that ksft_exit_fail_msg() will call exit() at the point in the pipeline that the warning is emitted because inlining has not occurred, so it assumes control flow will resume normally after ksft_exit_fail_msg() is called. Make it clear to clang that all of the functions that call exit() unconditionally in kselftest.h are noreturn transitively by marking them explicitly with '__attribute__((__noreturn__))', which clears up the warning above and any future warnings that may appear for the same reason. Fixes: 6d029c2 ("selftests/timers/posix_timers: Reimplement check_timer_distribution()") Reported-by: John Stultz <[email protected]> Signed-off-by: Nathan Chancellor <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Acked-by: Shuah Khan <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/20240411-mark-kselftest-exit-funcs-noreturn-v1-1-b027c948f586@kernel.org Closes: https://lore.kernel.org/all/[email protected]/
1 parent e4a6bce commit f7d5bcd

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

tools/testing/selftests/kselftest.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@
8080
#define KSFT_XPASS 3
8181
#define KSFT_SKIP 4
8282

83+
#ifndef __noreturn
84+
#define __noreturn __attribute__((__noreturn__))
85+
#endif
8386
#define __printf(a, b) __attribute__((format(printf, a, b)))
8487

8588
/* counters */
@@ -300,13 +303,13 @@ void ksft_test_result_code(int exit_code, const char *test_name,
300303
va_end(args);
301304
}
302305

303-
static inline int ksft_exit_pass(void)
306+
static inline __noreturn int ksft_exit_pass(void)
304307
{
305308
ksft_print_cnts();
306309
exit(KSFT_PASS);
307310
}
308311

309-
static inline int ksft_exit_fail(void)
312+
static inline __noreturn int ksft_exit_fail(void)
310313
{
311314
ksft_print_cnts();
312315
exit(KSFT_FAIL);
@@ -333,7 +336,7 @@ static inline int ksft_exit_fail(void)
333336
ksft_cnt.ksft_xfail + \
334337
ksft_cnt.ksft_xskip)
335338

336-
static inline __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
339+
static inline __noreturn __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
337340
{
338341
int saved_errno = errno;
339342
va_list args;
@@ -348,19 +351,19 @@ static inline __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
348351
exit(KSFT_FAIL);
349352
}
350353

351-
static inline int ksft_exit_xfail(void)
354+
static inline __noreturn int ksft_exit_xfail(void)
352355
{
353356
ksft_print_cnts();
354357
exit(KSFT_XFAIL);
355358
}
356359

357-
static inline int ksft_exit_xpass(void)
360+
static inline __noreturn int ksft_exit_xpass(void)
358361
{
359362
ksft_print_cnts();
360363
exit(KSFT_XPASS);
361364
}
362365

363-
static inline __printf(1, 2) int ksft_exit_skip(const char *msg, ...)
366+
static inline __noreturn __printf(1, 2) int ksft_exit_skip(const char *msg, ...)
364367
{
365368
int saved_errno = errno;
366369
va_list args;

0 commit comments

Comments
 (0)