Skip to contentSkip to conversation

staging: greybus: fix fw is NULL but dereferenced

PATCH In review Jian Dong 4 participants 6 messages Mar 25, 2021

Subsystems (3)
lore
Share & tools
lore

Jian Dong

author
#0 lore raw

PATCH 1/1 staging: greybus: fix fw is NULL but dereferenced

From: Jian Dong <[email protected]>

fixes coccicheck Error:

drivers/staging/greybus/bootrom.c:301:41-45: ERROR:
fw is NULL but dereferenced.

if procedure goto label directly, ret will be nefative, so the fw is NULL and the if(condition) end with dereferenced fw. let's fix it.

---
 drivers/staging/greybus/bootrom.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/staging/greybus/bootrom.c +4 -4
index a8efb86..0439efa 100644
@@ -246,7 +246,7 @@ static int gb_bootrom_get_firmware(struct gb_operation *op)
 	struct gb_bootrom_get_firmware_response *firmware_response;
 	struct device *dev = &op->connection->bundle->dev;
 	unsigned int offset, size;
-	enum next_request_type next_request;
+	enum next_request_type next_request = NEXT_REQ_GET_FIRMWARE;
 	int ret = 0;
 
 	/* Disable timeouts */
@@ -298,10 +298,10 @@ static int gb_bootrom_get_firmware(struct gb_operation *op)
 
 queue_work:
 	/* Refresh timeout */
-	if (!ret && (offset + size == fw->size))
-		next_request = NEXT_REQ_READY_TO_BOOT;
-	else
+	if (!!ret)
 		next_request = NEXT_REQ_GET_FIRMWARE;
+	else if (offset + size == fw->size)
+		next_request = NEXT_REQ_READY_TO_BOOT;
 
 	gb_bootrom_set_timeout(bootrom, next_request, NEXT_REQ_TIMEOUT_MS);
 
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 9 lines · Jian Dong

From: Jian Dong <[email protected]>

fixes coccicheck Error:

drivers/staging/greybus/bootrom.c:301:41-45: ERROR:
fw is NULL but dereferenced.

if procedure goto label directly, ret will be nefative, so the fw is NULL and the if(condition) end with dereferenced fw. let's fix it.

Why is this all indented a space?

Quoted 20 lines
---
 drivers/staging/greybus/bootrom.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/staging/greybus/bootrom.c +1 -1
index a8efb86..0439efa 100644
@@ -246,7 +246,7 @@ static int gb_bootrom_get_firmware(struct gb_operation *op)
 	struct gb_bootrom_get_firmware_response *firmware_response;
 	struct device *dev = &op->connection->bundle->dev;
 	unsigned int offset, size;
-	enum next_request_type next_request;
+	enum next_request_type next_request = NEXT_REQ_GET_FIRMWARE;
 	int ret = 0;
 
 	/* Disable timeouts */

drivers/staging/greybus/bootrom.c static int gb_bootrom_get_firmware(struct gb_operation *op) · line 301

Quoted 7 lines
drivers/staging/greybus/bootrom.c +1 -3
@@ -298,10 +298,10 @@ static int gb_bootrom_get_firmware(struct gb_operation *op)
 
 queue_work:
 	/* Refresh timeout */
-	if (!ret && (offset + size == fw->size))
-		next_request = NEXT_REQ_READY_TO_BOOT;
-	else
+	if (!!ret)

That is hard to understand, please make this more obvious.

thanks,

greg k-h

author
#2 lore raw
Quoted 5 lines · On Thu, 25 Mar 2021 11:29:06 +0100 Greg KH <[email protected]> wrote:
Quoted 4 lines · Jian Dong

From: Jian Dong <[email protected]>

fixes coccicheck Error:

Quoted 8 lines
Quoted 6 lines

drivers/staging/greybus/bootrom.c:301:41-45: ERROR:
fw is NULL but dereferenced.

if procedure goto label directly, ret will be nefative, so the fw
is NULL and the if(condition) end with dereferenced fw. let's fix it.

Why is this all indented a space?

this maybe caused by my terminal, I will take notice next time.

Quoted 19 lines
Quoted 19 lines
---
 drivers/staging/greybus/bootrom.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/greybus/bootrom.c index a8efb86..0439efa 100644

drivers/staging/greybus/bootrom.c +0 -0
@@ -246,7 +246,7 @@ static int gb_bootrom_get_firmware(struct

gb_operation *op) struct gb_bootrom_get_firmware_response *firmware_response; struct device *dev = &op->connection->bundle->dev; unsigned int offset, size;

drivers/staging/greybus/bootrom.c +1 -1
-	enum next_request_type next_request;
+	enum next_request_type next_request =

NEXT_REQ_GET_FIRMWARE; int ret = 0;

Quoted 12 lines

