Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <pedro@palves.net>
To: Kevin Buettner <kevinb@redhat.com>
Cc: gdb-patches@sourceware.org, Tom Tromey <tom@tromey.com>,
	Tom de Vries <tdevries@suse.de>
Subject: Re: [PATCH] [gdb/exp] Fix cast handling for indirection
Date: Sat, 4 May 2024 00:26:49 +0100	[thread overview]
Message-ID: <1b81045d-5353-49cf-b80e-f64498dd3562@palves.net> (raw)
In-Reply-To: <c0bcf771-6aa1-4209-b3c7-cc2f487e2e46@palves.net>



On 2024-05-04 00:17, Pedro Alves wrote:
> On 2024-05-03 18:30, Kevin Buettner wrote:
>> On Fri, 3 May 2024 17:04:41 +0100
>> Pedro Alves <pedro@palves.net> wrote:
>>
>>>> I was wondering if this patch causes gdb to accept some weird things
>>>> that might have been rejected in the past, by introducing a hidden cast.
>>>> Maybe print (char) *85732 does something surprising now.  I'm not
>>>> entirely sure if that's bad.  
>>>
>>> I am totally surprised that:
>>>
>>>  +# Regression test for PR31693.
>>>  +gdb_test "p (char)*a_loc ()" " = 97 'a'"
>>>
>>> this actually works, instead of telling the user:
>>>
>>>   "'a_loc' has unknown return type; cast the call to its declared return type"
>>>
>>> It seems like a misfeature to me to assume that "char *" is the right type.
>>>
>>> Thus, I don't agree with the patch.
>>
>> Using a GDB built with Tom de Vries's patch, I see:
>>
>> (gdb) p *a_loc()
>> 'a_loc' has unknown return type; cast the call to its declared return type
>>
>> This is the same as the pre-patch behavior.
>>
>> With Tom's patch, GDB now infers the function's return type, based
>> on the cast:
>>
>> (gdb) p (char)*a_loc()
>> $1 = 97 'a'
>>
> 
> But that is not what GDB told you to do.  It told you to cast the _call_,
> not the result of de-referencing the result of the call.  
> It is telling cast to the declared return type, which is "char *".  I.e.,
> it is telling you to write:
> 
>  (gdb) p *(char *)a_loc()
> 
> See 7022349d5c86 ("Stop assuming no-debug-info functions return int").
> 
> This cast here:
> 
>  (gdb) p (char)*a_loc()
> 
> ... should not affect the call's return type.  That is decided before
> the * operator is involved.  In the same way, this:
> 
>  (gdb) p (long long)*a_loc()
> 
> should not result in gdb assuming that a_loc() returns a "long long *",
> that it wrong.  It should still error out with
> 
>   'a_loc' has unknown return type; cast the call to its declared return type
> 
> and so the user should write:
> 
>  (gdb) p (long long) *(char *)a_loc()
> 
> and then with this last expression a proper sign extension is applied when
> char is converted to long long, if char is signed.  I.e., in steps:
> 
>  1: (char *)a_loc() -> call, and get char * return value
>  2: *(char *)a_loc() -> deref, and get char value
>  3: (long long) *(char *)a_loc() -> sign extend char value -> long long value
> 

I kind of emphasized the sign extension part above, but to be clear, with

  (gdb) p (long long)*a_loc()

and gdb assuming that means a_loc returns "long long *", gdb incorrectly reads a
64-bit value off of the pointer address, which is totally bogus and would not
be what gdb would do if it had debug info for a_loc(), in which case GDB would
know that it returns char *, and thus would deref only one byte and behave like
described above in the 1: 2: 3: steps.  The behavior of the expression should not
change like that depending on whether you have debug info.  Thus, GDB should error
out.

  reply	other threads:[~2024-05-03 23:27 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-02 15:49 Tom de Vries
2024-05-03  2:31 ` Kevin Buettner
2024-05-03  7:37   ` Tom de Vries
2024-05-03 15:27   ` Tom Tromey
2024-05-03 16:04     ` Pedro Alves
2024-05-03 17:30       ` Kevin Buettner
2024-05-03 23:17         ` Pedro Alves
2024-05-03 23:26           ` Pedro Alves [this message]
2024-05-06  0:54             ` Kevin Buettner
2024-05-06  6:52               ` Tom de Vries

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=1b81045d-5353-49cf-b80e-f64498dd3562@palves.net \
    --to=pedro@palves.net \
    --cc=gdb-patches@sourceware.org \
    --cc=kevinb@redhat.com \
    --cc=tdevries@suse.de \
    --cc=tom@tromey.com \
    /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