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-Url: http://git.haproxy.org/?a=commitdiff_plain;h=2d577d527d71449ce1e5f4c6e3612a2618eaa4cf;p=haproxy-2.1.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. (cherry picked from commit 12e1027aa6c7687b5638c0c9260696266c9e400b) Signed-off-by: Willy Tarreau (cherry picked from commit 4d61bdbe244f43d7c1c9356564ee5863542c005b) Signed-off-by: Willy Tarreau (cherry picked from commit 3fe1c0e6b86bd4647f1390769f54ce6a24e7fb58) Signed-off-by: Willy Tarreau --- diff --git a/src/standard.c b/src/standard.c index 4e67b9d..ab429d8 100644 --- a/src/standard.c +++ b/src/standard.c @@ -1228,7 +1228,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) { @@ -1241,9 +1243,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) @@ -1267,7 +1270,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; } /*