<p>Vadim Yanitskiy has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.osmocom.org/13535">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">ipaccess-config: use POSIX regex for Unit ID format check<br><br>Instead of counting digits and slashes of the IPA Unit ID manually,<br>use POSIX regex functions, so the code is easier to maintain and<br>read. As a bonus, this fixes CID#188854: variable 'remain_slash'<br>was of type 'uint8_t', so it could never be lower than zero.<br><br>Change-Id: Id613bf650833dd38eaad08fdfffdf8dbe2f002b1<br>Related: CID#188854 Unsigned integer overflow<br>---<br>M src/ipaccess/ipaccess-config.c<br>1 file changed, 9 insertions(+), 21 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.osmocom.org:29418/osmo-bsc refs/changes/35/13535/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/src/ipaccess/ipaccess-config.c b/src/ipaccess/ipaccess-config.c</span><br><span>index da19ce2..54e4efd 100644</span><br><span>--- a/src/ipaccess/ipaccess-config.c</span><br><span>+++ b/src/ipaccess/ipaccess-config.c</span><br><span>@@ -27,6 +27,7 @@</span><br><span> #include <getopt.h></span><br><span> #include <errno.h></span><br><span> #include <ctype.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <regex.h></span><br><span> #include <inttypes.h></span><br><span> #include <sys/fcntl.h></span><br><span> #include <sys/stat.h></span><br><span>@@ -842,33 +843,20 @@</span><br><span> </span><br><span> static bool check_unitid_fmt(const char* unit_id)</span><br><span> {</span><br><span style="color: hsl(0, 100%, 40%);">-    const char *p = unit_id;</span><br><span style="color: hsl(0, 100%, 40%);">-        bool must_digit = true;</span><br><span style="color: hsl(0, 100%, 40%);">- uint8_t remain_slash = 2;</span><br><span style="color: hsl(120, 100%, 40%);">+     regex_t regexp;</span><br><span style="color: hsl(120, 100%, 40%);">+       int rc;</span><br><span> </span><br><span>  if (strlen(unit_id) < 5)</span><br><span>          goto wrong_fmt;</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-     while (*p != '\0') {</span><br><span style="color: hsl(0, 100%, 40%);">-            if (*p != '/' && !isdigit(*p))</span><br><span style="color: hsl(0, 100%, 40%);">-                  goto wrong_fmt;</span><br><span style="color: hsl(0, 100%, 40%);">-         if (*p == '/' && must_digit)</span><br><span style="color: hsl(0, 100%, 40%);">-                    goto wrong_fmt;</span><br><span style="color: hsl(0, 100%, 40%);">-         if (*p == '/') {</span><br><span style="color: hsl(0, 100%, 40%);">-                        must_digit = true;</span><br><span style="color: hsl(0, 100%, 40%);">-                      remain_slash--;</span><br><span style="color: hsl(0, 100%, 40%);">-                 if (remain_slash < 0)</span><br><span style="color: hsl(0, 100%, 40%);">-                                goto wrong_fmt;</span><br><span style="color: hsl(0, 100%, 40%);">-         } else {</span><br><span style="color: hsl(0, 100%, 40%);">-                        must_digit = false;</span><br><span style="color: hsl(0, 100%, 40%);">-             }</span><br><span style="color: hsl(0, 100%, 40%);">-               p++;</span><br><span style="color: hsl(0, 100%, 40%);">-    }</span><br><span style="color: hsl(120, 100%, 40%);">+     rc = regcomp(&regexp, "^[0-9]+/[0-9]+/[0-9]+$", REG_EXTENDED | REG_NOSUB);</span><br><span style="color: hsl(120, 100%, 40%);">+      OSMO_ASSERT(!rc);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-   if (*(p-1) == '/')</span><br><span style="color: hsl(0, 100%, 40%);">-              goto wrong_fmt;</span><br><span style="color: hsl(120, 100%, 40%);">+       rc = regexec(&regexp, unit_id, 0, NULL, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+       regfree(&regexp);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-       return true;</span><br><span style="color: hsl(120, 100%, 40%);">+  if (rc == 0)</span><br><span style="color: hsl(120, 100%, 40%);">+          return true;</span><br><span> </span><br><span> wrong_fmt:</span><br><span>       fprintf(stderr, "ERROR: unit-id wrong format. Must be '\\d+/\\d+/\\d+'\n");</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/13535">change 13535</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/13535"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: osmo-bsc </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Id613bf650833dd38eaad08fdfffdf8dbe2f002b1 </div>
<div style="display:none"> Gerrit-Change-Number: 13535 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Vadim Yanitskiy <axilirator@gmail.com> </div>