Harald Welte has submitted this change and it was merged.

Change subject: ipaccess-config: Check cmdlie arg unit-id format
......................................................................


ipaccess-config: Check cmdlie arg unit-id format

Print a clear error when the format in not correct. I was in the
situation several times in which I was passing "123" instead of
"123/0/0", and spent a while seeing what was wrong.

Change-Id: I70906939b3320473c56a87929c4886aac9d7d064
---
M src/ipaccess/ipaccess-config.c
1 file changed, 38 insertions(+), 0 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/src/ipaccess/ipaccess-config.c b/src/ipaccess/ipaccess-config.c
index b83846d..7b9bd02 100644
--- a/src/ipaccess/ipaccess-config.c
+++ b/src/ipaccess/ipaccess-config.c
@@ -26,6 +26,7 @@
 #include <string.h>
 #include <getopt.h>
 #include <errno.h>
+#include <ctype.h>
 #include <sys/fcntl.h>
 #include <sys/stat.h>
 
@@ -782,6 +783,41 @@
        talloc_free(tall_firm_ctx);
 }
 
+static bool check_unitid_fmt(const char* unit_id)
+{
+       const char *p = unit_id;
+       bool must_digit = true;
+       uint8_t remain_slash = 2;
+
+       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++;
+       }
+
+       if (*(p-1) == '/')
+               goto wrong_fmt;
+
+       return true;
+
+wrong_fmt:
+       fprintf(stderr, "ERROR: unit-id wrong format. Must be 
'\\d+/\\d+/\\d+'\n");
+       return false;
+}
+
 static void print_usage(void)
 {
        printf("Usage: ipaccess-config IP_OF_BTS\n");
@@ -904,6 +940,8 @@
 
                switch (c) {
                case 'u':
+                       if (!check_unitid_fmt(optarg))
+                               exit(2);
                        unit_id = optarg;
                        break;
                case 'o':

-- 
To view, visit https://gerrit.osmocom.org/7324
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I70906939b3320473c56a87929c4886aac9d7d064
Gerrit-PatchSet: 2
Gerrit-Project: osmo-bsc
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pes...@sysmocom.de>
Gerrit-Reviewer: Harald Welte <lafo...@gnumonks.org>
Gerrit-Reviewer: Jenkins Builder

Reply via email to