* Coverity CIDs 138749, 138750: cnb20le_res() unintended sign extension
@ 2014-04-03 20:56 Bjorn Helgaas
2014-04-04 0:02 ` Yinghai Lu
2014-04-04 15:40 ` Bjorn Helgaas
0 siblings, 2 replies; 4+ messages in thread
From: Bjorn Helgaas @ 2014-04-03 20:56 UTC (permalink / raw)
To: Ira W. Snyder; +Cc: Yinghai Lu, linux-pci
Coverity complains about unintended sign extension in cnb20le_res() in
arch/x86/pci/broadcom_bus.c here:
60 word1 = read_pci_config_16(bus, slot, func, 0xc4);
61 word2 = read_pci_config_16(bus, slot, func, 0xc6);
62 if (word1 != word2) {
CID 138749 (#1 of 2): Unintended sign extension (SIGN_EXTENSION)
sign_extension: Suspicious implicit sign extension: word1 with type
unsigned short (16 bits, unsigned) is promoted in (word1 << 16) | 0 to
type int (32 bits, signed), then sign-extended to type unsigned long
long (64 bits, unsigned). If (word1 << 16) | 0 is greater than
0x7FFFFFFF, the upper bits of the result will all be 1.
63 res.start = (word1 << 16) | 0x0000;
CID 138750: Unintended sign extension (SIGN_EXTENSION) [select issue]
64 res.end = (word2 << 16) | 0xffff;
65 res.flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
66 update_res(info, res.start, res.end, res.flags, 0);
67 }
Bjorn
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Coverity CIDs 138749, 138750: cnb20le_res() unintended sign extension
2014-04-03 20:56 Coverity CIDs 138749, 138750: cnb20le_res() unintended sign extension Bjorn Helgaas
@ 2014-04-04 0:02 ` Yinghai Lu
2014-04-04 15:40 ` Bjorn Helgaas
1 sibling, 0 replies; 4+ messages in thread
From: Yinghai Lu @ 2014-04-04 0:02 UTC (permalink / raw)
To: Bjorn Helgaas; +Cc: Ira W. Snyder, linux-pci
On Thu, Apr 3, 2014 at 1:56 PM, Bjorn Helgaas <bhelgaas@google.com> wrote:
> Coverity complains about unintended sign extension in cnb20le_res() in
> arch/x86/pci/broadcom_bus.c here:
>
> 60 word1 = read_pci_config_16(bus, slot, func, 0xc4);
> 61 word2 = read_pci_config_16(bus, slot, func, 0xc6);
> 62 if (word1 != word2) {
>
> CID 138749 (#1 of 2): Unintended sign extension (SIGN_EXTENSION)
> sign_extension: Suspicious implicit sign extension: word1 with type
> unsigned short (16 bits, unsigned) is promoted in (word1 << 16) | 0 to
> type int (32 bits, signed), then sign-extended to type unsigned long
> long (64 bits, unsigned). If (word1 << 16) | 0 is greater than
> 0x7FFFFFFF, the upper bits of the result will all be 1.
> 63 res.start = (word1 << 16) | 0x0000;
> CID 138750: Unintended sign extension (SIGN_EXTENSION) [select issue]
> 64 res.end = (word2 << 16) | 0xffff;
> 65 res.flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
> 66 update_res(info, res.start, res.end, res.flags, 0);
> 67 }
could just add (resource_size_t) cast for word1 word2.
Thanks
Yinghai
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Coverity CIDs 138749, 138750: cnb20le_res() unintended sign extension
2014-04-03 20:56 Coverity CIDs 138749, 138750: cnb20le_res() unintended sign extension Bjorn Helgaas
2014-04-04 0:02 ` Yinghai Lu
@ 2014-04-04 15:40 ` Bjorn Helgaas
2014-04-25 17:02 ` Bjorn Helgaas
1 sibling, 1 reply; 4+ messages in thread
From: Bjorn Helgaas @ 2014-04-04 15:40 UTC (permalink / raw)
To: Ira W. Snyder; +Cc: Yinghai Lu, linux-pci
On Thu, Apr 03, 2014 at 02:56:26PM -0600, Bjorn Helgaas wrote:
> Coverity complains about unintended sign extension in cnb20le_res() in
> arch/x86/pci/broadcom_bus.c here:
>
> 60 word1 = read_pci_config_16(bus, slot, func, 0xc4);
> 61 word2 = read_pci_config_16(bus, slot, func, 0xc6);
> 62 if (word1 != word2) {
>
> CID 138749 (#1 of 2): Unintended sign extension (SIGN_EXTENSION)
> sign_extension: Suspicious implicit sign extension: word1 with type
> unsigned short (16 bits, unsigned) is promoted in (word1 << 16) | 0 to
> type int (32 bits, signed), then sign-extended to type unsigned long
> long (64 bits, unsigned). If (word1 << 16) | 0 is greater than
> 0x7FFFFFFF, the upper bits of the result will all be 1.
> 63 res.start = (word1 << 16) | 0x0000;
> CID 138750: Unintended sign extension (SIGN_EXTENSION) [select issue]
I propose the following patch for this. Unless there's objection, I'll
queue this for v3.16.
x86/PCI: Fix Broadcom CNB20LE unintended sign extension
From: Bjorn Helgaas <bhelgaas@google.com>
In the expression "word1 << 16", word1 starts as u16, but is promoted to
a signed int, then sign-extended to resource_size_t, which is probably
not what was intended. Cast to resource_size_t to avoid the sign
extension.
Found by Coverity (CID 138749, 138750).
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
---
arch/x86/pci/broadcom_bus.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/x86/pci/broadcom_bus.c b/arch/x86/pci/broadcom_bus.c
index 614392ced7d6..bb461cfd01ab 100644
--- a/arch/x86/pci/broadcom_bus.c
+++ b/arch/x86/pci/broadcom_bus.c
@@ -60,8 +60,8 @@ static void __init cnb20le_res(u8 bus, u8 slot, u8 func)
word1 = read_pci_config_16(bus, slot, func, 0xc4);
word2 = read_pci_config_16(bus, slot, func, 0xc6);
if (word1 != word2) {
- res.start = (word1 << 16) | 0x0000;
- res.end = (word2 << 16) | 0xffff;
+ res.start = ((resource_size_t) word1 << 16) | 0x0000;
+ res.end = ((resource_size_t) word2 << 16) | 0xffff;
res.flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
update_res(info, res.start, res.end, res.flags, 0);
}
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: Coverity CIDs 138749, 138750: cnb20le_res() unintended sign extension
2014-04-04 15:40 ` Bjorn Helgaas
@ 2014-04-25 17:02 ` Bjorn Helgaas
0 siblings, 0 replies; 4+ messages in thread
From: Bjorn Helgaas @ 2014-04-25 17:02 UTC (permalink / raw)
To: Ira W. Snyder; +Cc: Yinghai Lu, linux-pci
On Fri, Apr 04, 2014 at 09:40:23AM -0600, Bjorn Helgaas wrote:
> On Thu, Apr 03, 2014 at 02:56:26PM -0600, Bjorn Helgaas wrote:
> > Coverity complains about unintended sign extension in cnb20le_res() in
> > arch/x86/pci/broadcom_bus.c here:
> >
> > 60 word1 = read_pci_config_16(bus, slot, func, 0xc4);
> > 61 word2 = read_pci_config_16(bus, slot, func, 0xc6);
> > 62 if (word1 != word2) {
> >
> > CID 138749 (#1 of 2): Unintended sign extension (SIGN_EXTENSION)
> > sign_extension: Suspicious implicit sign extension: word1 with type
> > unsigned short (16 bits, unsigned) is promoted in (word1 << 16) | 0 to
> > type int (32 bits, signed), then sign-extended to type unsigned long
> > long (64 bits, unsigned). If (word1 << 16) | 0 is greater than
> > 0x7FFFFFFF, the upper bits of the result will all be 1.
> > 63 res.start = (word1 << 16) | 0x0000;
> > CID 138750: Unintended sign extension (SIGN_EXTENSION) [select issue]
>
> I propose the following patch for this. Unless there's objection, I'll
> queue this for v3.16.
>
>
> x86/PCI: Fix Broadcom CNB20LE unintended sign extension
>
> From: Bjorn Helgaas <bhelgaas@google.com>
>
> In the expression "word1 << 16", word1 starts as u16, but is promoted to
> a signed int, then sign-extended to resource_size_t, which is probably
> not what was intended. Cast to resource_size_t to avoid the sign
> extension.
>
> Found by Coverity (CID 138749, 138750).
>
> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
I applied this to pci/resource for v3.16.
> ---
> arch/x86/pci/broadcom_bus.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/x86/pci/broadcom_bus.c b/arch/x86/pci/broadcom_bus.c
> index 614392ced7d6..bb461cfd01ab 100644
> --- a/arch/x86/pci/broadcom_bus.c
> +++ b/arch/x86/pci/broadcom_bus.c
> @@ -60,8 +60,8 @@ static void __init cnb20le_res(u8 bus, u8 slot, u8 func)
> word1 = read_pci_config_16(bus, slot, func, 0xc4);
> word2 = read_pci_config_16(bus, slot, func, 0xc6);
> if (word1 != word2) {
> - res.start = (word1 << 16) | 0x0000;
> - res.end = (word2 << 16) | 0xffff;
> + res.start = ((resource_size_t) word1 << 16) | 0x0000;
> + res.end = ((resource_size_t) word2 << 16) | 0xffff;
> res.flags = IORESOURCE_MEM | IORESOURCE_PREFETCH;
> update_res(info, res.start, res.end, res.flags, 0);
> }
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2014-04-25 17:02 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-04-03 20:56 Coverity CIDs 138749, 138750: cnb20le_res() unintended sign extension Bjorn Helgaas
2014-04-04 0:02 ` Yinghai Lu
2014-04-04 15:40 ` Bjorn Helgaas
2014-04-25 17:02 ` Bjorn Helgaas
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.