public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: Andrew Burgess <aburgess@redhat.com>,
	Simon Marchi <simark@simark.ca>,
	gdb-patches@sourceware.org
Subject: Re: [PATCH] gdb: fix missing null-character when using value_cstring
Date: Tue, 11 Apr 2023 13:58:06 +0100	[thread overview]
Message-ID: <56b5d8f3-463e-7a73-3cf6-4da661172d4f@palves.net> (raw)
In-Reply-To: <87r0sxb0zy.fsf@redhat.com>

On 2023-04-06 2:20 p.m., Andrew Burgess via Gdb-patches wrote:
> Simon Marchi <simark@simark.ca> writes:
> 
>> On 4/3/23 17:49, Andrew Burgess via Gdb-patches wrote:

>>> Alternatively, if we change the requirements of value_cstring so that
>>> we require the passed length includes the trailing null, then we can
>>> assert that this is indeed the case within value_cstring.  Any
>>> incorrect uses in the future will be quickly spotted.
>>>
>>> So that's what I did, c_string_operation::evaluate is left unchanged,
>>> but every other use of value_cstring is adjusted with a '+ 1' so that
>>> we include the null within the length being passed.
>>
>> That sounds counterintuitive to me.  With an API of style pointer +
>> length, I don't expect the length to include the null terminator.  It
>> also unnecessarily forces the caller to have a null-terminated version
>> of the string, which may not always be the case (you might want to call
>> value_cstring on a subset of an existing string).
>>
>> I think that:
>>
>> struct value *
>> value_cstring (const char *ptr, ssize_t len, struct type *char_type)
>>
>> should take a length excluding the null terminator, but a null
>> terminator in the result (its job is to build a C string, and a C string
>> requires a null terminator at the end).
> 
> This is why writing comments is so important :)
> 
> I read it as "build a value* from this C-string", which is why I figured
> we can assume there will be a '\0' at the end.
> 
> Anyway, I don't really mind either way, just so long as we can get
> something that works!  I'll flip this around to the way you suggest and
> repost.
> 

Hmm, this rang a bell.  See this patch and following discussion:

 https://inbox.sourceware.org/gdb-patches/20210713153142.3957666-1-simon.marchi@efficios.com/

Pedro Alves

  reply	other threads:[~2023-04-11 12:58 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-03 21:49 Andrew Burgess
2023-04-04 13:58 ` Simon Marchi
2023-04-06 13:20   ` Andrew Burgess
2023-04-11 12:58     ` Pedro Alves [this message]
2023-04-12 20:47       ` Andrew Burgess
2023-04-13 11:56         ` Pedro Alves
2023-04-07  6:35 ` [PATCHv2] " Andrew Burgess
2023-05-24 14:10   ` [PATCHv3] gdb: building inferior strings from within GDB Andrew Burgess
2023-05-24 15:42     ` Simon Marchi
2023-06-05 12:26       ` Andrew Burgess
2023-06-05 17:57         ` Simon Marchi
2023-06-06 15:50           ` Andrew Burgess
2023-06-09 13:41             ` Tom Tromey
2023-06-09 14:20               ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56b5d8f3-463e-7a73-3cf6-4da661172d4f@palves.net \
    --to=pedro@palves.net \
    --cc=aburgess@redhat.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simark@simark.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).