All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mediatek: mt7601u: fix clang -Wformat warning
@ 2022-07-11 21:29 ` Justin Stitt
  0 siblings, 0 replies; 12+ messages in thread
From: Justin Stitt @ 2022-07-11 21:29 UTC (permalink / raw)
  To: Jakub Kicinski, Kalle Valo, David S . Miller, Eric Dumazet,
	Paolo Abeni, Matthias Brugger
  Cc: Nathan Chancellor, Nick Desaulniers, Tom Rix, linux-wireless,
	netdev, linux-arm-kernel, linux-mediatek, linux-kernel, llvm,
	Justin Stitt

When building with Clang we encounter this warning:
| drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
| specifies type 'unsigned char' but the argument has type 'int'
| [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);

The format specifier used is `%hhu` which describes a u8. Both
`dev->ee->reg.start` and `.num` are u8 as well. However, the expression
as a whole is promoted to an int as you cannot get smaller-than-int from
addition. Therefore, to fix the warning, use the promoted-to-type's
format specifier -- in this case `%d`.

example:
```
uint8_t a = 4, b = 7;
int size = sizeof(a + b - 1);
printf("%d\n", size);
// output: 4
```

See more:
(https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules)
"Integer types smaller than int are promoted when an operation is
performed on them. If all values of the original type can be represented
as an int, the value of the smaller type is converted to an int;
otherwise, it is converted to an unsigned int."

Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: This patch silences the -Wformat warning for this file (which is
the goal) but in reality all instances of `%hh[dux]` should be converted
to `%[dux]` for this file and probably every file. That's a bit larger
scope than the goal of enabling -Wformat for Clang builds, though.

 drivers/net/wireless/mediatek/mt7601u/debugfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
index 20669eacb66e..230b0e1061a7 100644
--- a/drivers/net/wireless/mediatek/mt7601u/debugfs.c
+++ b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
@@ -88,7 +88,7 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
 		   dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
 	seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
 	seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
-	seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
+	seq_printf(file, "Reg channels: %hhu-%d\n", dev->ee->reg.start,
 		   dev->ee->reg.start + dev->ee->reg.num - 1);
 
 	seq_puts(file, "Per rate power:\n");
-- 
2.37.0.144.g8ac04bfd2-goog


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH] mediatek: mt7601u: fix clang -Wformat warning
@ 2022-07-11 21:29 ` Justin Stitt
  0 siblings, 0 replies; 12+ messages in thread
From: Justin Stitt @ 2022-07-11 21:29 UTC (permalink / raw)
  To: Jakub Kicinski, Kalle Valo, David S . Miller, Eric Dumazet,
	Paolo Abeni, Matthias Brugger
  Cc: Nathan Chancellor, Nick Desaulniers, Tom Rix, linux-wireless,
	netdev, linux-arm-kernel, linux-mediatek, linux-kernel, llvm,
	Justin Stitt

When building with Clang we encounter this warning:
| drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
| specifies type 'unsigned char' but the argument has type 'int'
| [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);

The format specifier used is `%hhu` which describes a u8. Both
`dev->ee->reg.start` and `.num` are u8 as well. However, the expression
as a whole is promoted to an int as you cannot get smaller-than-int from
addition. Therefore, to fix the warning, use the promoted-to-type's
format specifier -- in this case `%d`.

example:
```
uint8_t a = 4, b = 7;
int size = sizeof(a + b - 1);
printf("%d\n", size);
// output: 4
```

See more:
(https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules)
"Integer types smaller than int are promoted when an operation is
performed on them. If all values of the original type can be represented
as an int, the value of the smaller type is converted to an int;
otherwise, it is converted to an unsigned int."

Signed-off-by: Justin Stitt <justinstitt@google.com>
---
Note: This patch silences the -Wformat warning for this file (which is
the goal) but in reality all instances of `%hh[dux]` should be converted
to `%[dux]` for this file and probably every file. That's a bit larger
scope than the goal of enabling -Wformat for Clang builds, though.

 drivers/net/wireless/mediatek/mt7601u/debugfs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
index 20669eacb66e..230b0e1061a7 100644
--- a/drivers/net/wireless/mediatek/mt7601u/debugfs.c
+++ b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
@@ -88,7 +88,7 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
 		   dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
 	seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
 	seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
-	seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
+	seq_printf(file, "Reg channels: %hhu-%d\n", dev->ee->reg.start,
 		   dev->ee->reg.start + dev->ee->reg.num - 1);
 
 	seq_puts(file, "Per rate power:\n");
