From patchwork Sun Jan 8 14:25:21 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Wagner X-Patchwork-Id: 134914 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 DD47DB6F65 for ; Mon, 9 Jan 2012 01:25:41 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 9B9D0282B4; Sun, 8 Jan 2012 15:25:37 +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 FV4wlZu8guXM; Sun, 8 Jan 2012 15:25:37 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0EA2E2829B; Sun, 8 Jan 2012 15:25:36 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 1C45E2829B for ; Sun, 8 Jan 2012 15:25:34 +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 1GN4hzsyF8+F for ; Sun, 8 Jan 2012 15:25:32 +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 471D0280EF for ; Sun, 8 Jan 2012 15:25:30 +0100 (CET) Received: by mail.free-electrons.com (Postfix, from userid 106) id 0F992182; Sun, 8 Jan 2012 15:20:00 +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 DD06B16B; Sun, 8 Jan 2012 15:19:52 +0100 (CET) From: David Wagner To: u-boot@lists.denx.de Date: Sun, 8 Jan 2012 15:25:21 +0100 Message-Id: <1326032721-6150-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] [PATCHv3 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 | 30 ++++++++++++++++++++++++++++-- 1 files changed, 28 insertions(+), 2 deletions(-) diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c index 810a89e..9f2490e 100644 --- a/tools/mkenvimage.c +++ b/tools/mkenvimage.c @@ -63,6 +63,23 @@ static void usage(const char *exec_name) exec_name); } +long int xstrtol(char *s) +{ + long int tmp; + + 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 +109,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 +125,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 +183,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);