Re: [PATCH 1/2] use zuinteger_unlimited for some remote commands

Pedro Alves
2013-03-04T16:21:01Z

On 03/04/2013 02:37 PM, Yao Qi wrote:
> On 03/04/2013 09:19 PM, Pedro Alves wrote:
>> On 02/15/2013 01:27 PM, Yao Qi wrote:
>>> -int remote_hw_watchpoint_limit = -1;
>>> -int remote_hw_watchpoint_length_limit = -1;
>>> -int remote_hw_breakpoint_limit = -1;
>>> +static unsigned int remote_hw_watchpoint_limit = UINT_MAX;
>>> +static unsigned int remote_hw_watchpoint_length_limit = UINT_MAX;
>>> +static unsigned int remote_hw_breakpoint_limit = UINT_MAX;
>>
>> ...
>>
>>> @@ -8259,7 +8255,7 @@ remote_check_watch_resources (int type, int cnt, int ot)
>>> {
>>> if (remote_hw_watchpoint_limit == 0)
>>> return 0;
>>> - else if (remote_hw_watchpoint_limit < 0)
>>> + else if (remote_hw_watchpoint_limit == UINT_MAX)
>>
>> This made me notice something with var_zuinteger_unlimited.
>>
>> What's the point of making it work with unsigned variables,
>> and UINT_MAX, if the contents of the variable are actually
>> treated as int everywhere in cli-setshow.c? (and val is
>> still cut at INT_MAX). Vis, e.g.,
>>
>

(swapping for better answer:)

> That is reason I change type of "remote_hw_watchpoint_limit" from signed to
> unsigned. Their default value is unlimited (-1), so I initialize them to UINT_MAX.
> Originally I used "(unsigned int) -1", but UINT_MAX is better as "unlimited" here.

That's all clear, and a _consequence_ of var_zuinteger_unlimited
wanting an unsigned variable.

> Yes, in cli-setshow.c, the val is treated as signed integer, because -1 stands for unlimited.
> Outside of cli, var_zuinteger_unlimited is about a unsigned integer with a unlimited
> state.

But then, var_zuinteger_unlimited treats numbers
from INT_MAX to UINT_MAX-1 as invalid, and, while
forbidding any other negative number except -1 at
the same time.

(gdb) set listsize 2147483647 (INT_MAX)
integer 2147483647 out of range
(gdb) set listsize 2147483647+1
only -1 is allowed to set as unlimited
(gdb) set listsize 4294967295U (UINT_MAX)
integer 4294967295 out of range
(gdb) set listsize 4294967295U-1
integer 4294967294 out of range

Okay, so I notice that's documented in:

/* ZeroableUnsignedInteger with unlimited value. *VAR is an unsigned
int, but its range is [0, INT_MAX]. -1 stands for unlimited. */
var_zuinteger_unlimited,

But I ask, what's either:

- the point of making it internally signed
with things like:

if (*(int *) c->var != val)

and forbidding INT_MAX..UINT_MAX-1. (Not that I'm
arguing it should). The care to only accept -1
and not any other negative number made me think
numbers in that range should be accepted.

- the point of making it externally unsigned if
it only accepts [0, INT_MAX]. If the variable
assigned to the command was signed too, then this
range would be both implicit and explicit, meaning, one
weird detail less the user of the API needs to know.

BTW, if the variable is unsigned, then I believe accessing it
as signed as cli-setshow.c does is actually undefined behavior.

--
Pedro Alves