Skip to content

Commit 2f809f9

Browse files
air237jonasjelonek
authored andcommitted
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: openwrt/openwrt#23552 Signed-off-by: Jonas Jelonek <jelonek.jonas@gmail.com>
1 parent e122d49 commit 2f809f9

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

  • package/system/mtd/src

package/system/mtd/src/mtd.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,14 @@ int mtd_erase_block(int fd, int offset)
183183

184184
int mtd_write_buffer(int fd, const char *buf, int offset, int length)
185185
{
186-
lseek(fd, offset, SEEK_SET);
187-
write(fd, buf, length);
186+
if (lseek(fd, offset, SEEK_SET) != offset) {
187+
fprintf(stderr, "Failed to seek MTD device: %s\n", strerror(errno));
188+
return -1;
189+
}
190+
if (write(fd, buf, length) != length) {
191+
fprintf(stderr, "Short write to MTD device\n");
192+
return -1;
193+
}
188194
return 0;
189195
}
190196

0 commit comments

Comments
 (0)