2022-07-02 00:07:51

by Justin Stitt

[permalink] [raw]
Subject: [PATCH] eeprom: idt_89hpesx: fix clang -Wformat warnings

see warnings:
| drivers/misc/eeprom/idt_89hpesx.c:570:5: error: format specifies type
| 'unsigned char' but the argument has type 'u16' (aka 'unsigned short')
| [-Werror,-Wformat] memaddr);
-
| drivers/misc/eeprom/idt_89hpesx.c:579:5: error: format specifies type
| 'unsigned char' but the argument has type 'u16' (aka 'unsigned short')
| [-Werror,-Wformat] memaddr);
-
| drivers/misc/eeprom/idt_89hpesx.c:814:4: error: format specifies type
| 'unsigned short' but the argument has type 'unsigned int'
| [-Werror,-Wformat] CSR_REAL_ADDR(csraddr));

There's an ongoing movement to eventually enable the -Wformat flag for
clang. See: https://github.com/ClangBuiltLinux/linux/issues/378

The format specifier for idt_89hpesx.c:570 and 579 was `0x%02hhx`. The
part we care about `%hhx` describes a single byte format, wherein the
leftmost byte of our u16 type (of which memaddr is) is truncated.

example:
```
uint16_t x = 0xbabe;
printf("%hhx\n", x);
// output is: be
// we lost 'ba'
```

There exists a similar issue at idt_89hpesx.c:814 which involves the
CSR_REAL_ADDR macro. This macro returns a u16 but due to default
argument promotion for variadic functions (printf-like) actually
provides an int to the dev_err method.

My proposed solution is to expand the width of the format specifier to
fully encompass the provided argument (which is promoted to an int, see
below). I opted for '%x' as this specifies an unsigned hexadecimal
integer which, with a guarantee, can represent all the values of a u16.

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.`

Link: https://github.com/ClangBuiltLinux/linux/issues/378
Signed-off-by: Justin Stitt <[email protected]>
---
drivers/misc/eeprom/idt_89hpesx.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/misc/eeprom/idt_89hpesx.c b/drivers/misc/eeprom/idt_89hpesx.c
index b0cff4b152da..42eac114edd7 100644
--- a/drivers/misc/eeprom/idt_89hpesx.c
+++ b/drivers/misc/eeprom/idt_89hpesx.c
@@ -566,7 +566,7 @@ static int idt_eeprom_read_byte(struct idt_89hpesx_dev *pdev, u16 memaddr,
eeseq.memaddr = cpu_to_le16(memaddr);
ret = pdev->smb_write(pdev, &smbseq);
if (ret != 0) {
- dev_err(dev, "Failed to init eeprom addr 0x%02hhx",
+ dev_err(dev, "Failed to init eeprom addr 0x%02x",
memaddr);
break;
}
@@ -575,7 +575,7 @@ static int idt_eeprom_read_byte(struct idt_89hpesx_dev *pdev, u16 memaddr,
smbseq.bytecnt = EEPROM_RD_CNT;
ret = pdev->smb_read(pdev, &smbseq);
if (ret != 0) {
- dev_err(dev, "Failed to read eeprom data 0x%02hhx",
+ dev_err(dev, "Failed to read eeprom data 0x%02x",
memaddr);
break;
}
@@ -810,7 +810,7 @@ static int idt_csr_read(struct idt_89hpesx_dev *pdev, u16 csraddr, u32 *data)
smbseq.bytecnt = CSR_RD_CNT;
ret = pdev->smb_read(pdev, &smbseq);
if (ret != 0) {
- dev_err(dev, "Failed to read csr 0x%04hx",
+ dev_err(dev, "Failed to read csr 0x%04x",
CSR_REAL_ADDR(csraddr));
goto err_mutex_unlock;
}
--
2.37.0.rc0.161.g10f37bed90-goog


2022-07-07 18:18:59

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH] eeprom: idt_89hpesx: fix clang -Wformat warnings

On Fri, Jul 01, 2022 at 04:20:31PM -0700, Justin Stitt wrote:
> see warnings:
> | drivers/misc/eeprom/idt_89hpesx.c:570:5: error: format specifies type
> | 'unsigned char' but the argument has type 'u16' (aka 'unsigned short')
> | [-Werror,-Wformat] memaddr);
> -
> | drivers/misc/eeprom/idt_89hpesx.c:579:5: error: format specifies type
> | 'unsigned char' but the argument has type 'u16' (aka 'unsigned short')
> | [-Werror,-Wformat] memaddr);
> -
> | drivers/misc/eeprom/idt_89hpesx.c:814:4: error: format specifies type
> | 'unsigned short' but the argument has type 'unsigned int'
> | [-Werror,-Wformat] CSR_REAL_ADDR(csraddr));
>
> There's an ongoing movement to eventually enable the -Wformat flag for
> clang. See: https://github.com/ClangBuiltLinux/linux/issues/378
>
> The format specifier for idt_89hpesx.c:570 and 579 was `0x%02hhx`. The
> part we care about `%hhx` describes a single byte format, wherein the
> leftmost byte of our u16 type (of which memaddr is) is truncated.
>
> example:
> ```
> uint16_t x = 0xbabe;
> printf("%hhx\n", x);
> // output is: be
> // we lost 'ba'
> ```
>
> There exists a similar issue at idt_89hpesx.c:814 which involves the
> CSR_REAL_ADDR macro. This macro returns a u16 but due to default
> argument promotion for variadic functions (printf-like) actually
> provides an int to the dev_err method.
>
> My proposed solution is to expand the width of the format specifier to
> fully encompass the provided argument (which is promoted to an int, see
> below). I opted for '%x' as this specifies an unsigned hexadecimal
> integer which, with a guarantee, can represent all the values of a u16.

Additionally, this solution is in line with printk-formats.rst after
commit cbacb5ab0aa0 ("docs: printk-formats: Stop encouraging use of
unnecessary %h[xudi] and %hh[xudi]").

> 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.`
>
> Link: https://github.com/ClangBuiltLinux/linux/issues/378
> Signed-off-by: Justin Stitt <[email protected]>