-- 
2.37.0.144.g8ac04bfd2-goog


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] mediatek: mt7601u: fix clang -Wformat warning
  2022-07-11 21:29 ` Justin Stitt
@ 2022-07-11 22:16   ` Jakub Kicinski
  -1 siblings, 0 replies; 12+ messages in thread
From: Jakub Kicinski @ 2022-07-11 22:16 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Kalle Valo, David S . Miller, Eric Dumazet, Paolo Abeni,
	Matthias Brugger, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	linux-wireless, netdev, linux-arm-kernel, linux-mediatek,
	linux-kernel, llvm

On Mon, 11 Jul 2022 14:29:32 -0700 Justin Stitt wrote:
> When building with Clang we encounter this warning:
> | drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> | specifies type 'unsigned char' but the argument has type 'int'
> | [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
> 
> The format specifier used is `%hhu` which describes a u8. Both
> `dev->ee->reg.start` and `.num` are u8 as well. However, the expression
> as a whole is promoted to an int as you cannot get smaller-than-int from
> addition. Therefore, to fix the warning, use the promoted-to-type's
> format specifier -- in this case `%d`.

Acked-by: Jakub Kicinski <kubakici@wp.pl>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] mediatek: mt7601u: fix clang -Wformat warning
@ 2022-07-11 22:16   ` Jakub Kicinski
  0 siblings, 0 replies; 12+ messages in thread
From: Jakub Kicinski @ 2022-07-11 22:16 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Kalle Valo, David S . Miller, Eric Dumazet, Paolo Abeni,
	Matthias Brugger, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	linux-wireless, netdev, linux-arm-kernel, linux-mediatek,
	linux-kernel, llvm

On Mon, 11 Jul 2022 14:29:32 -0700 Justin Stitt wrote:
> When building with Clang we encounter this warning:
> | drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> | specifies type 'unsigned char' but the argument has type 'int'
> | [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
> 
> The format specifier used is `%hhu` which describes a u8. Both
> `dev->ee->reg.start` and `.num` are u8 as well. However, the expression
> as a whole is promoted to an int as you cannot get smaller-than-int from
> addition. Therefore, to fix the warning, use the promoted-to-type's
> format specifier -- in this case `%d`.

Acked-by: Jakub Kicinski <kubakici@wp.pl>

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] mediatek: mt7601u: fix clang -Wformat warning
  2022-07-11 21:29 ` Justin Stitt
