Skip to contentSkip to conversation

net: ipv4: fix clang -Wformat warnings

PATCH Applied Justin Stitt 2 participants 2 messages Jul 9, 2022 – Jul 14, 2022

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

PATCH v2 1/1 net: ipv4: fix clang -Wformat warnings

When building with Clang we encounter these warnings:

| 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);
-
| net/ipv4/esp4.c:1114:5: 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.

---
diff from v1 -> v2:
* packaged two related patches together: (Suggested by Nick)
- https://lore.kernel.org/all/[email protected]/
- https://lore.kernel.org/all/[email protected]/
* use Joe's suggestion regarding %u over %d.

 net/ipv4/ah4.c  | 2 +-
 net/ipv4/esp4.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
net/ipv4/ah4.c +1 -1
index 6eea1e9e998d..f8ad04470d3a 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 != %u\n",
 			__func__, x->aalg->alg_name,
 			crypto_ahash_digestsize(ahash),
 			aalg_desc->uinfo.auth.icv_fullbits / 8);
net/ipv4/esp4.c +1 -1
index b21238df3301..b694f352ce7a 100644
@@ -1108,7 +1108,7 @@ static int esp_init_authenc(struct xfrm_state *x)
 		err = -EINVAL;
 		if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
 		    crypto_aead_authsize(aead)) {
-			pr_info("ESP: %s digestsize %u != %hu\n",
+			pr_info("ESP: %s digestsize %u != %u\n",
 				x->aalg->alg_name,
 				crypto_aead_authsize(aead),
 				aalg_desc->uinfo.auth.icv_fullbits / 8);
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
maintainer Applied
#1 lore raw
Quoted 24 lines · Justin Stitt

When building with Clang we encounter these warnings:

| 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);
-
| net/ipv4/esp4.c:1114:5: 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

Applied to the ipsec tree, thanks a lot!