From: Daniel Stenberg Date: Wed, 31 Dec 2025 17:17:15 +0000 (+0100) Subject: tool_paramhlp: simplify number parsing X-Git-Tag: curl-8_18_0~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c7b26b6679e2efebb37ff9735c882384035c338a;p=thirdparty%2Fcurl.git tool_paramhlp: simplify number parsing Closes #20134 --- diff --git a/src/tool_paramhlp.c b/src/tool_paramhlp.c index 3f8213835a..753feca424 100644 --- a/src/tool_paramhlp.c +++ b/src/tool_paramhlp.c @@ -203,47 +203,39 @@ ParameterError file2memory(char **bufp, size_t *size, FILE *file) * getparameter a lot, we must check it for NULL before accessing the str * data. */ -static ParameterError getnum(long *val, const char *str, int base) -{ - DEBUGASSERT((base == 8) || (base == 10)); - if(str) { - curl_off_t num; - bool is_neg = FALSE; - if(base == 10) { - is_neg = (*str == '-'); - if(is_neg) - str++; - if(curlx_str_number(&str, &num, LONG_MAX)) - return PARAM_BAD_NUMERIC; - } - else { /* base == 8 */ - if(curlx_str_octal(&str, &num, LONG_MAX)) - return PARAM_BAD_NUMERIC; - } - if(!curlx_str_single(&str, '\0')) { - *val = (long)num; - if(is_neg) - *val = -*val; - return PARAM_OK; /* Ok */ - } - } - return PARAM_BAD_NUMERIC; /* badness */ -} - ParameterError str2num(long *val, const char *str) { - return getnum(val, str, 10); + curl_off_t num; + bool is_neg = FALSE; + DEBUGASSERT(str); + if(!curlx_str_single(&str, '-')) + is_neg = TRUE; + if(curlx_str_number(&str, &num, LONG_MAX) || + curlx_str_single(&str, '\0')) + return PARAM_BAD_NUMERIC; /* badness */ + + *val = (long)num; + if(is_neg) + *val = -*val; + return PARAM_OK; /* Ok */ } ParameterError oct2nummax(long *val, const char *str, long max) { - ParameterError result = getnum(val, str, 8); - if(result != PARAM_OK) - return result; - else if(*val > max) - return PARAM_NUMBER_TOO_LARGE; - else if(*val < 0) + curl_off_t num; + int rc; + DEBUGASSERT(str); + rc = curlx_str_octal(&str, &num, max); + if(rc) { + if(STRE_OVERFLOW == rc) + return PARAM_NUMBER_TOO_LARGE; + return PARAM_BAD_NUMERIC; + } + if(curlx_str_single(&str, '\0')) + return PARAM_BAD_NUMERIC; + if(num < 0) return PARAM_NEGATIVE_NUMERIC; + *val = (long)num; return PARAM_OK; } @@ -259,7 +251,7 @@ ParameterError oct2nummax(long *val, const char *str, long max) ParameterError str2unum(long *val, const char *str) { - ParameterError result = getnum(val, str, 10); + ParameterError result = str2num(val, str); if(result != PARAM_OK) return result; if(*val < 0)