summaryrefslogtreecommitdiffstats
path: root/lib/strutils.c
diff options
context:
space:
mode:
authorKarel Zak2014-01-30 12:56:01 +0100
committerKarel Zak2014-03-11 11:35:13 +0100
commit3643958f481f9d4530bf96ab0c80ec23480827f0 (patch)
tree78c383bad9754804ce84aa19d7423c0e4ab2b493 /lib/strutils.c
parentlibfdisk: rewrite freespace code (diff)
downloadkernel-qcow2-util-linux-3643958f481f9d4530bf96ab0c80ec23480827f0.tar.gz
kernel-qcow2-util-linux-3643958f481f9d4530bf96ab0c80ec23480827f0.tar.xz
kernel-qcow2-util-linux-3643958f481f9d4530bf96ab0c80ec23480827f0.zip
lib/strutils: use proper return codes in parse_size()
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'lib/strutils.c')
-rw-r--r--lib/strutils.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/lib/strutils.c b/lib/strutils.c
index 69b7ba23f..f87bf97f4 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -21,7 +21,7 @@ static int do_scale_by_power (uintmax_t *x, int base, int power)
{
while (power--) {
if (UINTMAX_MAX / base < *x)
- return -2;
+ return -ERANGE;
*x *= base;
}
return 0;
@@ -74,17 +74,20 @@ int parse_size(const char *str, uintmax_t *res, int *power)
p = (char *) str;
while (isspace((unsigned char) *p))
p++;
- if (*p == '-')
+ if (*p == '-') {
+ rc = -EINVAL;
goto err;
+ }
p = NULL;
errno = 0;
x = strtoumax(str, &p, 0);
if (p == str ||
- (errno != 0 && (x == UINTMAX_MAX || x == 0)))
+ (errno != 0 && (x == UINTMAX_MAX || x == 0))) {
+ rc = errno ? -errno : -1;
goto err;
-
+ }
if (!p || !*p)
goto done; /* without suffix */
@@ -95,9 +98,10 @@ int parse_size(const char *str, uintmax_t *res, int *power)
base = 1024; /* XiB, 2^N */
else if (*(p + 1) == 'B' && !*(p + 2))
base = 1000; /* XB, 10^N */
- else if (*(p + 1))
+ else if (*(p + 1)) {
+ rc = -EINVAL;
goto err; /* unexpected suffix */
-
+ }
sp = strchr(suf, *p);
if (sp)
pwr = (sp - suf) + 1;
@@ -105,8 +109,10 @@ int parse_size(const char *str, uintmax_t *res, int *power)
sp = strchr(suf2, *p);
if (sp)
pwr = (sp - suf2) + 1;
- else
+ else {
+ rc = -EINVAL;
goto err;
+ }
}
rc = do_scale_by_power(&x, base, pwr);
@@ -114,9 +120,8 @@ int parse_size(const char *str, uintmax_t *res, int *power)
*power = pwr;
done:
*res = x;
- return rc;
err:
- return -1;
+ return rc;
}
int strtosize(const char *str, uintmax_t *res)