mirror of
https://github.com/curl/curl.git
synced 2026-01-18 17:21:26 +01:00
tool_getparam: simplify the --rate parser
Avoids a memcpy() Closes #20119
This commit is contained in:
@@ -980,31 +980,23 @@ static ParameterError set_rate(const char *nextarg)
|
||||
/d == per day (24 hours)
|
||||
*/
|
||||
ParameterError err = PARAM_OK;
|
||||
const char *div = strchr(nextarg, '/');
|
||||
char number[26];
|
||||
long denominator;
|
||||
long numerator = 60 * 60 * 1000; /* default per hour */
|
||||
size_t numlen = div ? (size_t)(div - nextarg) : strlen(nextarg);
|
||||
if(numlen > sizeof(number) - 1)
|
||||
return PARAM_NUMBER_TOO_LARGE;
|
||||
const char *p = nextarg;
|
||||
curl_off_t denominator;
|
||||
curl_off_t numerator = 60 * 60 * 1000; /* default per hour */
|
||||
|
||||
memcpy(number, nextarg, numlen);
|
||||
number[numlen] = 0;
|
||||
err = str2unum(&denominator, number);
|
||||
if(err)
|
||||
return err;
|
||||
if(curlx_str_number(&p, &denominator, CURL_OFF_T_MAX))
|
||||
return PARAM_BAD_NUMERIC;
|
||||
|
||||
if(denominator < 1)
|
||||
return PARAM_BAD_USE;
|
||||
|
||||
if(div) {
|
||||
if(!curlx_str_single(&p, '/')) {
|
||||
curl_off_t numunits;
|
||||
div++;
|
||||
|
||||
if(curlx_str_number(&div, &numunits, CURL_OFF_T_MAX))
|
||||
if(curlx_str_number(&p, &numunits, CURL_OFF_T_MAX))
|
||||
numunits = 1;
|
||||
|
||||
switch(*div) {
|
||||
switch(*p) {
|
||||
case 's': /* per second */
|
||||
numerator = 1000;
|
||||
break;
|
||||
@@ -1022,14 +1014,14 @@ static ParameterError set_rate(const char *nextarg)
|
||||
break;
|
||||
}
|
||||
|
||||
if((LONG_MAX / numerator) < numunits) {
|
||||
if((CURL_OFF_T_MAX / numerator) < numunits) {
|
||||
/* overflow, too large number */
|
||||
errorf("too large --rate unit");
|
||||
err = PARAM_NUMBER_TOO_LARGE;
|
||||
}
|
||||
else
|
||||
/* this typecast is okay based on the check above */
|
||||
numerator *= (long)numunits;
|
||||
numerator *= numunits;
|
||||
}
|
||||
|
||||
if(err)
|
||||
|
||||
Reference in New Issue
Block a user