From ce5eacb0762bb19c61a543b64167f7b27f189bed Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Tue, 12 Apr 2022 22:51:04 +0100 Subject: [PATCH] libfstools: mtd: improve error handling Use -1 to mark invalid file descriptors as 0 can theoretically be a valid open file descriptor. Do not ignore lseek() return value and check if an error has occured. Signed-off-by: Daniel Golle --- libfstools/mtd.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/libfstools/mtd.c b/libfstools/mtd.c index c5dce50..3696828 100644 --- a/libfstools/mtd.c +++ b/libfstools/mtd.c @@ -67,7 +67,7 @@ static void mtd_volume_close(struct mtd_volume *p) return; close(p->fd); - p->fd = 0; + p->fd = -1; } static int mtd_volume_load(struct mtd_volume *p) @@ -76,17 +76,14 @@ static int mtd_volume_load(struct mtd_volume *p) struct mtd_info_user mtdInfo; struct erase_info_user mtdLockInfo; - if (p->fd) { - lseek(p->fd, 0, SEEK_SET); - return 0; - } + if (p->fd >= 0) + return (lseek(p->fd, 0, SEEK_SET) == -1); if (!p->chr) return -1; p->fd = mtd_open(p->chr, 0); if (p->fd < 0) { - p->fd = 0; ULOG_ERR("Could not open mtd device: %s\n", p->chr); return -1; } @@ -167,6 +164,7 @@ static struct volume *mtd_volume_find(char *name) v->name = strdup(name); v->drv = &mtd_driver; p->idx = atoi(idx); + p->fd = -1; snprintf(buffer, sizeof(buffer), "/dev/mtdblock%s", idx); v->blk = strdup(buffer); -- 2.30.2