@ 2022-07-15  6:32   ` Joe Perches
  -1 siblings, 0 replies; 12+ messages in thread
From: Joe Perches @ 2022-07-15  6:32 UTC (permalink / raw)
  To: Justin Stitt, Jakub Kicinski, Kalle Valo, David S . Miller,
	Eric Dumazet, Paolo Abeni, Matthias Brugger
  Cc: Nathan Chancellor, Nick Desaulniers, Tom Rix, linux-wireless,
	netdev, linux-arm-kernel, linux-mediatek, linux-kernel, llvm

On Mon, 2022-07-11 at 14:29 -0700, Justin Stitt wrote:
> When building with Clang we encounter this warning:
> > drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> > specifies type 'unsigned char' but the argument has type 'int'
> > [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
> 
> The format specifier used is `%hhu` which describes a u8. Both
> `dev->ee->reg.start` and `.num` are u8 as well. However, the expression
> as a whole is promoted to an int as you cannot get smaller-than-int from
> addition. Therefore, to fix the warning, use the promoted-to-type's
> format specifier -- in this case `%d`.

I think whenever a sizeof(unsigned type) that is less than sizeof(int) is
emitted with vsprintf, the preferred format specifier should be %u not %d.

> diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
[]
> @@ -88,7 +88,7 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
>  		   dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
>  	seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
>  	seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
> -	seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
> +	seq_printf(file, "Reg channels: %hhu-%d\n", dev->ee->reg.start,
>  		   dev->ee->reg.start + dev->ee->reg.num - 1);

And this is not a promotion of an argument to int via varargs.
The arithmetic did the promotion.

I suggest s/%hh/%/ for all the uses here, not just this one.

checkpatch could do this somewhat automatically.
Of course any changes it suggests need human review.

$ ./scripts/checkpatch.pl -f drivers/net/wireless/mediatek/mt7601u/debugfs.c --show-types --types=unnecessary_modifier --fix-inplace
$ git diff --stat -p drivers/net/wireless/mediatek/mt7601u/debugfs.c
---
 drivers/net/wireless/mediatek/mt7601u/debugfs.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
index 20669eacb66ea..b7a6376e3352e 100644
--- a/drivers/net/wireless/mediatek/mt7601u/debugfs.c
+++ b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
@@ -83,28 +83,28 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
 	struct tssi_data *td = &dev->ee->tssi_data;
 	int i;
 
-	seq_printf(file, "RF freq offset: %hhx\n", dev->ee->rf_freq_off);
-	seq_printf(file, "RSSI offset: %hhx %hhx\n",
+	seq_printf(file, "RF freq offset: %x\n", dev->ee->rf_freq_off);
+	seq_printf(file, "RSSI offset: %x %x\n",
 		   dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
-	seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
-	seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
-	seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
+	seq_printf(file, "Reference temp: %x\n", dev->ee->ref_temp);
+	seq_printf(file, "LNA gain: %x\n", dev->ee->lna_gain);
+	seq_printf(file, "Reg channels: %u-%u\n", dev->ee->reg.start,
 		   dev->ee->reg.start + dev->ee->reg.num - 1);
 
 	seq_puts(file, "Per rate power:\n");
 	for (i = 0; i < 2; i++)
-		seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
+		seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
 			   rp->cck[i].raw, rp->cck[i].bw20, rp->cck[i].bw40);
 	for (i = 0; i < 4; i++)
-		seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
+		seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
 			   rp->ofdm[i].raw, rp->ofdm[i].bw20, rp->ofdm[i].bw40);
 	for (i = 0; i < 4; i++)
-		seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
+		seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
 			   rp->ht[i].raw, rp->ht[i].bw20, rp->ht[i].bw40);
 
 	seq_puts(file, "Per channel power:\n");
 	for (i = 0; i < 7; i++)
-		seq_printf(file, "\t tx_power  ch%u:%02hhx ch%u:%02hhx\n",
+		seq_printf(file, "\t tx_power  ch%u:%02x ch%u:%02x\n",
 			   i * 2 + 1, dev->ee->chan_pwr[i * 2],
 			   i * 2 + 2, dev->ee->chan_pwr[i * 2 + 1]);
 
@@ -112,8 +112,8 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
 		return 0;
 
 	seq_puts(file, "TSSI:\n");
-	seq_printf(file, "\t slope:%02hhx\n", td->slope);
-	seq_printf(file, "\t offset=%02hhx %02hhx %02hhx\n",
+	seq_printf(file, "\t slope:%02x\n", td->slope);
+	seq_printf(file, "\t offset=%02x %02x %02x\n",
 		   td->offset[0], td->offset[1], td->offset[2]);
 	seq_printf(file, "\t delta_off:%08x\n", td->tx0_delta_offset);
 


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] mediatek: mt7601u: fix clang -Wformat warning
@ 2022-07-15  6:32   ` Joe Perches
  0 siblings, 0 replies; 12+ messages in thread
From: Joe Perches @ 2022-07-15  6:32 UTC (permalink / raw)
  To: Justin Stitt, Jakub Kicinski, Kalle Valo, David S . Miller,
	Eric Dumazet, Paolo Abeni, Matthias Brugger
  Cc: Nathan Chancellor, Nick Desaulniers, Tom Rix, linux-wireless,
	netdev, linux-arm-kernel, linux-mediatek, linux-kernel, llvm

