From e94ce889f011b15f7f98b16ae4ddb2123c7c9b0f Mon Sep 17 00:00:00 2001 From: Vadim Yanitskiy Date: Sat, 6 Apr 2019 20:01:43 +0700 Subject: ipaccess-config: use POSIX regex for Unit ID format check Instead of counting digits and slashes of the IPA Unit ID manually, use POSIX regex functions, so the code is easier to maintain and read. As a bonus, this fixes CID#188854: variable 'remain_slash' was of type 'uint8_t', so it could never be lower than zero. Change-Id: Id613bf650833dd38eaad08fdfffdf8dbe2f002b1 Related: CID#188854 Unsigned integer overflow --- src/ipaccess/ipaccess-config.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/src/ipaccess/ipaccess-config.c b/src/ipaccess/ipaccess-config.c index da19ce20a..54e4efd09 100644 --- a/src/ipaccess/ipaccess-config.c +++ b/src/ipaccess/ipaccess-config.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -842,33 +843,20 @@ static void analyze_firmware(const char *filename) static bool check_unitid_fmt(const char* unit_id) { - const char *p = unit_id; - bool must_digit = true; - uint8_t remain_slash = 2; + regex_t regexp; + int rc; if (strlen(unit_id) < 5) goto wrong_fmt; - while (*p != '\0') { - if (*p != '/' && !isdigit(*p)) - goto wrong_fmt; - if (*p == '/' && must_digit) - goto wrong_fmt; - if (*p == '/') { - must_digit = true; - remain_slash--; - if (remain_slash < 0) - goto wrong_fmt; - } else { - must_digit = false; - } - p++; - } + rc = regcomp(®exp, "^[0-9]+/[0-9]+/[0-9]+$", REG_EXTENDED | REG_NOSUB); + OSMO_ASSERT(!rc); - if (*(p-1) == '/') - goto wrong_fmt; + rc = regexec(®exp, unit_id, 0, NULL, 0); + regfree(®exp); - return true; + if (rc == 0) + return true; wrong_fmt: fprintf(stderr, "ERROR: unit-id wrong format. Must be '\\d+/\\d+/\\d+'\n"); -- cgit v1.2.3