Skip to contentSkip to conversation

pciutils: Fix sysfs_get_string to handle invalid read

PATCH In review Ameya Palande 2 participants 3 messages Aug 24, 2015

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

PATCH 1/1 pciutils: Fix sysfs_get_string to handle invalid read

- Fix minor indentation alignment

---
 lib/sysfs.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
lib/sysfs.c +14 -8
index 9f348bb..90ed013 100644
@@ -99,15 +99,21 @@ sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory)
   if (fd < 0)
     {
       if (mandatory)
-	a->error("Cannot open %s: %s", namebuf, strerror(errno));
+        a->error("Cannot open %s: %s", namebuf, strerror(errno));
       return 0;
     }
   n = read(fd, buf, OBJBUFSIZE);
   close(fd);
-  if (n < 0)
-    a->error("Error reading %s: %s", namebuf, strerror(errno));
-  if (n >= OBJBUFSIZE)
-    a->error("Value in %s too long", namebuf);
+  if (n < 0) {
+    if (mandatory)
+      a->error("Error reading %s: %s", namebuf, strerror(errno));
+    return 0;
+  }
+  if (n >= OBJBUFSIZE) {
+    if (mandatory)
+      a->error("Value in %s too long", namebuf);
+    return 0;
+  }
   buf[n] = 0;
   return 1;
 }
@@ -274,21 +280,21 @@ sysfs_fill_info(struct pci_dev *d, int flags)
       struct pci_dev *pd;
       sysfs_fill_slots(d->access);
       for (pd = d->access->devices; pd; pd = pd->next)
-	pd->known_fields |= PCI_FILL_PHYS_SLOT;
+        pd->known_fields |= PCI_FILL_PHYS_SLOT;
     }
 
   if ((flags & PCI_FILL_MODULE_ALIAS) && !(d->known_fields & PCI_FILL_MODULE_ALIAS))
     {
       char buf[OBJBUFSIZE];
       if (sysfs_get_string(d, "modalias", buf, 0))
-	d->module_alias = pci_strdup(d->access, buf);
+        d->module_alias = pci_strdup(d->access, buf);
     }
 
   if ((flags & PCI_FILL_LABEL) && !(d->known_fields & PCI_FILL_LABEL))
     {
       char buf[OBJBUFSIZE];
       if (sysfs_get_string(d, "label", buf, 0))
-	d->label = pci_strdup(d->access, buf);
+        d->label = pci_strdup(d->access, buf);
     }
 
   return pci_generic_fill_info(d, flags);
Signature · 1 line
1.8.3.1
#1 lore raw

Hello!

Quoted 1 line

- Fix minor indentation alignment

I am not sure what you intend to do. Your indentation fixes do not seem to fix anything -- maybe you have a non-standard tab size?

Martin

#2 lore raw

PATCH 1/1 pciutils: Fix sysfs_get_string to handle invalid read

---
 lib/sysfs.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
lib/sysfs.c +10 -2
index 9f348bb..8f0cabe 100644
@@ -105,9 +105,17 @@ sysfs_get_string(struct pci_dev *d, char *object, char *buf, int mandatory)
   n = read(fd, buf, OBJBUFSIZE);
   close(fd);
   if (n < 0)
-    a->error("Error reading %s: %s", namebuf, strerror(errno));
+    {
+      if (mandatory)
+        a->error("Error reading %s: %s", namebuf, strerror(errno));
+      return 0;
+    }
   if (n >= OBJBUFSIZE)
-    a->error("Value in %s too long", namebuf);
+    {
+      if (mandatory)
+        a->error("Value in %s too long", namebuf);
+      return 0;
+    }
   buf[n] = 0;
   return 1;
 }
Signature · 1 line
1.8.3.1