Skip to contentSkip to conversation

pci: Fix unreasonable return value check

PATCH In review Mao Zhongyi 3 participants 4 messages May 31, 2017 – Jun 1, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pci: Fix unreasonable return value check

The return value of pci_add_capability2() is only 2 cases, positive on success, nagetive on failure and set error message to Error. In other worlds, If Error is filled, the return value must be nagetive. There is no case where errp is set but the return value is a positive. But pci_add_capability() does. So the return value check is illogical.

Meanwhile, all other callers of pci_add_capability2() do the same check as this patch. So fix it.

---
 hw/pci/pci.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
hw/pci/pci.c +1 -5
index 259483b..1faf060 100644
@@ -2269,12 +2269,8 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
     Error *local_err = NULL;
 
     ret = pci_add_capability2(pdev, cap_id, offset, size, &local_err);
-    if (local_err) {
-        assert(ret < 0);
+    if (ret < 0) {
         error_report_err(local_err);
-    } else {
-        /* success implies a positive offset in config space */
-        assert(ret > 0);
     }
     return ret;
 }
Signature · 1 line
2.9.3
#1 lore raw

This is cleanup, not a bug fix, so:

pci: Clean up error checking in pci_add_capability()

Quoted 2 lines · Mao Zhongyi

The return value of pci_add_capability2() is only 2 cases, positive on success, nagetive on failure and set error message to Error. In

negative

Quoted 1 line

other worlds, If Error is filled, the return value must be nagetive.

words, if

Quoted 2 lines

There is no case where errp is set but the return value is a positive. But pci_add_capability() does. So the return value check is illogical.

pci_add_capability2() could use a function comment explaining its return value. Not this patch's job.

Quoted 2 lines

Meanwhile, all other callers of pci_add_capability2() do the same check as this patch. So fix it.

Suggest:

pci: Clean up error checking in pci_add_capability()

On success, pci_add_capability2() returns a positive value. On failure, it sets an error and returns a negative value.

pci_add_capability() laboriously checks this behavior. No other caller does. Drop the checks from pci_add_capability().

Quoted 13 lines
---
 hw/pci/pci.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
hw/pci/pci.c +0 -0
index 259483b..1faf060 100644
@@ -2269,12 +2269,8 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
     Error *local_err = NULL;
 
     ret = pci_add_capability2(pdev, cap_id, offset, size, &local_err);

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, · line 2276

Quoted 10 lines
hw/pci/pci.c +1 -5
-    if (local_err) {
-        assert(ret < 0);
+    if (ret < 0) {
         error_report_err(local_err);
-    } else {
-        /* success implies a positive offset in config space */
-        assert(ret > 0);
     }
     return ret;
 }

Many functions return distinct error values in addition to setting an error. We usually check one of the two, and assume the other is sane. This is one of the few places where we assert it is. Not wrong, just cumbersome. I'd prefer to drop the assertions, i.e. take this patch. But it's up to the PCI maintainers.

#2 lore raw

Hi, Markus

Quoted 13 lines · Markus Armbruster

This is cleanup, not a bug fix, so:

pci: Clean up error checking in pci_add_capability()

Quoted 2 lines · Mao Zhongyi

The return value of pci_add_capability2() is only 2 cases, positive on success, nagetive on failure and set error message to Error. In

negative

Quoted 1 line

other worlds, If Error is filled, the return value must be nagetive.

Quoted 8 lines

words, if

Quoted 2 lines

There is no case where errp is set but the return value is a positive. But pci_add_capability() does. So the return value check is illogical.

pci_add_capability2() could use a function comment explaining its return value. Not this patch's job.

Thanks, will make a separated patch to explain it.

Quoted 5 lines
Quoted 2 lines

Meanwhile, all other callers of pci_add_capability2() do the same check as this patch. So fix it.

Suggest:

Quoted 7 lines

pci: Clean up error checking in pci_add_capability()

