]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: make url2ipv4 return the exact number of bytes parsed
authorWilly Tarreau <w@1wt.eu>
Thu, 25 Mar 2021 10:34:40 +0000 (11:34 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 25 Mar 2021 14:18:47 +0000 (15:18 +0100)
The function's return value is currently used as a boolean but we'll
need it to return the number of bytes parsed. Right now it returns
it minus one, unless the last char doesn't match what is permitted.
Let's update this to make it more usable.

src/tools.c

index 98adb322668687ca2475f1d6bc6ebe88cb9c39c7..546ad4416a6f158209bb4b8caaffe2ff63d1ae13 100644 (file)
@@ -1447,7 +1447,9 @@ int str62net(const char *str, struct in6_addr *addr, unsigned char *mask)
 
 
 /*
- * Parse IPv4 address found in url.
+ * Parse IPv4 address found in url. Return the number of bytes parsed. It
+ * expects exactly 4 numbers between 0 and 255 delimited by dots, and returns
+ * zero in case of mismatch.
  */
 int url2ipv4(const char *addr, struct in_addr *dst)
 {
@@ -1460,9 +1462,10 @@ int url2ipv4(const char *addr, struct in_addr *dst)
        *(tp = tmp) = 0;
 
        while (*addr) {
-               unsigned char digit = (ch = *addr++) - '0';
+               unsigned char digit = (ch = *addr) - '0';
                if (digit > 9 && ch != '.')
                        break;
+               addr++;
                if (digit <= 9) {
                        u_int new = *tp * 10 + digit;
                        if (new > 255)
@@ -1486,7 +1489,7 @@ int url2ipv4(const char *addr, struct in_addr *dst)
                return 0;
 
        memcpy(&dst->s_addr, tmp, 4);
-       return addr-cp-1;
+       return addr - cp;
 }
 
 /*