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 --- bin/gensquashfs/src/sort_by_file.c | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) (limited to 'bin') diff --git a/bin/gensquashfs/src/sort_by_file.c b/bin/gensquashfs/src/sort_by_file.c index f543767..1ca5aa7 100644 --- a/bin/gensquashfs/src/sort_by_file.c +++ b/bin/gensquashfs/src/sort_by_file.c @@ -9,27 +9,14 @@ static int decode_priority(const char *filename, size_t line_no, char *line, sqfs_s64 *priority) { - bool negative = false; - size_t i = 0; + size_t i; + int ret; - if (line[0] == '-') { - negative = true; - i = 1; - } - - if (!isdigit(line[i])) + ret = parse_int(line, strlen(line), &i, 0, 0, priority); + if (ret == SQFS_ERROR_CORRUPTED) goto fail_number; - - *priority = 0; - - for (; isdigit(line[i]); ++i) { - sqfs_s64 x = line[i] - '0'; - - if ((*priority) >= ((0x7FFFFFFFFFFFFFFFL - x) / 10L)) - goto fail_ov; - - (*priority) = (*priority) * 10 + x; - } + if (ret != 0) + goto fail_ov; if (!isspace(line[i])) goto fail_filename; @@ -40,9 +27,6 @@ static int decode_priority(const char *filename, size_t line_no, if (line[i] == '\0') goto fail_filename; - if (negative) - (*priority) = -(*priority); - memmove(line, line + i, strlen(line + i) + 1); return 0; fail_number: -- cgit v1.2.3