Patchwork nandwrite - handle situation when read returns less bytes the expected

login
register
mail settings
Submitter Hai Zaar
Date Dec. 14, 2008, 1:59 p.m.
Message ID <76d9e2a30812140559y6ade25c0jf55fd8a7b299e83a@mail.gmail.com>
Download mbox | patch
Permalink /patch/14174/
State New, archived
Headers show

Comments

Hai Zaar - Dec. 14, 2008, 1:59 p.m.
Good day!
I've found a bug: nandwrite does not handle the situation when read
syscall returns less bytes when expected. Instead of requesting more
input, nandwrite just aborts with error.
The bug is especially triggered when input comes from stdin[1] which
is filled from remote host.

[1] http://lists.infradead.org/pipermail/linux-mtd/2008-September/022913.html.
BTW - is it going to be applied?

P.S. I'm not on the list, so please CC me.

Patch

read syscall can return less bytes then requested - handle this.
This bug is triggered when reading from stdin that comes from remote host

Author: Hai Zaar <haizaar@haizaar.com>

--- mtd-utils-1.2.0/nandwrite.c.orig	2008-12-14 14:56:11.000000000 +0200
+++ mtd-utils-1.2.0/nandwrite.c	2008-12-14 15:11:56.000000000 +0200
@@ -218,6 +218,7 @@ 
 	int ret, readlen;
 	int oobinfochanged = 0;
 	struct nand_oobinfo old_oobinfo;
+	int readcnt = 0;
 
 	process_options(argc, argv);
 
@@ -405,12 +406,22 @@ 
 		}
 
 		/* Read Page Data from input file */
-		if ((cnt = read(ifd, writebuf, readlen)) != readlen) {
-			if (cnt == 0)	// EOF
-				break;
-			perror ("File I/O error on input file");
-			goto closeall;
+		readcnt = 0;
+		while (readcnt < readlen) {
+			if ((cnt = read(ifd, writebuf + readcnt, readlen - readcnt)) < 0) {
+				perror ("File I/O error on input file");
+				goto closeall;
+			} else {
+				if ((cnt == 0) && (readcnt == 0)) {	// EOF
+					break;
+				} else {
+					readcnt += cnt;
+				}
+			}
+
 		}
+		if ((cnt == 0) && (readcnt == 0))	// EOF
+			break;
 
 		if (writeoob) {
 			/* Read OOB data from input file, exit on failure */