From a64417804f4c2b0425e167851d10854cf1f23e99 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sat, 1 Jul 2023 12:41:56 +0200 Subject: Consolidate some of the stray integer parsers There are several ad-hoc int/uint parsers scattered around the code, add a single helper function for that task and replace the multiple instances. A simple white-box test case is added for the utility function. Signed-off-by: David Oberhollenzer --- lib/util/src/parse_int.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 lib/util/src/parse_int.c (limited to 'lib/util/src/parse_int.c') diff --git a/lib/util/src/parse_int.c b/lib/util/src/parse_int.c new file mode 100644 index 0000000..1bca528 --- /dev/null +++ b/lib/util/src/parse_int.c @@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: LGPL-3.0-or-later */ +/* + * alloc.c + * + * Copyright (C) 2019 David Oberhollenzer + */ +#include "config.h" + +#include "util/parse.h" +#include "sqfs/error.h" + +#include + +int parse_uint(const char *in, size_t len, size_t *diff, + sqfs_u64 vmin, sqfs_u64 vmax, sqfs_u64 *out) +{ + /* init result */ + if (diff != NULL) + *diff = 0; + *out = 0; + + /* sequence has at least 1 digit */ + if (len == 0 || !isdigit(*in)) + return SQFS_ERROR_CORRUPTED; + + /* parse sequence */ + while (len > 0 && isdigit(*in)) { + sqfs_u64 x = *(in++) - '0'; + --len; + + if (diff != NULL) + ++(*diff); + + if ((*out) >= (0xFFFFFFFFFFFFFFFFULL / 10ULL)) + return SQFS_ERROR_OVERFLOW; + + (*out) *= 10; + + if ((*out) > (0xFFFFFFFFFFFFFFFFULL - x)) + return SQFS_ERROR_OVERFLOW; + + (*out) += x; + } + + /* range check */ + if ((vmin != vmax) && ((*out < vmin) || (*out > vmax))) + return SQFS_ERROR_OUT_OF_BOUNDS; + + /* if diff is not used, entire must have been processed */ + if (diff == NULL && (len > 0 && *in != '\0')) + return SQFS_ERROR_CORRUPTED; + + return 0; +} + + +int parse_int(const char *in, size_t len, size_t *diff, + sqfs_s64 vmin, sqfs_s64 vmax, sqfs_s64 *out) +{ + bool negative = false; + sqfs_u64 temp; + int ret; + + if (len > 0 && *in == '-') { + ++in; + --len; + negative = true; + } + + ret = parse_uint(in, len, diff, 0, 0, &temp); + if (ret) + return ret; + + if (temp >= 0x7FFFFFFFFFFFFFFFULL) + return SQFS_ERROR_OVERFLOW; + + if (negative) { + if (diff != NULL) + (*diff) += 1; + *out = -((sqfs_s64)temp); + } else { + *out = (sqfs_s64)temp; + } + + if (vmin != vmax && ((*out < vmin) || (*out > vmax))) + return SQFS_ERROR_OUT_OF_BOUNDS; + + return 0; +} -- cgit v1.2.3