Skip to content

Commit 20d5620

Browse files
Dan CarpenterWolfram Sang
authored andcommitted
i2c: atr: Fix end of loop test in i2c_atr_find_mapping_by_addr()
When the list_for_each_entry_reverse() exits without hitting a break then the list cursor points to invalid memory. So this check for if (c2a->fixed) is checking bogus memory. Fix it by using a "found" variable to track if we found what we were looking for or not. The list head (i2c_atr_chan.alias_pairs) is not a full entry, it's just a struct list_head. When the for loop runs to completion, c2a doesn't point to a struct i2c_atr_alias_pair, so you can't access c2a->fixed. Fixes: c3f5524 ("i2c: Support dynamic address translation") Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org> Reviewed-by: Tomi Valkeinen <tomi.valkeinen@ideasonboard.com> Reviewed-by: Romain Gantois <romain.gantois@bootlin.com> Reviewed-by: Luca Ceresoli <luca.ceresoli@bootlin.com> Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
1 parent 0466877 commit 20d5620

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

drivers/i2c/i2c-atr.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
240240
struct i2c_atr *atr = chan->atr;
241241
struct i2c_atr_alias_pair *c2a;
242242
struct list_head *alias_pairs;
243+
bool found = false;
243244
u16 alias;
244245
int ret;
245246

@@ -258,11 +259,14 @@ i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
258259
if (unlikely(list_empty(alias_pairs)))
259260
return NULL;
260261

261-
list_for_each_entry_reverse(c2a, alias_pairs, node)
262-
if (!c2a->fixed)
262+
list_for_each_entry_reverse(c2a, alias_pairs, node) {
263+
if (!c2a->fixed) {
264+
found = true;
263265
break;
266+
}
267+
}
264268

265-
if (c2a->fixed)
269+
if (!found)
266270
return NULL;
267271

268272
atr->ops->detach_addr(atr, chan->chan_id, c2a->addr);

0 commit comments

Comments
 (0)