From: Willy Tarreau Date: Thu, 19 Nov 2026 16:26:44 +0000 (+0100) Subject: MINOR: tools: add some of the missing null-terminated integer parsers X-Git-Url: https://git.kaiwu.me/http/%22../static/gitweb.js?a=commitdiff_plain;h=b923aa9d2df98b20bc0fa610876e4fc6496f1309;p=haproxy.git MINOR: tools: add some of the missing null-terminated integer parsers We have a number of integer parsers depending on what delimits the integer, but the null-terminated ones were still fairly limited, especially when range checks were expected. This commit adds the following parsers: int str2irc(const char *str, int *ret); int str2uirc(const char *str, uint *ret); int str2lrc(const char *str, long *ret); int str2ulrc(const char *str, ulong *ret); int str2llrc(const char *str, llong *ret); int str2ullrc(const char *str, ullong *ret); The all return an error status like their equivalent length-defined ones, with the returned value via a pointer. They stop on a null character and return parsing error when non-digit chars appear in the string, or when too large a number is being produced for the range. Note that for now they're in tools.c and defined in intops.h because other ones were done similarly, but all of these should move to a new intops.c file. --- diff --git a/include/haproxy/intops.h b/include/haproxy/intops.h index 3195dda9b..1475d3d6e 100644 --- a/include/haproxy/intops.h +++ b/include/haproxy/intops.h @@ -36,6 +36,12 @@ unsigned int str2ui(const char *s); unsigned int str2uic(const char *s); unsigned int strl2ui(const char *s, int len); unsigned int strl2uic(const char *s, int len); +int str2irc(const char *str, int *ret); +int str2uirc(const char *str, uint *ret); +int str2lrc(const char *str, long *ret); +int str2ulrc(const char *str, ulong *ret); +int str2llrc(const char *str, llong *ret); +int str2ullrc(const char *str, ullong *ret); int strl2ic(const char *s, int len); int strl2irc(const char *s, int len, int *ret); int strl2llrc(const char *s, int len, long long *ret); diff --git a/src/tools.c b/src/tools.c index 83a28cc31..2b14ca98f 100644 --- a/src/tools.c +++ b/src/tools.c @@ -2808,6 +2808,169 @@ int strl2ic(const char *s, int len) return i; } +/* The str{,l,ll}2{,u}irc() functions read non-NUL chars from and convert + * them to a signed or unsigned int/long/long long which they store into . + * They accurately detects any error (empty/truncated string, invalid chars, + * positive/negative overflows). They're meant to be used on exposed contents. + * Contrary to the libc's strtoxxx() family, they're locale-agnostic and meant + * to be very fast. They're built around an inline version for the core loop, + * and the user-visible parts call it and apply sign/range checks. They return + * an error code 0 when the number has successfully been converted, non-zero + * otherwise. When an error is returned, the value is left untouched. + */ + +/* generator for the int/uint/long/ulong/llong/ullong variants, do not use + * outside this file! + */ +#define __str2uirc(type, str, lim, err) ({ \ + __label__ _done; \ + const char *s = (str); \ + const type _lim = (lim); \ + int *_err = (err); \ + type _i = 0; \ + type _j = 0; \ + while ((_j = (type)(*s++ - '0')) <= 9) { \ + if (_i > _lim / 10) { \ + *_err = 1; /* multiply overflow */ \ + goto _done; \ + } \ + _i = _i * 10; \ + if (_i + _j < _i || _i + _j > _lim) { \ + *_err = 1; /* add overflow */ \ + goto _done; \ + } \ + _i = _i + _j; \ + } \ + if (_j != (type)(0 - '0')) \ + *_err = 1; /* non-NUL ? invalid char */ \ + else if (s == str + 1) \ + *_err = 1; /* empty */ \ +_done: \ + _i; \ +}) + +/* core for the int/uint variant */ +static forceinline uint _str2uirc(const char *str, uint lim, int *err) +{ + return __str2uirc(uint, str, lim, err); +} + +/* core for the long/ulong variant */ +static forceinline ulong _str2ulrc(const char *str, ulong lim, int *err) +{ + return __str2uirc(ulong, str, lim, err); +} + +/* core for the long/ulong variant */ +static forceinline ullong _str2ullrc(const char *str, ullong lim, int *err) +{ + return __str2uirc(ullong, str, lim, err); +} + +/* parses uint in , stores in , returns 0 on success, see above. */ +int str2uirc(const char *str, uint *ret) +{ + uint i = 0; + int err = 0; + + i = _str2uirc(str, UINT_MAX, &err); + if (err) + return 1; + + *ret = i; + return 0; +} + +/* parses int in , stores in , returns 0 on success, see above. */ +int str2irc(const char *str, int *ret) +{ + uint lim = INT_MAX; + uint i = 0; + int err = 0; + + if (*str == '-') { + str++; + lim = -(uint)INT_MIN; + } + + i = _str2uirc(str, lim, &err); + if (err) + return 1; + + i = (lim == INT_MAX) ? i : -i; + *ret = (int)i; + return 0; +} + +/* parses ulong in , stores in , returns 0 on success, see above. */ +int str2ulrc(const char *str, ulong *ret) +{ + ulong i = 0; + int err = 0; + + i = _str2ulrc(str, ULONG_MAX, &err); + if (err) + return 1; + + *ret = i; + return 0; +} + +/* parses long in , stores in , returns 0 on success, see above. */ +int str2lrc(const char *str, long *ret) +{ + ulong lim = LONG_MAX; + ulong i = 0; + int err = 0; + + if (*str == '-') { + str++; + lim = -(ulong)LONG_MIN; + } + + i = _str2ulrc(str, lim, &err); + if (err) + return 1; + + i = (lim == LONG_MAX) ? i : -i; + *ret = (long)i; + return 0; +} + +/* parses ullong in , stores in , returns 0 on success, see above. */ +int str2ullrc(const char *str, ullong *ret) +{ + ullong i = 0; + int err = 0; + + i = _str2ullrc(str, ULLONG_MAX, &err); + if (err) + return 1; + + *ret = i; + return 0; +} + +/* parses llong in , stores in , returns 0 on success, see above. */ +int str2llrc(const char *str, llong *ret) +{ + ullong lim = LLONG_MAX; + ullong i = 0; + int err = 0; + + if (*str == '-') { + str++; + lim = -(ullong)LLONG_MIN; + } + + i = _str2ullrc(str, lim, &err); + if (err) + return 1; + + i = (lim == LLONG_MAX) ? i : -i; + *ret = (llong)i; + return 0; +} /* This function reads exactly chars from and converts them to a * signed integer which it stores into . It accurately detects any error