On Mon, 2022-07-11 at 14:29 -0700, Justin Stitt wrote:
> When building with Clang we encounter this warning:
> > drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> > specifies type 'unsigned char' but the argument has type 'int'
> > [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
> 
> The format specifier used is `%hhu` which describes a u8. Both
> `dev->ee->reg.start` and `.num` are u8 as well. However, the expression
> as a whole is promoted to an int as you cannot get smaller-than-int from
> addition. Therefore, to fix the warning, use the promoted-to-type's
> format specifier -- in this case `%d`.

I think whenever a sizeof(unsigned type) that is less than sizeof(int) is
emitted with vsprintf, the preferred format specifier should be %u not %d.

> diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
[]
> @@ -88,7 +88,7 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
>  		   dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
>  	seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
>  	seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
> -	seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
> +	seq_printf(file, "Reg channels: %hhu-%d\n", dev->ee->reg.start,
>  		   dev->ee->reg.start + dev->ee->reg.num - 1);

And this is not a promotion of an argument to int via varargs.
The arithmetic did the promotion.

I suggest s/%hh/%/ for all the uses here, not just this one.

checkpatch could do this somewhat automatically.
Of course any changes it suggests need human review.

$ ./scripts/checkpatch.pl -f drivers/net/wireless/mediatek/mt7601u/debugfs.c --show-types --types=unnecessary_modifier --fix-inplace
$ git diff --stat -p drivers/net/wireless/mediatek/mt7601u/debugfs.c
---
 drivers/net/wireless/mediatek/mt7601u/debugfs.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
index 20669eacb66ea..b7a6376e3352e 100644
--- a/drivers/net/wireless/mediatek/mt7601u/debugfs.c
+++ b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
@@ -83,28 +83,28 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
 	struct tssi_data *td = &dev->ee->tssi_data;
 	int i;
 
-	seq_printf(file, "RF freq offset: %hhx\n", dev->ee->rf_freq_off);
-	seq_printf(file, "RSSI offset: %hhx %hhx\n",
+	seq_printf(file, "RF freq offset: %x\n", dev->ee->rf_freq_off);
+	seq_printf(file, "RSSI offset: %x %x\n",
 		   dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
-	seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
-	seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
-	seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
+	seq_printf(file, "Reference temp: %x\n", dev->ee->ref_temp);
+	seq_printf(file, "LNA gain: %x\n", dev->ee->lna_gain);
+	seq_printf(file, "Reg channels: %u-%u\n", dev->ee->reg.start,
 		   dev->ee->reg.start + dev->ee->reg.num - 1);
 
 	seq_puts(file, "Per rate power:\n");
 	for (i = 0; i < 2; i++)
-		seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
+		seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
 			   rp->cck[i].raw, rp->cck[i].bw20, rp->cck[i].bw40);
 	for (i = 0; i < 4; i++)
-		seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
+		seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
 			   rp->ofdm[i].raw, rp->ofdm[i].bw20, rp->ofdm[i].bw40);
 	for (i = 0; i < 4; i++)
-		seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
+		seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
 			   rp->ht[i].raw, rp->ht[i].bw20, rp->ht[i].bw40);
 
 	seq_puts(file, "Per channel power:\n");
 	for (i = 0; i < 7; i++)
