diff options
author | Rob Landley <rob@landley.net> | 2015-01-01 16:28:51 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2015-01-01 16:28:51 -0600 |
commit | 86c747a4493b2b1aabab9b20d1c4566fddeeb2ca (patch) | |
tree | fe5254ddbb277f19724c5bacb412e677f595d919 /lib | |
parent | cf2516ab4bcf2c1d4cb571539dae794c6a40c5c6 (diff) | |
download | toybox-86c747a4493b2b1aabab9b20d1c4566fddeeb2ca.tar.gz |
strtol() doesn't return error indicator for overflow, it just sets errno. So add estrtol() (which clears errno first), and xstrtol() (which error_exit()s on overflow).
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lib.c | 28 | ||||
-rw-r--r-- | lib/lib.h | 2 |
2 files changed, 25 insertions, 5 deletions
@@ -233,13 +233,30 @@ struct string_list *find_in_path(char *path, char *filename) return rlist; } +long estrtol(char *str, char **end, int base) +{ + errno = 0; + + return strtol(str, end, base); +} + +long xstrtol(char *str, char **end, int base) +{ + long l = estrtol(str, end, base); + + if (errno) perror_exit("%s", str); + + return l; +} + // atol() with the kilo/mega/giga/tera/peta/exa extensions. // (zetta and yotta don't fit in 64 bits.) long atolx(char *numstr) { char *c, *suffixes="cbkmgtpe", *end; - long val = strtol(numstr, &c, 0); + long val; + val = xstrtol(numstr, &c, 0); if (*c) { if (c != numstr && (end = strchr(suffixes, tolower(*c)))) { int shift = end-suffixes-2; @@ -685,8 +702,9 @@ int sig_to_num(char *pidstr) if (pidstr) { char *s; - i = strtol(pidstr, &s, 10); - if (!*s) return i; + + i = estrtol(pidstr, &s, 10); + if (!errno && !*s) return i; if (!strncasecmp(pidstr, "sig", 3)) pidstr+=3; } @@ -715,8 +733,8 @@ mode_t string_to_mode(char *modestr, mode_t mode) // Handle octal mode if (isdigit(*str)) { - mode = strtol(str, &s, 8); - if (*s || (mode & ~(07777))) goto barf; + mode = estrtol(str, &s, 8); + if (errno || *s || (mode & ~(07777))) goto barf; return mode | extrabits; } @@ -148,6 +148,8 @@ int64_t peek_be(void *ptr, unsigned size); int64_t peek(void *ptr, unsigned size); void poke(void *ptr, uint64_t val, int size); struct string_list *find_in_path(char *path, char *filename); +long estrtol(char *str, char **end, int base); +long xstrtol(char *str, char **end, int base); long atolx(char *c); long atolx_range(char *numstr, long low, long high); int numlen(long l); |