Re: [PATCH 6/6] parse-options: add precision handling for OPTION_COUNTUP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 7/1/25 12:55 PM, Patrick Steinhardt wrote:
> On Sun, Jun 29, 2025 at 01:51:36PM +0200, René Scharfe wrote:
>> diff --git a/parse-options.c b/parse-options.c
>> index 0dc9b0324a..0dd08a3a77 100644
>> --- a/parse-options.c
>> +++ b/parse-options.c
>> @@ -166,10 +166,22 @@ static enum parse_opt_result do_get_value(struct parse_opt_ctx_t *p,
>>  	}
>>  
>>  	case OPTION_COUNTUP:
>> -		if (*(int *)opt->value < 0)
>> -			*(int *)opt->value = 0;
>> -		*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
>> -		return 0;
>> +	{
>> +		size_t bits = CHAR_BIT * opt->precision;
>> +		intmax_t upper_bound = INTMAX_MAX >> (bitsizeof(intmax_t) - bits);
>> +		intmax_t value = get_int_value(opt);
>> +
>> +		if (value < 0)
>> +			value = 0;
>> +		if (unset)
>> +			value = 0;
>> +		else if (value < upper_bound)
>> +			value++;
>> +		else
>> +			return error(_("value for %s exceeds %"PRIdMAX),
>> +				     optname(opt, flags), upper_bound);
>> +		return set_int_value(opt, flags, value);
>> +	}
>>  
>>  	case OPTION_SET_INT:
>>  		return set_int_value(opt, flags, unset ? 0 : opt->defval);
>> @@ -630,10 +642,10 @@ static void parse_options_check(const struct option *opts)
>>  		case OPTION_BIT:
>>  		case OPTION_NEGBIT:
>>  		case OPTION_BITOP:
>> +		case OPTION_COUNTUP:
>>  			if (!signed_int_fits(opts->defval, opts->precision))
>>  				optbug(opts, "has invalid defval");
>>  			/* fallthru */
>> -		case OPTION_COUNTUP:
>>  		case OPTION_NUMBER:
>>  			if ((opts->flags & PARSE_OPT_OPTARG) ||
>>  			    !(opts->flags & PARSE_OPT_NOARG))
>> diff --git a/parse-options.h b/parse-options.h
>> index 8bdf469ae9..312045604d 100644
>> --- a/parse-options.h
>> +++ b/parse-options.h
>> @@ -183,6 +183,7 @@ struct option {
>>  	.short_name = (s), \
>>  	.long_name = (l), \
>>  	.value = (v), \
> 
> It's a bit surprising that `COUNTUP` accepts a signed integer, so should
> we maybe add `BARF_UNLESS_SIGNED(*(v))` here?

Perhaps, but that would require more changes to callers that use unsigned
variables than I can stomach.  That's why I declared it out of scope for
this series in its cover letter.  Later, unless (hopefully) someone beats
me to it.

> 
>> +	.precision = sizeof(*v), \
>>  	.help = (h), \
>>  	.flags = PARSE_OPT_NOARG|(f), \
>>  }
> 
> Patrick






[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux