diff mbox series

[v7,3/4] i2c: add param sanity check to i2c_transfer()

Message ID 20180403191151.14924-4-ard.biesheuvel@linaro.org
State Accepted
Commit 1eace8344c02c625ee99cc3ffa50187ded2c87b5
Headers show
Series add support for Socionext SynQuacer I2C controller | expand

Commit Message

Ard Biesheuvel April 3, 2018, 7:11 p.m. UTC
The API docs describe i2c_transfer() as taking a pointer to an array
of i2c_msg containing at least 1 entry, but leaves it to the individual
drivers to sanity check the msgs and num parameters. Let's do this in
core code instead.

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

---
 drivers/i2c/i2c-core-base.c | 3 +++
 1 file changed, 3 insertions(+)

-- 
2.15.1

Comments

Wolfram Sang April 4, 2018, 6:38 p.m. UTC | #1
On Tue, Apr 03, 2018 at 09:11:50PM +0200, Ard Biesheuvel wrote:
> The API docs describe i2c_transfer() as taking a pointer to an array

> of i2c_msg containing at least 1 entry, but leaves it to the individual

> drivers to sanity check the msgs and num parameters. Let's do this in

> core code instead.

> 

> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>


Looks good but I want to test it myself, so I will include it in my
second pull request later.
Ard Biesheuvel April 4, 2018, 9:01 p.m. UTC | #2
On 4 April 2018 at 20:38, Wolfram Sang <wsa@the-dreams.de> wrote:
> On Tue, Apr 03, 2018 at 09:11:50PM +0200, Ard Biesheuvel wrote:

>> The API docs describe i2c_transfer() as taking a pointer to an array

>> of i2c_msg containing at least 1 entry, but leaves it to the individual

>> drivers to sanity check the msgs and num parameters. Let's do this in

>> core code instead.

>>

>> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>

>

> Looks good but I want to test it myself, so I will include it in my

> second pull request later.

>


Thanks Wolfram
diff mbox series

Patch

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index c8bfe008f208..e2ca5c331ecb 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -1845,6 +1845,9 @@  int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 	unsigned long orig_jiffies;
 	int ret, try;
 
+	if (WARN_ON(!msgs || num <= 0))
+		return -EINVAL;
+
 	if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
 		return -EOPNOTSUPP;