From patchwork Sun Dec 14 13:59:09 2008 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hai Zaar X-Patchwork-Id: 14174 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from bombadil.infradead.org (bombadil.infradead.org [18.85.46.34]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id D65EEDDF66 for ; Tue, 16 Dec 2008 17:10:06 +1100 (EST) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.68 #1 (Red Hat Linux)) id 1LCT6L-0008VE-EE; Tue, 16 Dec 2008 06:08:13 +0000 Received: from rv-out-0708.google.com ([209.85.198.243]) by bombadil.infradead.org with esmtp (Exim 4.68 #1 (Red Hat Linux)) id 1LBrV1-0004yl-ED for linux-mtd@lists.infradead.org; Sun, 14 Dec 2008 13:59:15 +0000 Received: by rv-out-0708.google.com with SMTP id b17so2257608rvf.42 for ; Sun, 14 Dec 2008 05:59:09 -0800 (PST) Received: by 10.141.4.3 with SMTP id g3mr3089298rvi.124.1229263149455; Sun, 14 Dec 2008 05:59:09 -0800 (PST) Received: by 10.141.153.1 with HTTP; Sun, 14 Dec 2008 05:59:09 -0800 (PST) Message-ID: <76d9e2a30812140559y6ade25c0jf55fd8a7b299e83a@mail.gmail.com> Date: Sun, 14 Dec 2008 15:59:09 +0200 From: "Hai Zaar" To: linux-mtd@lists.infradead.org Subject: [PATCH] nandwrite - handle situation when read returns less bytes the expected MIME-Version: 1.0 X-Google-Sender-Auth: 54813a51c30c93d5 X-Spam-Score: 0.0 (/) X-Mailman-Approved-At: Tue, 16 Dec 2008 01:08:11 -0500 X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.9 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: linux-mtd-bounces@lists.infradead.org Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org 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. 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 --- 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 */