From patchwork Sun Jan 8 19:50:05 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wagner X-Patchwork-Id: 134923 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 8F519B6F68 for ; Mon, 9 Jan 2012 06:50:22 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id BE4662834B; Sun, 8 Jan 2012 20:50:20 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vE+NjcHPm1O3; Sun, 8 Jan 2012 20:50:20 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id DB9D12828F; Sun, 8 Jan 2012 20:50:18 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D29B42828F for ; Sun, 8 Jan 2012 20:50:17 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at theia.denx.de Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 8dH45Y6e08MJ for ; Sun, 8 Jan 2012 20:50:17 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail.free-electrons.com (mail.free-electrons.com [88.190.12.23]) by theia.denx.de (Postfix) with ESMTP id 538D72820A for ; Sun, 8 Jan 2012 20:50:15 +0100 (CET) Received: by mail.free-electrons.com (Postfix, from userid 106) id DCAE8182; Sun, 8 Jan 2012 20:44:40 +0100 (CET) Received: from localhost.localdomain (bon31-3-82-226-48-55.fbx.proxad.net [82.226.48.55]) by mail.free-electrons.com (Postfix) with ESMTPSA id BCF8C16B; Sun, 8 Jan 2012 20:44:33 +0100 (CET) From: David Wagner To: u-boot@lists.denx.de Date: Sun, 8 Jan 2012 20:50:05 +0100 Message-Id: <1326052205-9859-1-git-send-email-david.wagner@free-electrons.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1325789099-9260-4-git-send-email-david.wagner@free-electrons.com> References: <1325789099-9260-4-git-send-email-david.wagner@free-electrons.com> Cc: Thomas Petazzoni Subject: [U-Boot] [PATCHv4 4/8] mkenvimage: More error handling X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Verbosly fail if the target environment size or the padding byte are badly formated. Verbosly fail if something bad happens when reading from standard input. Signed-off-by: David Wagner --- tools/mkenvimage.c | 31 +++++++++++++++++++++++++++++-- 1 files changed, 29 insertions(+), 2 deletions(-) diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c index 810a89e..cc1deec 100644 --- a/tools/mkenvimage.c +++ b/tools/mkenvimage.c @@ -63,6 +63,24 @@ static void usage(const char *exec_name) exec_name); } +long int xstrtol(const char *s) +{ + long int tmp; + + errno = 0; + tmp = strtol(s, NULL, 0); + if (!errno) + return tmp; + + if (errno == ERANGE) + fprintf(stderr, "Bad integer format: %s\n", s); + else + fprintf(stderr, "Error while parsing %s: %s\n", s, + strerror(errno)); + + exit(EXIT_FAILURE); +} + int main(int argc, char **argv) { uint32_t crc, targetendian_crc; @@ -92,7 +110,7 @@ int main(int argc, char **argv) while ((option = getopt(argc, argv, ":s:o:rbp:hV")) != -1) { switch (option) { case 's': - datasize = strtol(optarg, NULL, 0); + datasize = xstrtol(optarg); break; case 'o': bin_filename = strdup(optarg); @@ -108,7 +126,7 @@ int main(int argc, char **argv) bigendian = 1; break; case 'p': - padbyte = strtol(optarg, NULL, 0); + padbyte = xstrtol(optarg); break; case 'h': usage(prg); @@ -166,7 +184,16 @@ int main(int argc, char **argv) do { filebuf = realloc(filebuf, readlen); + if (!filebuf) { + fprintf(stderr, "Can't realloc memory for the input file buffer\n"); + return EXIT_FAILURE; + } readbytes = read(txt_fd, filebuf + filesize, readlen); + if (errno) { + fprintf(stderr, "Error while reading stdin: %s\n", + strerror(errno)); + return EXIT_FAILURE; + } filesize += readbytes; } while (readbytes == readlen);