Skip to contentSkip to conversation

net: ipv4: fix clang -Wformat warning

PATCH In review Justin Stitt 2 participants 4 messages Jul 7, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: ipv4: fix clang -Wformat warning

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.

---
 net/ipv4/ah4.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/ah4.c +1 -1
index 6eea1e9e998d..3bf3d01fb7e3 100644
@@ -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);
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
#1 lore raw
Quoted 12 lines · Justin Stitt

When building with Clang we encounter this warning:

Quoted 3 lines

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.

Quoted 7 lines

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.

net/ipv4/ah4.c

Quoted 1 line
net/ipv4/ah4.c +0 -0

[]

Quoted 9 lines
net/ipv4/ah4.c +1 -1
@@ -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);
#2 lore raw
Quoted 22 lines · Joe Perches
Quoted 12 lines · Justin Stitt

When building with Clang we encounter this warning:

Quoted 3 lines

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.

Quoted 7 lines

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.

Quoted 2 lines

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?

Quoted 13 lines

net/ipv4/ah4.c

Quoted 2 lines
net/ipv4/ah4.c +0 -0

[]

Quoted 9 lines
net/ipv4/ah4.c +1 -1
@@ -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);
#3 lore raw
Quoted 23 lines · Justin Stitt
Quoted 22 lines · Joe Perches
Quoted 19 lines · Justin Stitt

When building with Clang we encounter this warning:

Quoted 3 lines

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.

Quoted 5 lines
Quoted 2 lines

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.

Quoted 13 lines
Quoted 13 lines

net/ipv4/ah4.c

Quoted 2 lines
net/ipv4/ah4.c +0 -0

[]

Quoted 9 lines
net/ipv4/ah4.c +1 -1
@@ -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);