summaryrefslogtreecommitdiff
path: root/tools/mkenvimage.c
diff options
context:
space:
mode:
authorDavid Wagner <david.wagner@free-electrons.com>2012-01-13 13:27:36 +0000
committerAnatolij Gustschin <agust@denx.de>2012-03-27 09:56:25 +0200
commit3d0f9bd0349712223d5be40aa69f4ca1a1965a3e (patch)
treec116a00a60d737806bdb3d7d026f224d54f8f68d /tools/mkenvimage.c
parentd1acdae98655af4a9ed1b138325ff172206d1c00 (diff)
mkenvimage: More error handling
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 <david.wagner@free-electrons.com> Acked-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'tools/mkenvimage.c')
-rw-r--r--tools/mkenvimage.c31
1 files changed, 29 insertions, 2 deletions
diff --git a/tools/mkenvimage.c b/tools/mkenvimage.c
index c1915795ea..b6879bcef7 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);