public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102022] New: incorrect code with -O2
@ 2021-08-23 10:27 scu319hy at msn dot com
  2021-08-23 10:30 ` [Bug c++/102022] " marxin at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: scu319hy at msn dot com @ 2021-08-23 10:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102022

            Bug ID: 102022
           Summary: incorrect code with -O2
           Product: gcc
           Version: 10.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: scu319hy at msn dot com
  Target Milestone: ---

following code doesn't works well with -O2 flag.

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>

struct _node_t {
        _node_t * _next;
};
static void _rebase_ptr(char *&ptr, void *old_base, void *new_base) {
        if ( ptr != 0 ) {
                ptr = (char *)((char *)(ptr) - (char *)(old_base) + (char
*)(new_base));
        }
}

static void _rebase_ptr( _node_t *&head, void *old_base, void *new_base ) {
        _rebase_ptr(*(char**)&head, old_base, new_base);
        // error!! value of "head" not changed

        _node_t * node = head;
        while( node != 0 ) {
                _rebase_ptr( *(char**)&(node->_next), old_base, new_base );
                node = node->_next;
        }
}

int main() {
        _node_t *tmp = (_node_t *)calloc(1, sizeof(_node_t));
        _node_t *tmp2 = (_node_t *)calloc(1, sizeof(_node_t));
        memcpy(tmp2, tmp, sizeof(_node_t));
        free(tmp);

        _rebase_ptr(tmp, tmp, tmp2);
        printf("%llx == %llx\n", (long long)tmp, (long long)tmp2);
        free(tmp);
        return 0;
}

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

* [Bug c++/102022] incorrect code with -O2
  2021-08-23 10:27 [Bug c++/102022] New: incorrect code with -O2 scu319hy at msn dot com
@ 2021-08-23 10:30 ` marxin at gcc dot gnu.org
  2021-08-23 10:33 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-08-23 10:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102022

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hubicka at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-08-23
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
Started with r6-1220-g6e042ef4e2b96c07.

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

* [Bug c++/102022] incorrect code with -O2
  2021-08-23 10:27 [Bug c++/102022] New: incorrect code with -O2 scu319hy at msn dot com
  2021-08-23 10:30 ` [Bug c++/102022] " marxin at gcc dot gnu.org
@ 2021-08-23 10:33 ` pinskia at gcc dot gnu.org
  2021-08-23 10:36 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-23 10:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102022

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This part of the code looks like there could be a huge alias violation waiting
to happen
*(char**)&head

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

* [Bug c++/102022] incorrect code with -O2
  2021-08-23 10:27 [Bug c++/102022] New: incorrect code with -O2 scu319hy at msn dot com
  2021-08-23 10:30 ` [Bug c++/102022] " marxin at gcc dot gnu.org
  2021-08-23 10:33 ` pinskia at gcc dot gnu.org
@ 2021-08-23 10:36 ` pinskia at gcc dot gnu.org
  2021-08-23 13:22 ` rguenth at gcc dot gnu.org
  2021-08-23 13:24 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-23 10:36 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102022

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
And yes it is.
You write head as both char* and then read it as head*. That is an alias
violation for sure. Use either -fno-strict-aliasing go figure out a way to use
an union or memcpy.

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

* [Bug c++/102022] incorrect code with -O2
  2021-08-23 10:27 [Bug c++/102022] New: incorrect code with -O2 scu319hy at msn dot com
                   ` (2 preceding siblings ...)
  2021-08-23 10:36 ` pinskia at gcc dot gnu.org
@ 2021-08-23 13:22 ` rguenth at gcc dot gnu.org
  2021-08-23 13:24 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-08-23 13:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102022

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
You could use *(void **) which GCC(!) treats conservative.  But yes, *(char
**)&head should use memcpy to be truly portable.

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

* [Bug c++/102022] incorrect code with -O2
  2021-08-23 10:27 [Bug c++/102022] New: incorrect code with -O2 scu319hy at msn dot com
                   ` (3 preceding siblings ...)
  2021-08-23 13:22 ` rguenth at gcc dot gnu.org
@ 2021-08-23 13:24 ` jakub at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-08-23 13:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102022

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Or when you already use C++ change _rebase_ptr into a function template that
will handle T *&ptr for template parameter T.

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

end of thread, other threads:[~2021-08-23 13:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-23 10:27 [Bug c++/102022] New: incorrect code with -O2 scu319hy at msn dot com
2021-08-23 10:30 ` [Bug c++/102022] " marxin at gcc dot gnu.org
2021-08-23 10:33 ` pinskia at gcc dot gnu.org
2021-08-23 10:36 ` pinskia at gcc dot gnu.org
2021-08-23 13:22 ` rguenth at gcc dot gnu.org
2021-08-23 13:24 ` jakub 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).