diff mbox series

btrfs-progs: fix usage warning in common/help.c

Message ID 3ec35c42add1b57d547c9cbd90213a645c7335c9.1728459736.git.anand.jain@oracle.com (mailing list archive)
State New
Headers show
Series btrfs-progs: fix usage warning in common/help.c | expand

Commit Message

Anand Jain Oct. 11, 2024, 4:17 a.m. UTC
On systems with glibc 2.34 and 2.39, the following warning appears when
building the binary, causing concern that something has failed.

    [CC]     common/help.o
common/help.c: In function ‘usage’:
common/help.c:315:58: warning: ‘%s’ directive argument is null [-Wformat-overflow=]
  315 |                 fprintf(outf, "No short description for '%s'\n", token);
      |                                                          ^~
common/help.c:312:46: warning: ‘%s’ directive argument is null [-Wformat-overflow=]
  312 |                 fprintf(outf, "No usage for '%s'\n", token);
      |                                              ^~

The compiler warns that in some code paths, the token is `NULL`. Silence
the warning by checking if the token is `NULL`.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 common/help.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Qu Wenruo Oct. 11, 2024, 4:39 a.m. UTC | #1
在 2024/10/11 14:47, Anand Jain 写道:
> On systems with glibc 2.34 and 2.39, the following warning appears when
> building the binary, causing concern that something has failed.
>
>      [CC]     common/help.o
> common/help.c: In function ‘usage’:
> common/help.c:315:58: warning: ‘%s’ directive argument is null [-Wformat-overflow=]
>    315 |                 fprintf(outf, "No short description for '%s'\n", token);
>        |                                                          ^~
> common/help.c:312:46: warning: ‘%s’ directive argument is null [-Wformat-overflow=]
>    312 |                 fprintf(outf, "No usage for '%s'\n", token);
>        |                                              ^~
>
> The compiler warns that in some code paths, the token is `NULL`. Silence
> the warning by checking if the token is `NULL`.
>
> Signed-off-by: Anand Jain <anand.jain@oracle.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

Just a small improvement that I will do when merge.
> ---
>   common/help.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/common/help.c b/common/help.c
> index 6cf8e2a9b2ae..eff9aac537db 100644
> --- a/common/help.c
> +++ b/common/help.c
> @@ -309,10 +309,11 @@ static int usage_command_internal(const char * const *usagestr,
>   	ret = do_usage_one_command(usagestr, flags, cmd_flags, outf);
>   	switch (ret) {
>   	case -1:
> -		fprintf(outf, "No usage for '%s'\n", token);
> +		fprintf(outf, "No usage for '%s'\n", token ? token : "");

You can just go with 'token ? : ""'

Thanks,
Qu
>   		break;
>   	case -2:
> -		fprintf(outf, "No short description for '%s'\n", token);
> +		fprintf(outf, "No short description for '%s'\n",
> +							token ? token : "");
>   		break;
>   	}
>
diff mbox series

Patch

diff --git a/common/help.c b/common/help.c
index 6cf8e2a9b2ae..eff9aac537db 100644
--- a/common/help.c
+++ b/common/help.c
@@ -309,10 +309,11 @@  static int usage_command_internal(const char * const *usagestr,
 	ret = do_usage_one_command(usagestr, flags, cmd_flags, outf);
 	switch (ret) {
 	case -1:
-		fprintf(outf, "No usage for '%s'\n", token);
+		fprintf(outf, "No usage for '%s'\n", token ? token : "");
 		break;
 	case -2:
-		fprintf(outf, "No short description for '%s'\n", token);
+		fprintf(outf, "No short description for '%s'\n",
+							token ? token : "");
 		break;
 	}