public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] nandwrite - handle situation when read returns less bytes the expected
@ 2008-12-14 13:59 Hai Zaar
  2008-12-16  6:28 ` Artem Bityutskiy
  0 siblings, 1 reply; 6+ messages in thread
From: Hai Zaar @ 2008-12-14 13:59 UTC (permalink / raw)
  To: linux-mtd

[-- Attachment #1: Type: text/plain, Size: 451 bytes --]

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.
-- 
Zaar

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: mtd-utils-1.2.0-all-read-fix.patch --]
[-- Type: text/x-diff; name=mtd-utils-1.2.0-all-read-fix.patch, Size: 1152 bytes --]

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 */

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2008-12-26 12:42 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-12-14 13:59 [PATCH] nandwrite - handle situation when read returns less bytes the expected Hai Zaar
2008-12-16  6:28 ` Artem Bityutskiy
2008-12-16  8:40   ` Hai Zaar
2008-12-16 10:18     ` Artem Bityutskiy
2008-12-24 14:48       ` Hai Zaar
2008-12-26 12:43         ` Artem Bityutskiy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox