Change in osmo-bsc[master]: ipaccess-config: use POSIX regex for Unit ID format check

Harald Welte gerrit-no-reply at lists.osmocom.org
Mon Apr 8 07:30:45 UTC 2019


Harald Welte has submitted this change and it was merged. ( https://gerrit.osmocom.org/13535 )

Change subject: ipaccess-config: use POSIX regex for Unit ID format check
......................................................................

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
---
M src/ipaccess/ipaccess-config.c
1 file changed, 9 insertions(+), 21 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Pau Espin Pedrol: Looks good to me, but someone else must approve
  Jenkins Builder: Verified



diff --git a/src/ipaccess/ipaccess-config.c b/src/ipaccess/ipaccess-config.c
index da19ce2..54e4efd 100644
--- a/src/ipaccess/ipaccess-config.c
+++ b/src/ipaccess/ipaccess-config.c
@@ -27,6 +27,7 @@
 #include <getopt.h>
 #include <errno.h>
 #include <ctype.h>
+#include <regex.h>
 #include <inttypes.h>
 #include <sys/fcntl.h>
 #include <sys/stat.h>
@@ -842,33 +843,20 @@
 
 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(&regexp, "^[0-9]+/[0-9]+/[0-9]+$", REG_EXTENDED | REG_NOSUB);
+	OSMO_ASSERT(!rc);
 
-	if (*(p-1) == '/')
-		goto wrong_fmt;
+	rc = regexec(&regexp, unit_id, 0, NULL, 0);
+	regfree(&regexp);
 
-	return true;
+	if (rc == 0)
+		return true;
 
 wrong_fmt:
 	fprintf(stderr, "ERROR: unit-id wrong format. Must be '\\d+/\\d+/\\d+'\n");

-- 
To view, visit https://gerrit.osmocom.org/13535
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-bsc
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Id613bf650833dd38eaad08fdfffdf8dbe2f002b1
Gerrit-Change-Number: 13535
Gerrit-PatchSet: 3
Gerrit-Owner: Vadim Yanitskiy <axilirator at gmail.com>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190408/160f3a96/attachment.html>


More information about the gerrit-log mailing list