sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

commit c25e7897485c4805f5638875e4e5c832634bc8cb
parent e6dc397f0636b3e6059499b5d9834cba1273a4bb
Author: FRIGN <dev@frign.de>
Date:   Wed Sep 30 19:14:14 +0200

Harden parseoffset() even more

1) Check for NULL.
2) Check for empty strings.
3) Clarify error-messages.

Diffstat:
libutil/parseoffset.c | 11++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/libutil/parseoffset.c b/libutil/parseoffset.c @@ -14,6 +14,11 @@ parseoffset(const char *str) int base = 10; char *end; + if (!str || !*str) { + weprintf("parseoffset: empty string\n"); + return -1; + } + /* bases */ if (!strncasecmp(str, "0x", strlen("0x"))) { base = 16; @@ -24,7 +29,7 @@ parseoffset(const char *str) res = strtol(str, &end, base); if (res < 0) { - weprintf("invalid file offset: %s\n", str); + weprintf("parseoffset %s: negative value\n", str); return -1; } @@ -44,14 +49,14 @@ parseoffset(const char *str) scale = 1024L * 1024L * 1024L; break; default: - weprintf("invalid file offset suffix: %s\n", str); + weprintf("parseoffset %s: invalid suffix\n", str); return -1; } } /* prevent overflow */ if (res > (SIZE_MAX / scale)) { - weprintf("file offset out of range: %s\n", str); + weprintf("parseoffset %s: out of range\n", str); return -1; }