Reviewed-by: Nathan Chancellor <[email protected]>

> ---
> drivers/misc/eeprom/idt_89hpesx.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/misc/eeprom/idt_89hpesx.c b/drivers/misc/eeprom/idt_89hpesx.c
> index b0cff4b152da..42eac114edd7 100644
> --- a/drivers/misc/eeprom/idt_89hpesx.c
> +++ b/drivers/misc/eeprom/idt_89hpesx.c
> @@ -566,7 +566,7 @@ static int idt_eeprom_read_byte(struct idt_89hpesx_dev *pdev, u16 memaddr,
> eeseq.memaddr = cpu_to_le16(memaddr);
> ret = pdev->smb_write(pdev, &smbseq);
> if (ret != 0) {
> - dev_err(dev, "Failed to init eeprom addr 0x%02hhx",
> + dev_err(dev, "Failed to init eeprom addr 0x%02x",
> memaddr);
> break;
> }
> @@ -575,7 +575,7 @@ static int idt_eeprom_read_byte(struct idt_89hpesx_dev *pdev, u16 memaddr,
> smbseq.bytecnt = EEPROM_RD_CNT;
> ret = pdev->smb_read(pdev, &smbseq);
> if (ret != 0) {
> - dev_err(dev, "Failed to read eeprom data 0x%02hhx",
> + dev_err(dev, "Failed to read eeprom data 0x%02x",
> memaddr);
> break;
> }
> @@ -810,7 +810,7 @@ static int idt_csr_read(struct idt_89hpesx_dev *pdev, u16 csraddr, u32 *data)
> smbseq.bytecnt = CSR_RD_CNT;
> ret = pdev->smb_read(pdev, &smbseq);
> if (ret != 0) {
> - dev_err(dev, "Failed to read csr 0x%04hx",
> + dev_err(dev, "Failed to read csr 0x%04x",
> CSR_REAL_ADDR(csraddr));
> goto err_mutex_unlock;
> }
> --
> 2.37.0.rc0.161.g10f37bed90-goog
>