Merge pull request #8534 from poettering/safe-atoi-full

some improvements to safe_atou() to allow specification of arbitrary bases
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2018-03-23 12:30:12 +01:00
committed by GitHub
2 changed files with 22 additions and 32 deletions

View File

@@ -370,12 +370,13 @@ finish:
}
int safe_atou(const char *s, unsigned *ret_u) {
int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
char *x = NULL;
unsigned long l;
assert(s);
assert(ret_u);
assert(base <= 16);
/* strtoul() is happy to parse negative values, and silently
* converts them to unsigned values without generating an
@@ -388,7 +389,7 @@ int safe_atou(const char *s, unsigned *ret_u) {
s += strspn(s, WHITESPACE);
errno = 0;
l = strtoul(s, &x, 0);
l = strtoul(s, &x, base);
if (errno > 0)
return -errno;
if (!x || x == s || *x != 0)
@@ -486,17 +487,18 @@ int safe_atou8(const char *s, uint8_t *ret) {
return 0;
}
int safe_atou16(const char *s, uint16_t *ret) {
int safe_atou16_full(const char *s, unsigned base, uint16_t *ret) {
char *x = NULL;
unsigned long l;
assert(s);
assert(ret);
assert(base <= 16);
s += strspn(s, WHITESPACE);
errno = 0;
l = strtoul(s, &x, 0);
l = strtoul(s, &x, base);
if (errno > 0)
return -errno;
if (!x || x == s || *x != 0)
@@ -530,30 +532,6 @@ int safe_atoi16(const char *s, int16_t *ret) {
return 0;
}
int safe_atoux16(const char *s, uint16_t *ret) {
char *x = NULL;
unsigned long l;
assert(s);
assert(ret);
s += strspn(s, WHITESPACE);
errno = 0;
l = strtoul(s, &x, 16);
if (errno > 0)
return -errno;
if (!x || x == s || *x != 0)
return -EINVAL;
if (s[0] == '-')
return -ERANGE;
if ((unsigned long) (uint16_t) l != l)
return -ERANGE;
*ret = (uint16_t) l;
return 0;
}
int safe_atod(const char *s, double *ret_d) {
_cleanup_(freelocalep) locale_t loc = (locale_t) 0;
char *x = NULL;

View File

@@ -44,17 +44,29 @@ int parse_syscall_and_errno(const char *in, char **name, int *error);
#define FORMAT_BYTES_MAX 8
char *format_bytes(char *buf, size_t l, uint64_t t);
int safe_atou(const char *s, unsigned *ret_u);
int safe_atou_full(const char *s, unsigned base, unsigned *ret_u);
static inline int safe_atou(const char *s, unsigned *ret_u) {
return safe_atou_full(s, 0, ret_u);
}
int safe_atoi(const char *s, int *ret_i);
int safe_atollu(const char *s, unsigned long long *ret_u);
int safe_atolli(const char *s, long long int *ret_i);
int safe_atou8(const char *s, uint8_t *ret);
int safe_atou16(const char *s, uint16_t *ret);
int safe_atoi16(const char *s, int16_t *ret);
int safe_atou16_full(const char *s, unsigned base, uint16_t *ret);
int safe_atoux16(const char *s, uint16_t *ret);
static inline int safe_atou16(const char *s, uint16_t *ret) {
return safe_atou16_full(s, 0, ret);
}
static inline int safe_atoux16(const char *s, uint16_t *ret) {
return safe_atou16_full(s, 16, ret);
}
int safe_atoi16(const char *s, int16_t *ret);
static inline int safe_atou32(const char *s, uint32_t *ret_u) {
assert_cc(sizeof(uint32_t) == sizeof(unsigned));