mtd: check return values of lseek() and write() in mtd_write_buffer()

Two ignored return values in mtd_write_buffer() caused silent failures:

- lseek() return value was ignored. A failed seek (e.g. EBADF, ESPIPE)
  followed by write() would silently write at the wrong offset.
- write() return value was ignored, silently discarding write errors.
  This could lead to data corruption on the MTD device without any
  indication.

Check both return values, report errors to stderr and return -1 on
failure.

Signed-off-by: Anna Kiri <bredcorn@gmail.com>
Link: https://github.com/openwrt/openwrt/pull/23552
Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com>
This commit is contained in:
Anna Kiri
2026-06-08 11:05:55 +02:00
committed by Jonas Jelonek
parent e122d49577
commit 2f809f9cff
+8 -2
View File
@@ -183,8 +183,14 @@ int mtd_erase_block(int fd, int offset)
int mtd_write_buffer(int fd, const char *buf, int offset, int length)
{
lseek(fd, offset, SEEK_SET);
write(fd, buf, length);
if (lseek(fd, offset, SEEK_SET) != offset) {
fprintf(stderr, "Failed to seek MTD device: %s\n", strerror(errno));
return -1;
}
if (write(fd, buf, length) != length) {
fprintf(stderr, "Short write to MTD device\n");
return -1;
}
return 0;
}