Re: [PATCH v2] i2c: smbus: make i2c_smbus_read_block_data() safer

From: Dmitry Torokhov

Date: Mon Sep 28 2026 - 01:44:03 EST


On Tue, Jul 28, 2026 at 11:00:23PM -0700, Dmitry Torokhov wrote:
> i2c_smbus_read_block_data() is dangerous to use because it may deliver
> up to I2C_SMBUS_BLOCK_MAX (32) bytes, which may be surprising to the
> caller. Callers tend to allocate buffers of sizes big enough to hold
> data from a well-behaving device and do not expect that
> i2c_smbus_read_block_data() may attempt to write more data than
> expected.
>
> To make i2c_smbus_read_block_data() safer to use, change it so that
> it accepts size of the supplied buffer as another argument and ensure
> that it will not copy more data than the size of the buffer. Signal
> oversized responses with -EMSGSIZE.
>
> To allow users to gradually transition to the new API employ some
> macro trickery allowing calling i2c_smbus_read_block_data() with either
> 3 or 4 arguments. When called with 3 arguments it is assumed that
> the buffer size is I2C_SMBUS_BLOCK_MAX bytes. Once everyone is
> transitioned to the 4 argument form the macros should be removed.
>
> Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx>
> ---
>
> v2:
> - dropped incorrect changes to Documentation (Andi)
> - signal oversized responses with -EMSGSIZE (Andi)

Andi, could this be applied or would you like me to change something
else?

Thanks.

--
Dmitry