drivers/staging/greybus/bootrom.c static int gb_bootrom_get_firmware(struct · line 301

Quoted 9 lines
drivers/staging/greybus/bootrom.c +1 -3
 	/* Disable timeouts */
@@ -298,10 +298,10 @@ static int gb_bootrom_get_firmware(struct
 gb_operation *op) 
 queue_work:
 	/* Refresh timeout */
-	if (!ret && (offset + size == fw->size))
-		next_request = NEXT_REQ_READY_TO_BOOT;
-	else
+	if (!!ret)  

That is hard to understand, please make this more obvious.

if (A && B) else (!A || !B)

So, when ret is NON-ZERO, set next_request as GET_FIRMWARE, else set READ_TO_BOOT. but if second express is flase, next_request still need be set as GET_FIRMWARE, So, I initialze it as GET_FIRMWARE.

this is will keep consistent with the origin conditional express:
both ret is ZERO and second express TRUE, then set as READ_TO_BOOT, else set as GET_FIRMWARE.

Quoted 3 lines

thanks,

greg k-h

maintainer
#3 lore raw
Quoted 48 lines · Jian Dong
Quoted 5 lines · On Thu, 25 Mar 2021 11:29:06 +0100 Greg KH <[email protected]> wrote:
Quoted 4 lines · Jian Dong

From: Jian Dong <[email protected]>

fixes coccicheck Error:

Quoted 8 lines
Quoted 6 lines

drivers/staging/greybus/bootrom.c:301:41-45: ERROR:
fw is NULL but dereferenced.

if procedure goto label directly, ret will be nefative, so the fw
is NULL and the if(condition) end with dereferenced fw. let's fix it.

Why is this all indented a space?

this maybe caused by my terminal, I will take notice next time.

Quoted 19 lines
Quoted 19 lines
---
 drivers/staging/greybus/bootrom.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/staging/greybus/bootrom.c index a8efb86..0439efa 100644

drivers/staging/greybus/bootrom.c +0 -0
@@ -246,7 +246,7 @@ static int gb_bootrom_get_firmware(struct

gb_operation *op) struct gb_bootrom_get_firmware_response *firmware_response; struct device *dev = &op->connection->bundle->dev; unsigned int offset, size;

drivers/staging/greybus/bootrom.c +1 -1
-	enum next_request_type next_request;
+	enum next_request_type next_request =

NEXT_REQ_GET_FIRMWARE; int ret = 0;

Quoted 12 lines

drivers/staging/greybus/bootrom.c static int gb_bootrom_get_firmware(struct · line 301

Quoted 9 lines
drivers/staging/greybus/bootrom.c +1 -3
 	/* Disable timeouts */
@@ -298,10 +298,10 @@ static int gb_bootrom_get_firmware(struct
 gb_operation *op) 
 queue_work:
 	/* Refresh timeout */
-	if (!ret && (offset + size == fw->size))
-		next_request = NEXT_REQ_READY_TO_BOOT;
-	else
+	if (!!ret)  

That is hard to understand, please make this more obvious.

Quoted 5 lines

if (A && B) else (!A || !B)

So, when ret is NON-ZERO, set next_request as GET_FIRMWARE, else set READ_TO_BOOT. but if second express is flase, next_request still need be set as GET_FIRMWARE, So, I initialze it as GET_FIRMWARE.

My point is:

	if (!!ret)

is odd, and is the same thing as:
if (ret)
correct?

And the latter is the common kernel style, no need to be complex when you do not need to.

Anyway, others have pointed out why this is incorrect, no need for further discussion.

thanks,

greg k-h

#4 lore raw
In reply to Jian Dong

The commit description is not clear but this patch doesn't change how the code works, it just silences a static checker false positive.

Just ignore the false positive. Always just ignore static checkers when they are wrong.

regards,
dan carpenter

#5 lore raw
In reply to Jian Dong
Quoted 9 lines · Jian Dong

From: Jian Dong <[email protected]>

fixes coccicheck Error:

drivers/staging/greybus/bootrom.c:301:41-45: ERROR:
fw is NULL but dereferenced.

if procedure goto label directly, ret will be nefative, so the fw is NULL and the if(condition) end with dereferenced fw. let's fix it.

No, fw is accessed only for !ret case.

Quoted 21 lines
---
 drivers/staging/greybus/bootrom.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/staging/greybus/bootrom.c +1 -1
index a8efb86..0439efa 100644
@@ -246,7 +246,7 @@ static int gb_bootrom_get_firmware(struct gb_operation *op)
 	struct gb_bootrom_get_firmware_response *firmware_response;
 	struct device *dev = &op->connection->bundle->dev;
 	unsigned int offset, size;
-	enum next_request_type next_request;
+	enum next_request_type next_request = NEXT_REQ_GET_FIRMWARE;
 	int ret = 0;
 
 	/* Disable timeouts */
@@ -298,10 +298,10 @@ static int gb_bootrom_get_firmware(struct gb_operation *op)
 
 queue_work:

drivers/staging/greybus/bootrom.c static int gb_bootrom_get_firmware(struct gb_operation *op) · line 306

Quoted 10 lines
drivers/staging/greybus/bootrom.c +3 -3
 	/* Refresh timeout */
-	if (!ret && (offset + size == fw->size))
-		next_request = NEXT_REQ_READY_TO_BOOT;
-	else
+	if (!!ret)
 		next_request = NEXT_REQ_GET_FIRMWARE;
+	else if (offset + size == fw->size)
+		next_request = NEXT_REQ_READY_TO_BOOT;
 
 	gb_bootrom_set_timeout(bootrom, next_request, NEXT_REQ_TIMEOUT_MS);

The code is fine AFAICT, the coccicheck is buggy as it is detecting a bug here.

Signature · 1 line
viresh