[hackers] [sbase] Harden parseoffset() even more || FRIGN

From: <git_AT_suckless.org>
Date: Wed, 30 Sep 2015 20:44:18 +0200 (CEST)

commit 870a75076d969f0ec8e3ccea9b75726612476bab
Author: FRIGN <dev_AT_frign.de>
AuthorDate: Wed Sep 30 19:14:14 2015 +0200
Commit: sin <sin_AT_2f30.org>
CommitDate: Wed Sep 30 19:44:10 2015 +0100

    Harden parseoffset() even more
    
    1) Check for NULL.
    2) Check for empty strings.
    3) Clarify error-messages.

diff --git a/libutil/parseoffset.c b/libutil/parseoffset.c
index 9ce0411..d12557f 100644
--- a/libutil/parseoffset.c
+++ b/libutil/parseoffset.c
_AT_@ -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;
_AT_@ -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;
         }
 
_AT_@ -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;
         }
 
Received on Wed Sep 30 2015 - 20:44:18 CEST

This archive was generated by hypermail 2.3.0 : Wed Sep 30 2015 - 20:48:38 CEST