Re: [PATCH] Change some arguments to gdb::string_view instead of name+len

Pedro Alves
2019-10-02T14:45:47Z

On 10/1/19 8:29 PM, Christian Biesinger wrote:
> On Tue, Oct 1, 2019 at 2:10 PM Pedro Alves <palves@redhat.com> wrote:

>> I wonder then, I assume that the caller up the stack should know whether
>> the string was originally null terminated? I wonder about tweaking the
>> interface to pass that info down somehow.
>
> Actually, let me come at this from a different direction...
>
> I looked at all the callers. In every case, they either passed a
> nullterminated string or a substring of an existing string. In other
> words, accessing name[length] was always valid, even if the
> string_view wasn't nullterminated.
>
> In light of that, how do you feel about just documenting that callers
> have to pass a string view where accessing [length] is guaranteed not
> to be an invalid memory access?

My feeling is that one of the points of vocabulary types like string_view is
that you replace documentation with rules or contracts enforced by, or
encoded in the types. To me, it feels like a bit of a hack or design issue that
we switch to string_view while at the same time, find that we need to step
outside its contract. A char * pointer is low level and as such forces
you into a "what exactly am I allowed to pass here?" mind state, while
a string_view has a contract that people should just be able to
assume.

OTOH, I don't feel that strongly about this case, there are
certainly bigger fish to fry!

So I guess I'll be happy with just documenting. But
please don't take that as approval. I'd rather defer to Tromey,
since he had started the review, and I haven't even looked at the
whole patch in detail.

Thanks,
Pedro Alves