From: Willy Tarreau Date: Thu, 25 Mar 2021 10:34:40 +0000 (+0100) Subject: MINOR: tools: make url2ipv4 return the exact number of bytes parsed X-Git-Tag: v2.4-dev14~30 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=12e1027aa6c7687b5638c0c9260696266c9e400b;p=thirdparty%2Fhaproxy.git MINOR: tools: make url2ipv4 return the exact number of bytes parsed 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. --- diff --git a/src/tools.c b/src/tools.c index 98adb32266..546ad4416a 100644 --- a/src/tools.c +++ b/src/tools.c @@ -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; } /*