Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Signed-off-by: Justin Stitt <justinstitt@google.com>
Link: https://lore.kernel.org/r/20220701232031.2639134-1-justinstitt@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Justin Stitt and committed by
Greg Kroah-Hartman
ffff4913 89e1ec77

+3 -3
+3 -3
drivers/misc/eeprom/idt_89hpesx.c
··· 566 566 eeseq.memaddr = cpu_to_le16(memaddr); 567 567 ret = pdev->smb_write(pdev, &smbseq); 568 568 if (ret != 0) { 569 - dev_err(dev, "Failed to init eeprom addr 0x%02hhx", 569 + dev_err(dev, "Failed to init eeprom addr 0x%02x", 570 570 memaddr); 571 571 break; 572 572 } ··· 575 575 smbseq.bytecnt = EEPROM_RD_CNT; 576 576 ret = pdev->smb_read(pdev, &smbseq); 577 577 if (ret != 0) { 578 - dev_err(dev, "Failed to read eeprom data 0x%02hhx", 578 + dev_err(dev, "Failed to read eeprom data 0x%02x", 579 579 memaddr); 580 580 break; 581 581 } ··· 810 810 smbseq.bytecnt = CSR_RD_CNT; 811 811 ret = pdev->smb_read(pdev, &smbseq); 812 812 if (ret != 0) { 813 - dev_err(dev, "Failed to read csr 0x%04hx", 813 + dev_err(dev, "Failed to read csr 0x%04x", 814 814 CSR_REAL_ADDR(csraddr)); 815 815 goto err_mutex_unlock; 816 816 }