diff mbox series

net: ipv4: fix clang -Wformat warning

Message ID 20220707173040.704116-1-justinstitt@google.com (mailing list archive)
State Changes Requested
Delegated to: Netdev Maintainers
Headers show
Series net: ipv4: fix clang -Wformat warning | expand

Checks

Context Check Description
netdev/tree_selection success Guessed tree name to be net-next
netdev/fixes_present success Fixes tag not required for -next series
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 2 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 8 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Justin Stitt July 7, 2022, 5:30 p.m. UTC
When building with Clang we encounter this warning:
| net/ipv4/ah4.c:513:4: error: format specifies type 'unsigned short' but
| the argument has type 'int' [-Werror,-Wformat]
| aalg_desc->uinfo.auth.icv_fullbits / 8);

`aalg_desc->uinfo.auth.icv_fullbits` is a u16 but due to default
argument promotion becomes an int.

Variadic functions (printf-like) undergo default argument promotion.
Documentation/core-api/printk-formats.rst specifically recommends using
the promoted-to-type's format flag.

As per C11 6.3.1.1:
(https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf) `If an int
can represent all values of the original type ..., the value is
converted to an int; otherwise, it is converted to an unsigned int.
These are called the integer promotions.` Thus it makes sense to change
%hu to %d not only to follow this standard but to suppress the warning
as well.

Link: https://github.com/ClangBuiltLinux/linux/issues/378
Signed-off-by: Justin Stitt <justinstitt@google.com>
---
 net/ipv4/ah4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Joe Perches July 7, 2022, 5:40 p.m. UTC | #1
On Thu, 2022-07-07 at 10:30 -0700, Justin Stitt wrote:
> When building with Clang we encounter this warning:
> > net/ipv4/ah4.c:513:4: error: format specifies type 'unsigned short' but
> > the argument has type 'int' [-Werror,-Wformat]
> > aalg_desc->uinfo.auth.icv_fullbits / 8);
> 
> `aalg_desc->uinfo.auth.icv_fullbits` is a u16 but due to default
> argument promotion becomes an int.
> 
> Variadic functions (printf-like) undergo default argument promotion.
> Documentation/core-api/printk-formats.rst specifically recommends using
> the promoted-to-type's format flag.
> 
> As per C11 6.3.1.1:
> (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf) `If an int
> can represent all values of the original type ..., the value is
> converted to an int; otherwise, it is converted to an unsigned int.
> These are called the integer promotions.` Thus it makes sense to change
> %hu to %d not only to follow this standard but to suppress the warning
> as well.

I think it also makes sense to use %u and not %d
as the original type is unsigned.

