Skip to content

Commit ae5b69c

Browse files
kaabiajhedberg
authored andcommitted
eeprom: fram: Fix read/write length and buffer pointer arithmetic
Fixes two critical bugs in the Fujitsu MB85RCxx I2C FRAM driver when handling transfers that cross page boundaries: Fixes incorrect write length: The mb85rcxx_write function was incorrectly passing the total remaining length (len) to the underlying I2C transfer instead of the page-limited length (len_in_page). This resulted in I2C errors or data corruption when writing past a page boundary. Fixes buffer pointer update: Corrects the loop's buffer pointer arithmetic in both mb85rcxx_read and mb85rcxx_write. The original code used incorrect casting (*(char *)&buf += len_in_page;), leading to a strict aliasing violation and potentially undefined behavior. The pointer is now correctly advanced using explicit casting: - mb85rcxx_read: buf = (char *)buf + len_in_page; - mb85rcxx_write: buf = (const char *)buf + len_in_page; Signed-off-by: Badr Bacem KAABIA <badrbacemkaabia@gmail.com>
1 parent bfdfe99 commit ae5b69c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

drivers/eeprom/eeprom_mb85rcxx.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static int mb85rcxx_read(const struct device *dev, off_t offset, void *buf, size
126126
}
127127

128128
len -= len_in_page;
129-
*(char *)&buf += len_in_page;
129+
buf = (char *)buf + len_in_page;
130130
offset += len_in_page;
131131
}
132132

@@ -182,15 +182,15 @@ static int mb85rcxx_write(const struct device *dev, off_t offset, const void *bu
182182
i2c_addr = mb85rcxx_translate_address(dev, offset, addr);
183183
len_in_page = mb85rcxx_remaining_len_in_page(dev, offset, len);
184184

185-
ret = mb85rcxx_i2c_write(dev, i2c_addr, addr, buf, len);
185+
ret = mb85rcxx_i2c_write(dev, i2c_addr, addr, buf, len_in_page);
186186
if (ret < 0) {
187187
LOG_ERR("failed to write to FRAM (err %d)", ret);
188188
k_mutex_unlock(&data->lock);
189189
return ret;
190190
}
191191

192192
len -= len_in_page;
193-
*(char *)&buf += len_in_page;
193+
buf = (const char *)buf + len_in_page;
194194
offset += len_in_page;
195195
}
196196

0 commit comments

Comments
 (0)