From 9bb210192dd5b60582633079d4769a582ac11074 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Thu, 19 Jan 2012 22:28:06 +0100 Subject: [PATCH] cleanup: readable test to check for 32bit overflow also swap return value 0 - fail, 1 - success. --- tools/toollib.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/toollib.c b/tools/toollib.c index 1916bfde9..3fe1c14c2 100644 --- a/tools/toollib.c +++ b/tools/toollib.c @@ -994,10 +994,11 @@ static int xstrtouint32(const char *s, char **p, int base, uint32_t *result) errno = 0; ul = strtoul(s, p, base); - if (errno || *p == s || (uint32_t) ul != ul) - return -1; + if (errno || *p == s || ul > UINT32_MAX) + return 0; *result = ul; - return 0; + + return 1; } static int _parse_pes(struct dm_pool *mem, char *c, struct dm_list *pe_ranges, @@ -1029,7 +1030,7 @@ static int _parse_pes(struct dm_pool *mem, char *c, struct dm_list *pe_ranges, /* Start extent given? */ if (isdigit(*c)) { - if (xstrtouint32(c, &endptr, 10, &start)) + if (!xstrtouint32(c, &endptr, 10, &start)) goto error; c = endptr; /* Just one number given? */ @@ -1040,7 +1041,7 @@ static int _parse_pes(struct dm_pool *mem, char *c, struct dm_list *pe_ranges, if (*c == '-') { c++; if (isdigit(*c)) { - if (xstrtouint32(c, &endptr, 10, &end)) + if (!xstrtouint32(c, &endptr, 10, &end)) goto error; c = endptr; }