Skip to contentSkip to conversation

arch/x86/kernel: Correct NULL test

PATCH Applied Julia Lawall 3 participants 3 messages Feb 6, 2010 – Feb 8, 2010

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

PATCH 3/11 arch/x86/kernel: Correct NULL test

From: Julia Lawall <[email protected]>

dev was tested just above, so drop the second test.

A simplified version of the semantic match that finds this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@r@
expression *x;
expression e;
identifier l;
@@

if (x = NULL || ...) {
... when forall
return ...; }

... when != goto l;
    when != x = e
    when != &x

*x = NULL
// </smpl>

---
 arch/x86/kernel/amd_iommu.c         |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/amd_iommu.c +1 -1
index adb0ba0..2c4a501 100644
@@ -118,7 +118,7 @@ static bool check_device(struct device *dev)
 		return false;
 
 	/* No device or no PCI device */
-	if (!dev || dev->bus != &pci_bus_type)
+	if (dev->bus != &pci_bus_type)
 		return false;
 
 	devid = get_device_id(dev);
#1 lore raw

Hello Julia,

May be it would be worthy to take your changes one step further and to report if the function is called with a null device pointer (which indicates a problem in the calling code, right?).

Here's a patch moving the null pointer check to its own if block and reporting the null pointer function call using printk based on 2.6.33-rc6 code.

arch/x86/kernel/amd_iommu.c +3 -1
index 23824fe..1a1d87c 100644
@@ -114,11 +114,17 @@ static bool check_device(struct device *dev)
 {
       u16 devid;
 
-       if (!dev || !dev->dma_mask)
+       /* Function is called with a null pointer. Flag this and return false */
+       if (!dev){
+               printk(KERN_INFO "amd_iommu check_device called with a

null pointer device\n");
+ return false;
+ }
+
+ if (!dev->dma_mask)

               return false;

       /* No device or no PCI device */
-       if (!dev || dev->bus != &pci_bus_type)
+       if (dev->bus != &pci_bus_type)
               return false;

       devid = get_device_id(dev);
Quoted 48 lines · Julia Lawall

From: Julia Lawall <[email protected]>

dev was tested just above, so drop the second test.

A simplified version of the semantic match that finds this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@r@
expression *x;
expression e;
identifier l;
@@

if (x = NULL || ...) {
   ... when forall
   return ...; }
... when != goto l;
   when != x = e
   when != &x
*x = NULL
// </smpl>

---
 arch/x86/kernel/amd_iommu.c         |    2 +-

 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/amd_iommu.c +1 -2
index adb0ba0..2c4a501 100644
@@ -118,7 +118,7 @@ static bool check_device(struct device *dev)
                return false;
 
        /* No device or no PCI device */
-       if (!dev || dev->bus != &pci_bus_type)
+       if (dev->bus != &pci_bus_type)
                return false;
 
        devid = get_device_id(dev);
--

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Signature · 1 line
Ludovic