Skip to contentSkip to conversation

i2c: amd_mp2: handle num is 0 input for i2c_amd_xfer

PATCH Applied [email protected] 3 participants 4 messages Sep 4, 2020 – Sep 21, 2020

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

PATCH 1/1 i2c: amd_mp2: handle num is 0 input for i2c_amd_xfer

From: Tom Rix <[email protected]>

clang static analyzer reports this problem

i2c-amd-mp2-plat.c:174:9: warning: Branch condition evaluates
to a garbage value

        return err ? err : num;
               ^~~

err is not initialized, it depends on the being set in the transfer loop which will not happen if num is 0. Surveying other master_xfer() implementations show all handle a 0 num.

Because returning 0 is expected, initialize err to 0.

---
 drivers/i2c/busses/i2c-amd-mp2-plat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-amd-mp2-plat.c +1 -1
index 17df9e8845b6..506433bc0ff2 100644
@@ -155,7 +155,7 @@ static int i2c_amd_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 	struct amd_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
 	int i;
 	struct i2c_msg *pmsg;
-	int err;
+	int err = 0;
 
 	/* the adapter might have been deleted while waiting for the bus lock */
 	if (unlikely(!i2c_dev->common.mp2_dev))
Signature · 1 line
2.18.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 37 lines · Le ven. 4 sept. 2020 à 20:06, <[email protected]> a écrit :

From: Tom Rix <[email protected]>

clang static analyzer reports this problem

i2c-amd-mp2-plat.c:174:9: warning: Branch condition evaluates
to a garbage value

        return err ? err : num;
               ^~~

err is not initialized, it depends on the being set in the transfer loop which will not happen if num is 0. Surveying other master_xfer() implementations show all handle a 0 num.

Because returning 0 is expected, initialize err to 0.

---
 drivers/i2c/busses/i2c-amd-mp2-plat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-amd-mp2-plat.c +1 -2
index 17df9e8845b6..506433bc0ff2 100644
@@ -155,7 +155,7 @@ static int i2c_amd_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
        struct amd_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
        int i;
        struct i2c_msg *pmsg;
-       int err;
+       int err = 0;
 
        /* the adapter might have been deleted while waiting for the bus lock */
        if (unlikely(!i2c_dev->common.mp2_dev))
--

2.18.1

#2 lore raw
In reply to [email protected]
Quoted 6 lines · On Fri, Sep 04, 2020 at 11:06:47AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analyzer reports this problem

i2c-amd-mp2-plat.c:174:9: warning: Branch condition evaluates
to a garbage value

Quoted 8 lines
        return err ? err : num;
               ^~~

err is not initialized, it depends on the being set in the transfer loop which will not happen if num is 0. Surveying other master_xfer() implementations show all handle a 0 num.

Because returning 0 is expected, initialize err to 0.

Well, it is not expected. The core does:

2019 if (WARN_ON(!msgs || num < 1))
2020 return -EINVAL;

Dunno if we should apply the patch nonetheless or add a comment that num is guaranteed to be at least 1.

Quoted 22 lines
---
 drivers/i2c/busses/i2c-amd-mp2-plat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-amd-mp2-plat.c +1 -1
index 17df9e8845b6..506433bc0ff2 100644
@@ -155,7 +155,7 @@ static int i2c_amd_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 	struct amd_i2c_dev *i2c_dev = i2c_get_adapdata(adap);
 	int i;
 	struct i2c_msg *pmsg;
-	int err;
+	int err = 0;
 
 	/* the adapter might have been deleted while waiting for the bus lock */
 	if (unlikely(!i2c_dev->common.mp2_dev))
Signature · 1 line
2.18.1
Applied
#3 lore raw
In reply to [email protected]
Quoted 9 lines · On Fri, Sep 04, 2020 at 11:06:47AM -0700, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analyzer reports this problem

i2c-amd-mp2-plat.c:174:9: warning: Branch condition evaluates
to a garbage value

        return err ? err : num;
               ^~~
Quoted 7 lines

err is not initialized, it depends on the being set in the transfer loop which will not happen if num is 0. Surveying other master_xfer() implementations show all handle a 0 num.

Because returning 0 is expected, initialize err to 0.

Applied to for-next, thanks!