Skip to contentSkip to conversation

misc/pca9554: Fix check of pin range value in property accessors

PATCH In review Cédric Le Goater 3 participants 5 messages Mar 21, 2024 – Mar 22, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc/pca9554: Fix check of pin range value in property accessors

Coverity detected an "Integer handling" issue with the pin value :

In expression "state >> pin", right shifting "state" by more than 7 bits always yields zero. The shift amount, "pin", is as much as 8.

In practice, this should not happen because the properties "pin8" and above are not created. Nevertheless, fix the range to avoid this warning.

Fixes CID 1534917
Fixes de0c7d543bca ("misc: Add a pca9554 GPIO device model")
Cc Glenn Miles <[email protected]>
---
 hw/misc/pca9554.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/misc/pca9554.c +2 -2
index 778b32e4430a8b618322c26b1b185ed3ead97cc4..5e31696797d9564666ece6fe17737ee2a9733e96 100644
@@ -160,7 +160,7 @@ static void pca9554_get_pin(Object *obj, Visitor *v, const char *name,
         error_setg(errp, "%s: error reading %s", __func__, name);
         return;
     }
-    if (pin < 0 || pin > PCA9554_PIN_COUNT) {
+    if (pin < 0 || pin >= PCA9554_PIN_COUNT) {
         error_setg(errp, "%s invalid pin %s", __func__, name);
         return;
     }
@@ -187,7 +187,7 @@ static void pca9554_set_pin(Object *obj, Visitor *v, const char *name,
         error_setg(errp, "%s: error reading %s", __func__, name);
         return;
     }
-    if (pin < 0 || pin > PCA9554_PIN_COUNT) {
+    if (pin < 0 || pin >= PCA9554_PIN_COUNT) {
         error_setg(errp, "%s invalid pin %s", __func__, name);
         return;
     }
Signature · 1 line
2.44.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 42 lines · Cédric Le Goater

Coverity detected an "Integer handling" issue with the pin value :

In expression "state >> pin", right shifting "state" by more than 7 bits always yields zero. The shift amount, "pin", is as much as 8.

In practice, this should not happen because the properties "pin8" and above are not created. Nevertheless, fix the range to avoid this warning.

Fixes CID 1534917
Fixes de0c7d543bca ("misc: Add a pca9554 GPIO device model")
Cc Glenn Miles <[email protected]>
---
 hw/misc/pca9554.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/misc/pca9554.c +0 -0

index 778b32e4430a8b618322c26b1b185ed3ead97cc4..5e31696797d9564666ece6fe177 37ee2a9733e96 100644

hw/misc/pca9554.c +1 -1
@@ -160,7 +160,7 @@ static void pca9554_get_pin(Object *obj, Visitor
 *v, const char *name,
         error_setg(errp, "%s: error reading %s", __func__, name);
         return;
     }
-    if (pin < 0 || pin > PCA9554_PIN_COUNT) {
+    if (pin < 0 || pin >= PCA9554_PIN_COUNT) {
         error_setg(errp, "%s invalid pin %s", __func__, name);
         return;
     }
hw/misc/pca9554.c +1 -1
@@ -187,7 +187,7 @@ static void pca9554_set_pin(Object *obj, Visitor
 *v, const char *name,
         error_setg(errp, "%s: error reading %s", __func__, name);
         return;
     }
-    if (pin < 0 || pin > PCA9554_PIN_COUNT) {
+    if (pin < 0 || pin >= PCA9554_PIN_COUNT) {
         error_setg(errp, "%s invalid pin %s", __func__, name);
         return;
Quoted 1 line
     }

Thanks, Cédric! I guess I should be running coverity myself.

-Glenn

#2 lore raw
Quoted 38 lines · Miles Glenn
Quoted 37 lines · Cédric Le Goater

Coverity detected an "Integer handling" issue with the pin value :

In expression "state >> pin", right shifting "state" by more than 7 bits always yields zero. The shift amount, "pin", is as much as 8.

In practice, this should not happen because the properties "pin8" and above are not created. Nevertheless, fix the range to avoid this warning.

Fixes CID 1534917
Fixes de0c7d543bca ("misc: Add a pca9554 GPIO device model")
Cc Glenn Miles <[email protected]>
---
  hw/misc/pca9554.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/misc/pca9554.c +0 -0

index 778b32e4430a8b618322c26b1b185ed3ead97cc4..5e31696797d9564666ece6fe177 37ee2a9733e96 100644

hw/misc/pca9554.c +1 -1
@@ -160,7 +160,7 @@ static void pca9554_get_pin(Object *obj, Visitor
 *v, const char *name,
          error_setg(errp, "%s: error reading %s", __func__, name);
          return;
      }
-    if (pin < 0 || pin > PCA9554_PIN_COUNT) {
+    if (pin < 0 || pin >= PCA9554_PIN_COUNT) {
          error_setg(errp, "%s invalid pin %s", __func__, name);
          return;
      }
hw/misc/pca9554.c +0 -0
@@ -187,7 +187,7 @@ static void pca9554_set_pin(Object *obj, Visitor
 *v, const char *name,
          error_setg(errp, "%s: error reading %s", __func__, name);
          return;
Quoted 8 lines
Quoted 6 lines
hw/misc/pca9554.c +1 -1
      }
-    if (pin < 0 || pin > PCA9554_PIN_COUNT) {
+    if (pin < 0 || pin >= PCA9554_PIN_COUNT) {
          error_setg(errp, "%s invalid pin %s", __func__, name);
          return;
      }

Thanks, Cédric! I guess I should be running coverity myself.

I don't myself. I get reports from :

https://scan.coverity.com/projects/qemu

Thanks,

C.

#3 lore raw
In reply to Cédric Le Goater
Quoted 8 lines · Cédric Le Goater

Coverity detected an "Integer handling" issue with the pin value :

In expression "state >> pin", right shifting "state" by more than 7 bits always yields zero. The shift amount, "pin", is as much as 8.

In practice, this should not happen because the properties "pin8" and above are not created. Nevertheless, fix the range to avoid this warning.

Quoted 7 lines
Fixes CID 1534917
Fixes de0c7d543bca ("misc: Add a pca9554 GPIO device model")
Cc Glenn Miles <[email protected]>
---
  hw/misc/pca9554.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

Isn't it the one Peter fixed in
https://lore.kernel.org/qemu-devel/[email protected]/?

#4 lore raw
Quoted 17 lines · Philippe Mathieu-Daudé
Quoted 8 lines · Cédric Le Goater

Coverity detected an "Integer handling" issue with the pin value :

   In expression "state >> pin", right shifting "state" by more than 7 bits always yields zero.  The shift amount, "pin", is as much as 8.

In practice, this should not happen because the properties "pin8" and above are not created. Nevertheless, fix the range to avoid this warning.

Quoted 7 lines
Fixes CID 1534917
Fixes de0c7d543bca ("misc: Add a pca9554 GPIO device model")
Cc Glenn Miles <[email protected]>

---
  hw/misc/pca9554.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 2 lines

Isn't it the one Peter fixed in
https://lore.kernel.org/qemu-devel/[email protected]/?

Oh yes. I missed it. Hopefully, they are similar. Let's keep Peter's.

However, what I would like to do as a follow-up is to move the hw/misc/pca955* models under hw/gpio/. Is it something we can do for 9.0 ?

Thanks,

C.