Re: [PATCH 4/4] gdb: fix value_subscript when array upper bound is not known

Simon Marchi via Gdb-patches
2020-12-09T18:51:04Z

On 2020-12-07 10:14 p.m., Joel Brobecker wrote:
> [just for the sake of discussing generalities, rather than suggesting
> modifications to your patch]
>
>>> Now, the bigger question is: Why are we even getting to this point?
>>> Shouldn't the bounds simply be resolved before we do the subscripting?
>>
>> If the array is c-style, there's no concept of high bound, so I don't think
>> there's a point resolving the high bound then.
>
> I believe you sometimes do, for intance in...
>
> char something[5];

Right, but the language doesn't do bound checks, so I don't think the debugger
should either. What I meant is that there's no need to resolve the upper bound
for c-style arrays, because we don't check the upper bound.

>> Again, I've never really been a user of such a language. Intuitively, I'd
>> like the debugger to work as close to the source language as possible, so
>> I'd like it to validate the bounds in my expression. But it might be that
>> people who use these languages sometimes need to side-step the language's
>> protections.
>
> Speaking for myself, sometimes I indeed want to go beyond.
>
> Even in C, when we have structures looking like this (don't know if we
> have any of those in our code anymore):
>
> struct something
> {
> size_t n_elems;
> int buf[0];
> };
>
> You'd know that you need to go beyond the first element of the array
> to get to the rest of the data...

I would imagine that with a language that is more strict and does bound-checks
when accessing arrays, this kind of trick wouldn't exist. So there probably
wouldn't be legitimate use cases for doing out-of-bounds accesses.

Simon