> diff --git a/net/ipv4/ah4.c b/net/ipv4/ah4.c
[]
> @@ -507,7 +507,7 @@ static int ah_init_state(struct xfrm_state *x)
>  
>  	if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
>  	    crypto_ahash_digestsize(ahash)) {
> -		pr_info("%s: %s digestsize %u != %hu\n",
> +		pr_info("%s: %s digestsize %u != %d\n",
>  			__func__, x->aalg->alg_name,
>  			crypto_ahash_digestsize(ahash),
>  			aalg_desc->uinfo.auth.icv_fullbits / 8);
Justin Stitt July 7, 2022, 5:47 p.m. UTC | #2
On Thu, Jul 7, 2022 at 10:40 AM Joe Perches <joe@perches.com> wrote:
>
> On Thu, 2022-07-07 at 10:30 -0700, Justin Stitt wrote:
> > When building with Clang we encounter this warning:
> > > net/ipv4/ah4.c:513:4: error: format specifies type 'unsigned short' but
> > > the argument has type 'int' [-Werror,-Wformat]
> > > aalg_desc->uinfo.auth.icv_fullbits / 8);
> >
> > `aalg_desc->uinfo.auth.icv_fullbits` is a u16 but due to default
> > argument promotion becomes an int.
> >
> > Variadic functions (printf-like) undergo default argument promotion.
> > Documentation/core-api/printk-formats.rst specifically recommends using
> > the promoted-to-type's format flag.
> >
> > As per C11 6.3.1.1:
> > (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf) `If an int
> > can represent all values of the original type ..., the value is
> > converted to an int; otherwise, it is converted to an unsigned int.
> > These are called the integer promotions.` Thus it makes sense to change
> > %hu to %d not only to follow this standard but to suppress the warning
> > as well.
>
> I think it also makes sense to use %u and not %d
> as the original type is unsigned.
Yeah, that would also work. An integer (even a signed one) fully
encompasses a u16 so it's really a choice of style. Do you think the
change to %u warrants a v2 of this patch?
>
> > diff --git a/net/ipv4/ah4.c b/net/ipv4/ah4.c
> []
> > @@ -507,7 +507,7 @@ static int ah_init_state(struct xfrm_state *x)
> >
> >       if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
> >           crypto_ahash_digestsize(ahash)) {
> > -             pr_info("%s: %s digestsize %u != %hu\n",
> > +             pr_info("%s: %s digestsize %u != %d\n",
> >                       __func__, x->aalg->alg_name,
> >                       crypto_ahash_digestsize(ahash),
> >                       aalg_desc->uinfo.auth.icv_fullbits / 8);
>
Joe Perches July 7, 2022, 6:01 p.m. UTC | #3
On Thu, 2022-07-07 at 10:47 -0700, Justin Stitt wrote:
> On Thu, Jul 7, 2022 at 10:40 AM Joe Perches <joe@perches.com> wrote:
> > 
> > On Thu, 2022-07-07 at 10:30 -0700, Justin Stitt wrote:
> > > When building with Clang we encounter this warning:
> > > > net/ipv4/ah4.c:513:4: error: format specifies type 'unsigned short' but
> > > > the argument has type 'int' [-Werror,-Wformat]
> > > > aalg_desc->uinfo.auth.icv_fullbits / 8);
> > > 
> > > `aalg_desc->uinfo.auth.icv_fullbits` is a u16 but due to default
> > > argument promotion becomes an int.
> > > 
> > > Variadic functions (printf-like) undergo default argument promotion.
> > > Documentation/core-api/printk-formats.rst specifically recommends using
> > > the promoted-to-type's format flag.
> > > 
> > > As per C11 6.3.1.1:
> > > (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf) `If an int
> > > can represent all values of the original type ..., the value is
> > > converted to an int; otherwise, it is converted to an unsigned int.
> > > These are called the integer promotions.` Thus it makes sense to change
> > > %hu to %d not only to follow this standard but to suppress the warning
> > > as well.
> > 
> > I think it also makes sense to use %u and not %d
> > as the original type is unsigned.
> Yeah, that would also work. An integer (even a signed one) fully
> encompasses a u16 so it's really a choice of style. Do you think the
> change to %u warrants a v2 of this patch?

As it's rather odd output to use '%u != %d', probably.

Your patch, up to you.

> > 
> > > diff --git a/net/ipv4/ah4.c b/net/ipv4/ah4.c
> > []
> > > @@ -507,7 +507,7 @@ static int ah_init_state(struct xfrm_state *x)
> > > 
> > >       if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
> > >           crypto_ahash_digestsize(ahash)) {
> > > -             pr_info("%s: %s digestsize %u != %hu\n",
> > > +             pr_info("%s: %s digestsize %u != %d\n",
> > >                       __func__, x->aalg->alg_name,
> > >                       crypto_ahash_digestsize(ahash),
> > >                       aalg_desc->uinfo.auth.icv_fullbits / 8);
> >
diff mbox series

Patch

diff --git a/net/ipv4/ah4.c b/net/ipv4/ah4.c
index 6eea1e9e998d..3bf3d01fb7e3 100644
--- a/net/ipv4/ah4.c
+++ b/net/ipv4/ah4.c
@@ -507,7 +507,7 @@  static int ah_init_state(struct xfrm_state *x)
 
 	if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
 	    crypto_ahash_digestsize(ahash)) {
-		pr_info("%s: %s digestsize %u != %hu\n",
+		pr_info("%s: %s digestsize %u != %d\n",
 			__func__, x->aalg->alg_name,
 			crypto_ahash_digestsize(ahash),
 			aalg_desc->uinfo.auth.icv_fullbits / 8);