diff mbox series

[1/2] selftests:timers: posix_timers: Fix warn_unused_result in __fatal_error()

Message ID 4f3a0acd903aeee52fb71acaec1106d513a2e88b.1727191485.git.skhan@linuxfoundation.org (mailing list archive)
State Accepted
Commit ba872972002bd1e9ae89c397307a079e04ee289a
Headers show
Series timers test fix and duplicate defines cleanup | expand

Commit Message

Shuah Khan Sept. 24, 2024, 3:56 p.m. UTC
__fatal_error routine doesn't check strerror_r() return value,
which results in the following compile time warning:

posix_timers.c: In function ‘__fatal_error’:
posix_timers.c:31:9: warning: ignoring return value of ‘strerror_r’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   31 |         strerror_r(errno, buf, sizeof(buf));

Fix this by adding a check for return value and error handling appropriate
for the GNU-specific strerror_r() in use in __fatal_error(). Check if
return string is null and handle accordingly.

From Linux strerror_r() manual page:

"The GNU-specific strerror_r() returns a pointer to a string containing
the error message. This may be either a pointer to a string that the
function stores in buf, or a pointer to some (immutable) static string
(in which case buf is unused). If the function stores a string in buf,
then at most buflen bytes are stored (the string may be truncated if
buflen is too small and errnum is unknown). The string always includes
a terminating null byte."

Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
---
 tools/testing/selftests/timers/posix_timers.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Comments

Shuah Khan Sept. 25, 2024, 3:13 p.m. UTC | #1
On 9/24/24 09:56, Shuah Khan wrote:
> __fatal_error routine doesn't check strerror_r() return value,
> which results in the following compile time warning:
> 
> posix_timers.c: In function ‘__fatal_error’:
> posix_timers.c:31:9: warning: ignoring return value of ‘strerror_r’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
>     31 |         strerror_r(errno, buf, sizeof(buf));
> 
> Fix this by adding a check for return value and error handling appropriate
> for the GNU-specific strerror_r() in use in __fatal_error(). Check if
> return string is null and handle accordingly.
> 
>  From Linux strerror_r() manual page:
> 
> "The GNU-specific strerror_r() returns a pointer to a string containing
> the error message. This may be either a pointer to a string that the
> function stores in buf, or a pointer to some (immutable) static string
> (in which case buf is unused). If the function stores a string in buf,
> then at most buflen bytes are stored (the string may be truncated if
> buflen is too small and errnum is unknown). The string always includes
> a terminating null byte."
> 
> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
> ---
>   tools/testing/selftests/timers/posix_timers.c | 12 ++++++++----
>   1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c
> index 16bd49492efa..ddb1cebc844e 100644
> --- a/tools/testing/selftests/timers/posix_timers.c
> +++ b/tools/testing/selftests/timers/posix_timers.c
> @@ -26,13 +26,17 @@
>   static void __fatal_error(const char *test, const char *name, const char *what)
>   {
>   	char buf[64];
> +	char *ret_str = NULL;
>   
> -	strerror_r(errno, buf, sizeof(buf));
> +	ret_str = strerror_r(errno, buf, sizeof(buf));
>   
> -	if (name && strlen(name))
> -		ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, buf);
> +	if (name && strlen(name) && ret_str)
> +		ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, ret_str);
> +	else if (ret_str)
> +		ksft_exit_fail_msg("%s %s %s\n", test, what, ret_str);
>   	else
> -		ksft_exit_fail_msg("%s %s %s\n", test, what, buf);
> +		ksft_exit_fail_msg("%s %s\n", test, what);
> +
>   }
>   
>   #define fatal_error(name, what)	__fatal_error(__func__, name, what)

Any comments on this patch John?

thanks,
-- Shuah
John Stultz Sept. 25, 2024, 5:19 p.m. UTC | #2
On Tue, Sep 24, 2024 at 8:57 AM Shuah Khan <skhan@linuxfoundation.org> wrote:
>
> __fatal_error routine doesn't check strerror_r() return value,
> which results in the following compile time warning:
>
> posix_timers.c: In function ‘__fatal_error’:
> posix_timers.c:31:9: warning: ignoring return value of ‘strerror_r’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
>    31 |         strerror_r(errno, buf, sizeof(buf));
>
> Fix this by adding a check for return value and error handling appropriate
> for the GNU-specific strerror_r() in use in __fatal_error(). Check if
> return string is null and handle accordingly.
>
> From Linux strerror_r() manual page:
>
> "The GNU-specific strerror_r() returns a pointer to a string containing
> the error message. This may be either a pointer to a string that the
> function stores in buf, or a pointer to some (immutable) static string
> (in which case buf is unused). If the function stores a string in buf,
> then at most buflen bytes are stored (the string may be truncated if
> buflen is too small and errnum is unknown). The string always includes
> a terminating null byte."
>
> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
> ---
>  tools/testing/selftests/timers/posix_timers.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
>
> diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c
> index 16bd49492efa..ddb1cebc844e 100644
> --- a/tools/testing/selftests/timers/posix_timers.c
> +++ b/tools/testing/selftests/timers/posix_timers.c
> @@ -26,13 +26,17 @@
>  static void __fatal_error(const char *test, const char *name, const char *what)
>  {
>         char buf[64];
> +       char *ret_str = NULL;
>
> -       strerror_r(errno, buf, sizeof(buf));
> +       ret_str = strerror_r(errno, buf, sizeof(buf));
>
> -       if (name && strlen(name))
> -               ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, buf);
> +       if (name && strlen(name) && ret_str)
> +               ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, ret_str);
> +       else if (ret_str)
> +               ksft_exit_fail_msg("%s %s %s\n", test, what, ret_str);
>         else
> -               ksft_exit_fail_msg("%s %s %s\n", test, what, buf);
> +               ksft_exit_fail_msg("%s %s\n", test, what);
> +
>  }

Acked-by: John Stultz <jstultz@google.com>
diff mbox series

Patch

diff --git a/tools/testing/selftests/timers/posix_timers.c b/tools/testing/selftests/timers/posix_timers.c
index 16bd49492efa..ddb1cebc844e 100644
--- a/tools/testing/selftests/timers/posix_timers.c
+++ b/tools/testing/selftests/timers/posix_timers.c
@@ -26,13 +26,17 @@ 
 static void __fatal_error(const char *test, const char *name, const char *what)
 {
 	char buf[64];
+	char *ret_str = NULL;
 
-	strerror_r(errno, buf, sizeof(buf));
+	ret_str = strerror_r(errno, buf, sizeof(buf));
 
-	if (name && strlen(name))
-		ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, buf);
+	if (name && strlen(name) && ret_str)
+		ksft_exit_fail_msg("%s %s %s %s\n", test, name, what, ret_str);
+	else if (ret_str)
+		ksft_exit_fail_msg("%s %s %s\n", test, what, ret_str);
 	else
-		ksft_exit_fail_msg("%s %s %s\n", test, what, buf);
+		ksft_exit_fail_msg("%s %s\n", test, what);
+
 }
 
 #define fatal_error(name, what)	__fatal_error(__func__, name, what)