-		seq_printf(file, "\t tx_power  ch%u:%02hhx ch%u:%02hhx\n",
+		seq_printf(file, "\t tx_power  ch%u:%02x ch%u:%02x\n",
 			   i * 2 + 1, dev->ee->chan_pwr[i * 2],
 			   i * 2 + 2, dev->ee->chan_pwr[i * 2 + 1]);
 
@@ -112,8 +112,8 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
 		return 0;
 
 	seq_puts(file, "TSSI:\n");
-	seq_printf(file, "\t slope:%02hhx\n", td->slope);
-	seq_printf(file, "\t offset=%02hhx %02hhx %02hhx\n",
+	seq_printf(file, "\t slope:%02x\n", td->slope);
+	seq_printf(file, "\t offset=%02x %02x %02x\n",
 		   td->offset[0], td->offset[1], td->offset[2]);
 	seq_printf(file, "\t delta_off:%08x\n", td->tx0_delta_offset);
 


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH] mediatek: mt7601u: fix clang -Wformat warning
  2022-07-15  6:32   ` Joe Perches
@ 2022-07-16  0:08     ` Justin Stitt
  -1 siblings, 0 replies; 12+ messages in thread
From: Justin Stitt @ 2022-07-16  0:08 UTC (permalink / raw)
  To: Joe Perches
  Cc: Jakub Kicinski, Kalle Valo, David S . Miller, Eric Dumazet,
	Paolo Abeni, Matthias Brugger, Nathan Chancellor,
	Nick Desaulniers, Tom Rix, linux-wireless, netdev,
	linux-arm-kernel, linux-mediatek, linux-kernel, llvm

On Thu, Jul 14, 2022 at 11:33 PM Joe Perches <joe@perches.com> wrote:
>
> On Mon, 2022-07-11 at 14:29 -0700, Justin Stitt wrote:
> > When building with Clang we encounter this warning:
> > > drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> > > specifies type 'unsigned char' but the argument has type 'int'
> > > [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
> >
> > The format specifier used is `%hhu` which describes a u8. Both
> > `dev->ee->reg.start` and `.num` are u8 as well. However, the expression
> > as a whole is promoted to an int as you cannot get smaller-than-int from
> > addition. Therefore, to fix the warning, use the promoted-to-type's
> > format specifier -- in this case `%d`.
>
> I think whenever a sizeof(unsigned type) that is less than sizeof(int) is
> emitted with vsprintf, the preferred format specifier should be %u not %d.

I believe the standard recommends using the promoted-to-type's format
specifier, in this case integer. I agree, though, that it is quite
bizarre to represent unsigned types with signed types. I'd be
interested in hearing more opinions on the matter.

> > diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
> []
> > @@ -88,7 +88,7 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
> >                  dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
> >       seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
> >       seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
> > -     seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
> > +     seq_printf(file, "Reg channels: %hhu-%d\n", dev->ee->reg.start,
> >                  dev->ee->reg.start + dev->ee->reg.num - 1);
>
> And this is not a promotion of an argument to int via varargs.
> The arithmetic did the promotion.

Right, I noted in my patch message that the type promotion was due to addition.

>
> I suggest s/%hh/%/ for all the uses here, not just this one.

I also contemplated this change but I think it might be a bit out of
scope for https://github.com/ClangBuiltLinux/linux/issues/378  -- What
do you think?
It could be argued that every single instance of %hh[dux] should be
replaced with %[dux].

> checkpatch could do this somewhat automatically.
> Of course any changes it suggests need human review.
>
> $ ./scripts/checkpatch.pl -f drivers/net/wireless/mediatek/mt7601u/debugfs.c --show-types --types=unnecessary_modifier --fix-inplace
> $ git diff --stat -p drivers/net/wireless/mediatek/mt7601u/debugfs.c
> ---
>  drivers/net/wireless/mediatek/mt7601u/debugfs.c | 22 +++++++++++-----------
>  1 file changed, 11 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
> index 20669eacb66ea..b7a6376e3352e 100644
> --- a/drivers/net/wireless/mediatek/mt7601u/debugfs.c
> +++ b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
> @@ -83,28 +83,28 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
>         struct tssi_data *td = &dev->ee->tssi_data;
>         int i;
>
> -       seq_printf(file, "RF freq offset: %hhx\n", dev->ee->rf_freq_off);
> -       seq_printf(file, "RSSI offset: %hhx %hhx\n",
> +       seq_printf(file, "RF freq offset: %x\n", dev->ee->rf_freq_off);
> +       seq_printf(file, "RSSI offset: %x %x\n",
>                    dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
> -       seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
> -       seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
> -       seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
> +       seq_printf(file, "Reference temp: %x\n", dev->ee->ref_temp);
> +       seq_printf(file, "LNA gain: %x\n", dev->ee->lna_gain);
> +       seq_printf(file, "Reg channels: %u-%u\n", dev->ee->reg.start,
>                    dev->ee->reg.start + dev->ee->reg.num - 1);
>
>         seq_puts(file, "Per rate power:\n");
>         for (i = 0; i < 2; i++)
> -               seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
> +               seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
>                            rp->cck[i].raw, rp->cck[i].bw20, rp->cck[i].bw40);
>         for (i = 0; i < 4; i++)
> -               seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
> +               seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
>                            rp->ofdm[i].raw, rp->ofdm[i].bw20, rp->ofdm[i].bw40);
>         for (i = 0; i < 4; i++)
> -               seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
> +               seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
>                            rp->ht[i].raw, rp->ht[i].bw20, rp->ht[i].bw40);
>
>         seq_puts(file, "Per channel power:\n");
>         for (i = 0; i < 7; i++)
> -               seq_printf(file, "\t tx_power  ch%u:%02hhx ch%u:%02hhx\n",
> +               seq_printf(file, "\t tx_power  ch%u:%02x ch%u:%02x\n",
>                            i * 2 + 1, dev->ee->chan_pwr[i * 2],
>                            i * 2 + 2, dev->ee->chan_pwr[i * 2 + 1]);
>
> @@ -112,8 +112,8 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
>                 return 0;
>
>         seq_puts(file, "TSSI:\n");
> -       seq_printf(file, "\t slope:%02hhx\n", td->slope);
> -       seq_printf(file, "\t offset=%02hhx %02hhx %02hhx\n",
> +       seq_printf(file, "\t slope:%02x\n", td->slope);
> +       seq_printf(file, "\t offset=%02x %02x %02x\n",
>                    td->offset[0], td->offset[1], td->offset[2]);
>         seq_printf(file, "\t delta_off:%08x\n", td->tx0_delta_offset);
>
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] mediatek: mt7601u: fix clang -Wformat warning
@ 2022-07-16  0:08     ` Justin Stitt
  0 siblings, 0 replies; 12+ messages in thread
From: Justin Stitt @ 2022-07-16  0:08 UTC (permalink / raw)
  To: Joe Perches
  Cc: Jakub Kicinski, Kalle Valo, David S . Miller, Eric Dumazet,
	Paolo Abeni, Matthias Brugger, Nathan Chancellor,
	Nick Desaulniers, Tom Rix, linux-wireless, netdev,
	linux-arm-kernel, linux-mediatek, linux-kernel, llvm

On Thu, Jul 14, 2022 at 11:33 PM Joe Perches <joe@perches.com> wrote:
>
> On Mon, 2022-07-11 at 14:29 -0700, Justin Stitt wrote:
> > When building with Clang we encounter this warning:
> > > drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> > > specifies type 'unsigned char' but the argument has type 'int'
> > > [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
> >
> > The format specifier used is `%hhu` which describes a u8. Both
> > `dev->ee->reg.start` and `.num` are u8 as well. However, the expression
> > as a whole is promoted to an int as you cannot get smaller-than-int from
> > addition. Therefore, to fix the warning, use the promoted-to-type's
> > format specifier -- in this case `%d`.
>
> I think whenever a sizeof(unsigned type) that is less than sizeof(int) is
> emitted with vsprintf, the preferred format specifier should be %u not %d.

I believe the standard recommends using the promoted-to-type's format
specifier, in this case integer. I agree, though, that it is quite
bizarre to represent unsigned types with signed types. I'd be
interested in hearing more opinions on the matter.

> > diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
> []
> > @@ -88,7 +88,7 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
> >                  dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
> >       seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
> >       seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
> > -     seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
> > +     seq_printf(file, "Reg channels: %hhu-%d\n", dev->ee->reg.start,
> >                  dev->ee->reg.start + dev->ee->reg.num - 1);
>
> And this is not a promotion of an argument to int via varargs.
> The arithmetic did the promotion.

Right, I noted in my patch message that the type promotion was due to addition.

>
> I suggest s/%hh/%/ for all the uses here, not just this one.

I also contemplated this change but I think it might be a bit out of
scope for https://github.com/ClangBuiltLinux/linux/issues/378  -- What
do you think?
It could be argued that every single instance of %hh[dux] should be
replaced with %[dux].

> checkpatch could do this somewhat automatically.
> Of course any changes it suggests need human review.
>
> $ ./scripts/checkpatch.pl -f drivers/net/wireless/mediatek/mt7601u/debugfs.c --show-types --types=unnecessary_modifier --fix-inplace
> $ git diff --stat -p drivers/net/wireless/mediatek/mt7601u/debugfs.c
> ---
>  drivers/net/wireless/mediatek/mt7601u/debugfs.c | 22 +++++++++++-----------
>  1 file changed, 11 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/net/wireless/mediatek/mt7601u/debugfs.c b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
> index 20669eacb66ea..b7a6376e3352e 100644
> --- a/drivers/net/wireless/mediatek/mt7601u/debugfs.c
> +++ b/drivers/net/wireless/mediatek/mt7601u/debugfs.c
> @@ -83,28 +83,28 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
>         struct tssi_data *td = &dev->ee->tssi_data;
>         int i;
>
> -       seq_printf(file, "RF freq offset: %hhx\n", dev->ee->rf_freq_off);
> -       seq_printf(file, "RSSI offset: %hhx %hhx\n",
> +       seq_printf(file, "RF freq offset: %x\n", dev->ee->rf_freq_off);
> +       seq_printf(file, "RSSI offset: %x %x\n",
>                    dev->ee->rssi_offset[0], dev->ee->rssi_offset[1]);
> -       seq_printf(file, "Reference temp: %hhx\n", dev->ee->ref_temp);
> -       seq_printf(file, "LNA gain: %hhx\n", dev->ee->lna_gain);
> -       seq_printf(file, "Reg channels: %hhu-%hhu\n", dev->ee->reg.start,
> +       seq_printf(file, "Reference temp: %x\n", dev->ee->ref_temp);
> +       seq_printf(file, "LNA gain: %x\n", dev->ee->lna_gain);
> +       seq_printf(file, "Reg channels: %u-%u\n", dev->ee->reg.start,
>                    dev->ee->reg.start + dev->ee->reg.num - 1);
>
>         seq_puts(file, "Per rate power:\n");
>         for (i = 0; i < 2; i++)
> -               seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
> +               seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
>                            rp->cck[i].raw, rp->cck[i].bw20, rp->cck[i].bw40);
>         for (i = 0; i < 4; i++)
> -               seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
> +               seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
>                            rp->ofdm[i].raw, rp->ofdm[i].bw20, rp->ofdm[i].bw40);
>         for (i = 0; i < 4; i++)
> -               seq_printf(file, "\t raw:%02hhx bw20:%02hhx bw40:%02hhx\n",
> +               seq_printf(file, "\t raw:%02x bw20:%02x bw40:%02x\n",
>                            rp->ht[i].raw, rp->ht[i].bw20, rp->ht[i].bw40);
>
>         seq_puts(file, "Per channel power:\n");
>         for (i = 0; i < 7; i++)
> -               seq_printf(file, "\t tx_power  ch%u:%02hhx ch%u:%02hhx\n",
> +               seq_printf(file, "\t tx_power  ch%u:%02x ch%u:%02x\n",
>                            i * 2 + 1, dev->ee->chan_pwr[i * 2],
>                            i * 2 + 2, dev->ee->chan_pwr[i * 2 + 1]);
>
> @@ -112,8 +112,8 @@ mt7601u_eeprom_param_show(struct seq_file *file, void *data)
>                 return 0;
>
>         seq_puts(file, "TSSI:\n");
> -       seq_printf(file, "\t slope:%02hhx\n", td->slope);
> -       seq_printf(file, "\t offset=%02hhx %02hhx %02hhx\n",
> +       seq_printf(file, "\t slope:%02x\n", td->slope);
> +       seq_printf(file, "\t offset=%02x %02x %02x\n",
>                    td->offset[0], td->offset[1], td->offset[2]);
>         seq_printf(file, "\t delta_off:%08x\n", td->tx0_delta_offset);
>
>

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] mediatek: mt7601u: fix clang -Wformat warning
  2022-07-16  0:08     ` Justin Stitt
@ 2022-07-16  1:40       ` Joe Perches
  -1 siblings, 0 replies; 12+ messages in thread
From: Joe Perches @ 2022-07-16  1:40 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Jakub Kicinski, Kalle Valo, David S . Miller, Eric Dumazet,
	Paolo Abeni, Matthias Brugger, Nathan Chancellor,
	Nick Desaulniers, Tom Rix, linux-wireless, netdev,
	linux-arm-kernel, linux-mediatek, linux-kernel, llvm

On Fri, 2022-07-15 at 17:08 -0700, Justin Stitt wrote:
> On Thu, Jul 14, 2022 at 11:33 PM Joe Perches <joe@perches.com> wrote:
> > 
> > On Mon, 2022-07-11 at 14:29 -0700, Justin Stitt wrote:
> > > When building with Clang we encounter this warning:
> > > > drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> > > > specifies type 'unsigned char' but the argument has type 'int'
> > > > [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
[]
> > I suggest s/%hh/%/ for all the uses here, not just this one.
> 
> I also contemplated this change but I think it might be a bit out of
> scope for https://github.com/ClangBuiltLinux/linux/issues/378  -- What
> do you think?

It would not be out of scope.

> It could be argued that every single instance of %hh[dux] should be
> replaced with %[dux].

All the vsprintf ones, but not the scanf ones.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH] mediatek: mt7601u: fix clang -Wformat warning
@ 2022-07-16  1:40       ` Joe Perches
  0 siblings, 0 replies; 12+ messages in thread
From: Joe Perches @ 2022-07-16  1:40 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Jakub Kicinski, Kalle Valo, David S . Miller, Eric Dumazet,
	Paolo Abeni, Matthias Brugger, Nathan Chancellor,
	Nick Desaulniers, Tom Rix, linux-wireless, netdev,
	linux-arm-kernel, linux-mediatek, linux-kernel, llvm

On Fri, 2022-07-15 at 17:08 -0700, Justin Stitt wrote:
> On Thu, Jul 14, 2022 at 11:33 PM Joe Perches <joe@perches.com> wrote:
> > 
> > On Mon, 2022-07-11 at 14:29 -0700, Justin Stitt wrote:
> > > When building with Clang we encounter this warning:
> > > > drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> > > > specifies type 'unsigned char' but the argument has type 'int'
> > > > [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
[]
> > I suggest s/%hh/%/ for all the uses here, not just this one.
> 
> I also contemplated this change but I think it might be a bit out of
> scope for https://github.com/ClangBuiltLinux/linux/issues/378  -- What
> do you think?

It would not be out of scope.

> It could be argued that every single instance of %hh[dux] should be
> replaced with %[dux].

All the vsprintf ones, but not the scanf ones.


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: wifi: mt7601u: fix clang -Wformat warning
  2022-07-11 21:29 ` Justin Stitt
@ 2022-07-18 11:54   ` Kalle Valo
  -1 siblings, 0 replies; 12+ messages in thread
From: Kalle Valo @ 2022-07-18 11:54 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Jakub Kicinski, David S . Miller, Eric Dumazet, Paolo Abeni,
	Matthias Brugger, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	linux-wireless, netdev, linux-arm-kernel, linux-mediatek,
	linux-kernel, llvm, Justin Stitt

Justin Stitt <justinstitt@google.com> wrote:

> When building with Clang we encounter this warning:
> | drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> | specifies type 'unsigned char' but the argument has type 'int'
> | [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
> 
> The format specifier used is `%hhu` which describes a u8. Both
> `dev->ee->reg.start` and `.num` are u8 as well. However, the expression
> as a whole is promoted to an int as you cannot get smaller-than-int from
> addition. Therefore, to fix the warning, use the promoted-to-type's
> format specifier -- in this case `%d`.
> 
> example:
> ```
> uint8_t a = 4, b = 7;
> int size = sizeof(a + b - 1);
> printf("%d\n", size);
> // output: 4
> ```
> 
> See more:
> (https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules)
> "Integer types smaller than int are promoted when an operation is
> performed on them. If all values of the original type can be represented
> as an int, the value of the smaller type is converted to an int;
> otherwise, it is converted to an unsigned int."
> 
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> Acked-by: Jakub Kicinski <kubakici@wp.pl>

Patch applied to wireless-next.git, thanks.

68204a696505 wifi: mt7601u: fix clang -Wformat warning

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/20220711212932.1501592-1-justinstitt@google.com/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: wifi: mt7601u: fix clang -Wformat warning
@ 2022-07-18 11:54   ` Kalle Valo
  0 siblings, 0 replies; 12+ messages in thread
From: Kalle Valo @ 2022-07-18 11:54 UTC (permalink / raw)
  To: Justin Stitt
  Cc: Jakub Kicinski, David S . Miller, Eric Dumazet, Paolo Abeni,
	Matthias Brugger, Nathan Chancellor, Nick Desaulniers, Tom Rix,
	linux-wireless, netdev, linux-arm-kernel, linux-mediatek,
	linux-kernel, llvm, Justin Stitt

Justin Stitt <justinstitt@google.com> wrote:

> When building with Clang we encounter this warning:
> | drivers/net/wireless/mediatek/mt7601u/debugfs.c:92:6: error: format
> | specifies type 'unsigned char' but the argument has type 'int'
> | [-Werror,-Wformat] dev->ee->reg.start + dev->ee->reg.num - 1);
> 
> The format specifier used is `%hhu` which describes a u8. Both
> `dev->ee->reg.start` and `.num` are u8 as well. However, the expression
> as a whole is promoted to an int as you cannot get smaller-than-int from
> addition. Therefore, to fix the warning, use the promoted-to-type's
> format specifier -- in this case `%d`.
> 
> example:
> ```
> uint8_t a = 4, b = 7;
> int size = sizeof(a + b - 1);
> printf("%d\n", size);
> // output: 4
> ```
> 
> See more:
> (https://wiki.sei.cmu.edu/confluence/display/c/INT02-C.+Understand+integer+conversion+rules)
> "Integer types smaller than int are promoted when an operation is
> performed on them. If all values of the original type can be represented
> as an int, the value of the smaller type is converted to an int;
> otherwise, it is converted to an unsigned int."
> 
> Signed-off-by: Justin Stitt <justinstitt@google.com>
> Acked-by: Jakub Kicinski <kubakici@wp.pl>

Patch applied to wireless-next.git, thanks.

68204a696505 wifi: mt7601u: fix clang -Wformat warning

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/20220711212932.1501592-1-justinstitt@google.com/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-07-18 11:56 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-11 21:29 [PATCH] mediatek: mt7601u: fix clang -Wformat warning Justin Stitt
2022-07-11 21:29 ` Justin Stitt
2022-07-11 22:16 ` Jakub Kicinski
2022-07-11 22:16   ` Jakub Kicinski
2022-07-15  6:32 ` Joe Perches
2022-07-15  6:32   ` Joe Perches
2022-07-16  0:08   ` Justin Stitt
2022-07-16  0:08     ` Justin Stitt
2022-07-16  1:40     ` Joe Perches
2022-07-16  1:40       ` Joe Perches
2022-07-18 11:54 ` wifi: " Kalle Valo
2022-07-18 11:54   ` Kalle Valo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.