public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/25329] incorrect evaluated address for struct fields
       [not found] <bug-25329-4717@http.sourceware.org/bugzilla/>
@ 2021-07-19 14:49 ` shahab.vahedi at gmail dot com
  2021-07-20  5:01 ` vries at gcc dot gnu.org
  1 sibling, 0 replies; 2+ messages in thread
From: shahab.vahedi at gmail dot com @ 2021-07-19 14:49 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=25329

Shahab <shahab.vahedi at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |shahab.vahedi at gmail dot com

--- Comment #1 from Shahab <shahab.vahedi at gmail dot com> ---
This is neither a valid bug for GDB nor GCC nor any compiler for that matter.


The C code used as a demo here is not semantically correct. "sizeof" should be
able to infer the size at _compile_ time and not at the runtime. I guess the
compiler considers the user's struct as one pointer and one integer. Whatever
the compiler does, it will not reflect the number of arguments given to the
running program. Hence the unexpected behaviour.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/25329] incorrect evaluated address for struct fields
       [not found] <bug-25329-4717@http.sourceware.org/bugzilla/>
  2021-07-19 14:49 ` [Bug c++/25329] incorrect evaluated address for struct fields shahab.vahedi at gmail dot com
@ 2021-07-20  5:01 ` vries at gcc dot gnu.org
  1 sibling, 0 replies; 2+ messages in thread
From: vries at gcc dot gnu.org @ 2021-07-20  5:01 UTC (permalink / raw)
  To: gdb-prs

https://sourceware.org/bugzilla/show_bug.cgi?id=25329

Tom de Vries <vries at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |vries at gcc dot gnu.org
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |MOVED

--- Comment #2 from Tom de Vries <vries at gcc dot gnu.org> ---
I've rewritten the example to this:
...
$ cat test.c
#include <stdio.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  struct S
  {
    int a[argc];
    int x;
  };
  struct S *s;
  size_t n = sizeof (*s);
  printf ("size: %zd\n", n);
  s = malloc (n);
  printf ("s       : %p\n", s);
  printf ("&s->a[1]: %p\n", &s->a[1]);
  printf ("&s->x   : %p\n", &s->x);
  return 0;
}
...
and:
...
$ gcc-11 -g test.c
$ gdb -q -batch a.out -ex "start a b c" -ex "next 9" -ex "p s" -ex "p &s->a[1]"
-ex "p &s->x"
Temporary breakpoint 1 at 0x400556: file test.c, line 7.

Temporary breakpoint 1, main (argc=4, argv=0x7fffffffdcc8) at test.c:7
7         struct S
size: 20
s       : 0x602670
&s->a[1]: 0x602674
&s->x   : 0x602680
19        return 0;
$1 = (struct S *) 0x602670
$2 = (int *) 0x602674
$3 = (int *) 0x602670
$
...

Here ( https://en.cppreference.com/w/c/language/sizeof ) I find:
...
If the type of expression is a variable-length array type, expression is
evaluated and the size of the array it evaluates to is calculated at run time.
(since C99)
...

So I'm not sure if this is invalid in all versions of C.

Looking at the generated debug info:
...
 <3><1e6>: Abbrev Number: 14 (DW_TAG_member)
    <1e7>   DW_AT_name        : x
    <1e9>   DW_AT_decl_file   : 1
    <1ea>   DW_AT_decl_line   : 10
    <1eb>   DW_AT_decl_column : 9
    <1ec>   DW_AT_type        : <0x12a>
    <1f0>   DW_AT_data_member_location: 0
...
it seems that gdb doesn't do anything wrong, it just works with the information
given by gcc.

This was filed as https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61132 .

Closing as resolved-moved.

-- 
You are receiving this mail because:
You are on the CC list for the bug.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-07-20  5:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-25329-4717@http.sourceware.org/bugzilla/>
2021-07-19 14:49 ` [Bug c++/25329] incorrect evaluated address for struct fields shahab.vahedi at gmail dot com
2021-07-20  5:01 ` vries at gcc dot gnu.org

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).