diff mbox series

[mtd-utils,2/7] misc-utils: flashcp: check for lseek errors

Message ID 20221102224757.58012-3-brandon.maier@collins.com
State Accepted
Delegated to: David Oberhollenzer
Headers show
Series misc-utils: flashcp: Bugfixes to --partition | expand

Commit Message

Brandon Maier Nov. 2, 2022, 10:47 p.m. UTC
Add a safe_lseek wrapper to check for lseek errors.

Signed-off-by: Brandon Maier <brandon.maier@collins.com>
---
 misc-utils/flashcp.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/misc-utils/flashcp.c b/misc-utils/flashcp.c
index 59389db..43a497f 100644
--- a/misc-utils/flashcp.c
+++ b/misc-utils/flashcp.c
@@ -170,13 +170,23 @@  static void safe_write (int fd,const void *buf,size_t count,size_t written,unsig
 	}
 }
 
-static void safe_rewind (int fd,const char *filename)
+static off_t safe_lseek (int fd,off_t offset,int whence,const char *filename)
 {
-	if (lseek (fd,0L,SEEK_SET) < 0)
+	off_t off;
+
+	off = lseek (fd,offset,whence);
+	if (off < 0)
 	{
-		log_printf (LOG_ERROR,"While seeking to start of %s: %m\n",filename);
+		log_printf (LOG_ERROR,"While seeking on %s: %m\n",filename);
 		exit (EXIT_FAILURE);
 	}
+
+	return off;
+}
+
+static void safe_rewind (int fd,const char *filename)
+{
+	safe_lseek(fd,0L,SEEK_SET,filename);
 }
 
 /******************************************************************************/
@@ -456,7 +466,7 @@  DIFF_BLOCKS:
 		safe_read (fil_fd,filename,src,i,flags & FLAG_VERBOSE);
 
 		/* read from device */
-		current_dev_block = lseek(dev_fd, 0, SEEK_CUR);
+		current_dev_block = safe_lseek(dev_fd, 0, SEEK_CUR, device);
 		safe_read (dev_fd,device,dest,i,flags & FLAG_VERBOSE);
 
 		/* compare buffers, if not the same, erase and write the block */
@@ -464,7 +474,7 @@  DIFF_BLOCKS:
 		{
 			diffBlock++;
 			/* erase block */
-			lseek(dev_fd, current_dev_block, SEEK_SET);
+			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
 			if (ioctl (dev_fd,MEMERASE,&erase) < 0)
 			{
 				log_printf (LOG_NORMAL,"\n");
@@ -475,7 +485,7 @@  DIFF_BLOCKS:
 			}
 
 			/* write to device */
-			lseek(dev_fd, current_dev_block, SEEK_SET);
+			safe_lseek(dev_fd, current_dev_block, SEEK_SET, device);
 			safe_write(dev_fd,src,i,written,(unsigned long long)filestat.st_size,device,flags & FLAG_VERBOSE);
 		}