Skip to content

Commit 1008691

Browse files
RICCIARDI-Adriencfriedt
authored andcommitted
drivers: i2c: i2c_dw: Fixed integer overflow in i2c_dw_data_ask().
The controller can implement a reception FIFO as deep as 256 bytes. However, the computation made by the driver code to determine how many bytes can be asked is stored in a signed 8-bit variable called rx_empty. If the reception FIFO depth is greater or equal to 128 bytes and the FIFO is currently empty, the rx_empty value will be 128 (or more), which stands for a negative value as the variable is signed. Thus, the later code checking if the FIFO is full will run while it should not and exit from the i2c_dw_data_ask() function too early. This hangs the controller in an infinite loop of interrupt storm because the interrupt flags are never cleared. Storing the rx_empty empty on a signed 32-bit variable instead of a 8-bit one solves the issue and is compliant with the controller hardware specifications of a maximum FIFO depth of 256 bytes. It has been agreed with upstream maintainers to change the type of the variables tx_empty, rx_empty, cnt, rx_buffer_depth and tx_buffer_depth to plain int because it is most effectively handled by the CPUs. Using 8-bit or 16-bit variables had no meaning here. Signed-off-by: Adrien Ricciardi <[email protected]> (cherry picked from commit 4824e40)
1 parent 01ad112 commit 1008691

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

drivers/i2c/i2c_dw.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ static inline void i2c_dw_data_ask(const struct device *dev)
4343
{
4444
struct i2c_dw_dev_config * const dw = dev->data;
4545
uint32_t data;
46-
uint8_t tx_empty;
47-
int8_t rx_empty;
48-
uint8_t cnt;
49-
uint8_t rx_buffer_depth, tx_buffer_depth;
46+
int tx_empty;
47+
int rx_empty;
48+
int cnt;
49+
int rx_buffer_depth, tx_buffer_depth;
5050
union ic_comp_param_1_register ic_comp_param_1;
5151
uint32_t reg_base = get_regs(dev);
5252

0 commit comments

Comments
 (0)