From b7f80de25d701ebbfbcb03ac73c2bb4de0979d90 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 10 Jan 2016 14:29:18 +0100 Subject: all: Check for return value of lseek --- src/cal.c | 3 ++- src/disk.c | 5 ++++- src/fiasco.c | 5 ++++- src/image.c | 38 +++++++++++++++++++++++++++++++++----- src/main.c | 5 ++++- 5 files changed, 47 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/cal.c b/src/cal.c index 99de241..102ffab 100644 --- a/src/cal.c +++ b/src/cal.c @@ -92,7 +92,8 @@ int cal_init_file(const char * file, struct cal ** cal_out) { blksize = lseek(fd, 0, SEEK_END); if ( blksize == (off_t)-1 ) goto err; - lseek(fd, 0, SEEK_SET); + if ( lseek(fd, 0, SEEK_SET) == (off_t)-1 ) + goto err; #endif if ( blksize > SSIZE_MAX ) goto err; diff --git a/src/disk.c b/src/disk.c index 7370f13..f8a51f8 100644 --- a/src/disk.c +++ b/src/disk.c @@ -185,7 +185,10 @@ int disk_dump_dev(int fd, const char * file) { return -1; } - lseek(fd, 0, SEEK_SET); + if ( lseek(fd, 0, SEEK_SET) == (off_t)-1 ) { + ERROR_INFO("Cannot seek to begin of block device"); + return -1; + } #endif diff --git a/src/fiasco.c b/src/fiasco.c index 7917385..ba6d262 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -218,6 +218,8 @@ struct fiasco * fiasco_alloc_from_file(const char * file) { READ_OR_RETURN(fiasco, buf, 1); offset = lseek(fiasco->fd, 0, SEEK_CUR); + if ( offset == (off_t)-1 ) + FIASCO_READ_ERROR(fiasco, "Cannot get offset of file"); VERBOSE(" version: %s\n", version); VERBOSE(" device: %s\n", device); @@ -231,7 +233,8 @@ struct fiasco * fiasco_alloc_from_file(const char * file) { fiasco_add_image(fiasco, image); - lseek(fiasco->fd, offset+length, SEEK_SET); + if ( lseek(fiasco->fd, offset+length, SEEK_SET) == (off_t)-1 ) + FIASCO_READ_ERROR(fiasco, "Cannot seek to next image in file"); } diff --git a/src/image.c b/src/image.c index 0b16de1..0daebf3 100644 --- a/src/image.c +++ b/src/image.c @@ -247,6 +247,7 @@ static struct image * image_alloc(void) { struct image * image_alloc_from_file(const char * file, const char * type, const char * device, const char * hwrevs, const char * version, const char * layout) { + off_t offset; struct image * image = image_alloc(); if ( ! image ) return NULL; @@ -259,11 +260,26 @@ struct image * image_alloc_from_file(const char * file, const char * type, const return NULL; } - image->size = lseek(image->fd, 0, SEEK_END); + offset = lseek(image->fd, 0, SEEK_END); + if ( offset == (off_t)-1 ) { + ERROR_INFO("Cannot seek to end of file %s", file); + close(image->fd); + free(image); + return NULL; + } + + image->size = offset; image->offset = 0; image->cur = 0; image->orig_filename = strdup(file); - lseek(image->fd, 0, SEEK_SET); + + if ( lseek(image->fd, 0, SEEK_SET) == (off_t)-1 ) { + ERROR_INFO("Cannot seek to begin of file %s", file); + close(image->fd); + free(image->orig_filename); + free(image); + return NULL; + } if ( image_append(image, type, device, hwrevs, version, layout) < 0 ) return NULL; @@ -331,17 +347,22 @@ void image_free(struct image * image) { void image_seek(struct image * image, size_t whence) { + off_t offset; + if ( whence > image->size ) return; if ( whence >= image->size - image->align ) { - lseek(image->fd, image->size - image->align - 1, SEEK_SET); + offset = lseek(image->fd, image->size - image->align - 1, SEEK_SET); image->acur = whence - ( image->size - image->align ); } else { - lseek(image->fd, image->offset + whence, SEEK_SET); + offset = lseek(image->fd, image->offset + whence, SEEK_SET); image->acur = 0; } + if ( offset == (off_t)-1 ) + ERROR_INFO("Seek in file %s failed", (image->orig_filename ? image->orig_filename : "(unknown)")); + IMAGE_STORE_CUR(image); } @@ -350,6 +371,7 @@ size_t image_read(struct image * image, void * buf, size_t count) { size_t cur; ssize_t ret; + off_t offset; size_t new_count = 0; size_t ret_count = 0; @@ -376,7 +398,13 @@ size_t image_read(struct image * image, void * buf, size_t count) { if ( ret_count == count ) return ret_count; - cur = lseek(image->fd, 0, SEEK_CUR) - image->offset; + offset = lseek(image->fd, 0, SEEK_CUR); + if ( offset == (off_t)-1 ) { + ERROR_INFO("Cannot get offset of file %s", (image->orig_filename ? image->orig_filename : "(unknown)")); + return 0; + } + + cur = offset - image->offset; if ( image->align && cur == image->size - image->align && image->acur < image->align ) { diff --git a/src/main.c b/src/main.c index b224e5c..a6994b8 100644 --- a/src/main.c +++ b/src/main.c @@ -212,7 +212,10 @@ static void parse_image_arg(char * arg, struct image_list ** image_first) { ERROR_INFO("Cannot get size of file %s", layout_file); exit(1); } - lseek(fd, 0, SEEK_SET); + if ( lseek(fd, 0, SEEK_SET) == (off_t)-1 ) { + ERROR_INFO("Cannot seek to begin of file %s", layout_file); + exit(1); + } layout = malloc(len+1); if ( ! layout ) { ALLOC_ERROR(); -- cgit v1.2.3