On success, pci_add_capability2() returns a positive value. On failure, it sets an error and returns a negative value.

pci_add_capability() laboriously checks this behavior. No other caller does. Drop the checks from pci_add_capability().

Thanks for your perfect suggestion.

Quoted 22 lines
Quoted 22 lines
---
 hw/pci/pci.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
hw/pci/pci.c +1 -5
index 259483b..1faf060 100644
@@ -2269,12 +2269,8 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
     Error *local_err = NULL;
 
     ret = pci_add_capability2(pdev, cap_id, offset, size, &local_err);
-    if (local_err) {
-        assert(ret < 0);
+    if (ret < 0) {
         error_report_err(local_err);
-    } else {
-        /* success implies a positive offset in config space */
-        assert(ret > 0);
     }
Quoted 8 lines

hw/pci/pci.c int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, · line 2276

Quoted 2 lines
hw/pci/pci.c +0 -0
     return ret;
 }

Many functions return distinct error values in addition to setting an error. We usually check one of the two, and assume the other is sane. This is one of the few places where we assert it is. Not wrong, just cumbersome. I'd prefer to drop the assertions, i.e. take this patch. But it's up to the PCI maintainers.

Yes, I also think it really is not necessary. Keeping code as simple as practical is desirable. So drop the assertions. Of course, I will listen to the views of Marcel and Michael.

Thanks
Mao

Reviewed-by
#3 lore raw
Quoted 74 lines · Mao Zhongyi

Hi, Markus

Quoted 13 lines · Markus Armbruster

This is cleanup, not a bug fix, so:

pci: Clean up error checking in pci_add_capability()

Quoted 2 lines · Mao Zhongyi

The return value of pci_add_capability2() is only 2 cases, positive on success, nagetive on failure and set error message to Error. In

negative

Quoted 1 line

other worlds, If Error is filled, the return value must be nagetive.

Quoted 8 lines

words, if

Quoted 2 lines

There is no case where errp is set but the return value is a positive. But pci_add_capability() does. So the return value check is illogical.

pci_add_capability2() could use a function comment explaining its return value. Not this patch's job.

Thanks, will make a separated patch to explain it.

Quoted 5 lines
Quoted 2 lines

Meanwhile, all other callers of pci_add_capability2() do the same check as this patch. So fix it.

Suggest:

Quoted 7 lines

pci: Clean up error checking in pci_add_capability()

On success, pci_add_capability2() returns a positive value. On failure, it sets an error and returns a negative value.

pci_add_capability() laboriously checks this behavior. No other caller does. Drop the checks from pci_add_capability().

Thanks for your perfect suggestion.

Quoted 23 lines
Quoted 23 lines
---
 hw/pci/pci.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
hw/pci/pci.c +1 -5
index 259483b..1faf060 100644
@@ -2269,12 +2269,8 @@ int pci_add_capability(PCIDevice *pdev, 
 uint8_t cap_id,
     Error *local_err = NULL;
 
     ret = pci_add_capability2(pdev, cap_id, offset, size, &local_err);
-    if (local_err) {
-        assert(ret < 0);
+    if (ret < 0) {
         error_report_err(local_err);
-    } else {
-        /* success implies a positive offset in config space */
-        assert(ret > 0);
     }
Quoted 8 lines
Quoted 2 lines
hw/pci/pci.c +0 -0
     return ret;

}

Many functions return distinct error values in addition to setting an error. We usually check one of the two, and assume the other is sane. This is one of the few places where we assert it is. Not wrong, just cumbersome. I'd prefer to drop the assertions, i.e. take this patch. But it's up to the PCI maintainers.

Quoted 4 lines

Yes, I also think it really is not necessary. Keeping code as simple as practical is desirable. So drop the assertions. Of course, I will listen to the views of Marcel and Michael.

I have nothing against it.
Thanks for the patch!

With Markus comments:

Thanks,
Marcel

Quoted 5 lines

Thanks
Mao