All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nathan Chancellor <nathan@kernel.org>
To: John Stultz <jstultz@google.com>
Cc: LKML <linux-kernel@vger.kernel.org>,
	Shuah Khan <shuah@kernel.org>,
	Anna-Maria Behnsen <anna-maria@linutronix.de>,
	Frederic Weisbecker <frederic@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Stephen Boyd <sboyd@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	Justin Stitt <justinstitt@google.com>,
	Oleg Nesterov <oleg@redhat.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Edward Liaw <edliaw@google.com>,
	Carlos Llamas <cmllamas@google.com>,
	kernel-team@android.com, linux-kselftest@vger.kernel.org
Subject: Re: [PATCH 2/3] selftests: timers: Fix uninitialized variable warning in ksft_min_kernel_version
Date: Thu, 11 Apr 2024 08:39:45 -0700	[thread overview]
Message-ID: <20240411153945.GA2507795@dev-arch.thelio-3990X> (raw)
In-Reply-To: <20240410232637.4135564-2-jstultz@google.com>

On Wed, Apr 10, 2024 at 04:26:29PM -0700, John Stultz wrote:
> Building with clang, I see the following warning:
> 
> In file included from posix_timers.c:17:
> ./../kselftest.h:398:6: warning: variable 'major' is used uninitialized whenever '||' condition is true [-Wsometimes-uninitialized]
>         if (uname(&info) || sscanf(info.release, "%u.%u.", &major, &minor) != 2)
>             ^~~~~~~~~~~~
> ./../kselftest.h:401:9: note: uninitialized use occurs here
>         return major > min_major || (major == min_major && minor >= min_minor);
>                ^~~~~
> 
> This is a bit of a red-herring as if the uname() call did fail,
> we would hit ksft_exit_fail_msg() which should exit.

Correct, although we have not really conveyed that to the compiler,
right? exit() is noreturn, which means all functions that call exit()
unconditionally are also noreturn, such as ksft_exit_fail_msg(). LLVM
will figure this out once it performs inlining and such but that happens
after clang's static analysis phase that this warning occurs in. I think
a better solution would be to add __noreturn to the functions in
tools/testing/selftests/kselftest.h that call exit(), so that the
compiler is aware of this through all pipeline phases, maybe something
like this? It resolves the wawrning for me.

diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h
index 050c5fd01840..29364c9f3332 100644
--- a/tools/testing/selftests/kselftest.h
+++ b/tools/testing/selftests/kselftest.h
@@ -83,6 +83,7 @@
 #define KSFT_XPASS 3
 #define KSFT_SKIP  4
 
+#define __noreturn       __attribute__((__noreturn__))
 #define __printf(a, b)   __attribute__((format(printf, a, b)))
 
 /* counters */
@@ -324,13 +325,13 @@ void ksft_test_result_code(int exit_code, const char *test_name,
 		break;						\
 	} } while (0)
 
-static inline int ksft_exit_pass(void)
+static inline __noreturn int ksft_exit_pass(void)
 {
 	ksft_print_cnts();
 	exit(KSFT_PASS);
 }
 
-static inline int ksft_exit_fail(void)
+static inline __noreturn int ksft_exit_fail(void)
 {
 	ksft_print_cnts();
 	exit(KSFT_FAIL);
@@ -357,7 +358,7 @@ static inline int ksft_exit_fail(void)
 		  ksft_cnt.ksft_xfail +	\
 		  ksft_cnt.ksft_xskip)
 
-static inline __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
+static inline __noreturn __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;
@@ -372,7 +373,7 @@ static inline __printf(1, 2) int ksft_exit_fail_msg(const char *msg, ...)
 	exit(KSFT_FAIL);
 }
 
-static inline void ksft_exit_fail_perror(const char *msg)
+static inline __noreturn void ksft_exit_fail_perror(const char *msg)
 {
 #ifndef NOLIBC
 	ksft_exit_fail_msg("%s: %s (%d)\n", msg, strerror(errno), errno);
@@ -385,19 +386,19 @@ static inline void ksft_exit_fail_perror(const char *msg)
 #endif
 }
 
-static inline int ksft_exit_xfail(void)
+static inline __noreturn int ksft_exit_xfail(void)
 {
 	ksft_print_cnts();
 	exit(KSFT_XFAIL);
 }
 
-static inline int ksft_exit_xpass(void)
+static inline __noreturn int ksft_exit_xpass(void)
 {
 	ksft_print_cnts();
 	exit(KSFT_XPASS);
 }
 
-static inline __printf(1, 2) int ksft_exit_skip(const char *msg, ...)
+static inline __noreturn __printf(1, 2) int ksft_exit_skip(const char *msg, ...)
 {
 	int saved_errno = errno;
 	va_list args;

  reply	other threads:[~2024-04-11 15:39 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-10 23:26 [PATCH 1/3] selftests: timers: Fix posix_timers ksft_print_msg warning John Stultz
2024-04-10 23:26 ` [PATCH 2/3] selftests: timers: Fix uninitialized variable warning in ksft_min_kernel_version John Stultz
2024-04-11 15:39   ` Nathan Chancellor [this message]
2024-04-11 18:11     ` John Stultz
2024-04-11 18:47       ` Nathan Chancellor
2024-04-10 23:26 ` [PATCH 3/3] selftests: timers: Fix abs() warning in posix_timers test John Stultz
2024-04-12 12:15   ` [tip: timers/urgent] " tip-bot2 for John Stultz
2024-04-10 23:28 ` [PATCH 1/3] selftests: timers: Fix posix_timers ksft_print_msg warning Justin Stitt
2024-04-11  9:38 ` Oleg Nesterov
2024-04-11 20:45 ` Shuah Khan
2024-04-11 20:53   ` John Stultz
2024-04-11 21:12     ` Shuah Khan
2024-04-12 12:15 ` [tip: timers/urgent] selftests: timers: Fix posix_timers ksft_print_msg() warning tip-bot2 for John Stultz

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=20240411153945.GA2507795@dev-arch.thelio-3990X \
    --to=nathan@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=anna-maria@linutronix.de \
    --cc=cmllamas@google.com \
    --cc=edliaw@google.com \
    --cc=frederic@kernel.org \
    --cc=jstultz@google.com \
    --cc=justinstitt@google.com \
    --cc=kernel-team@android.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=morbo@google.com \
    --cc=ndesaulniers@google.com \
    --cc=oleg@redhat.com \
    --cc=sboyd@kernel.org \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.