public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/66673] New: swapping variables via chained xor fails
@ 2015-06-25 19:23 joe.carnuccio at qlogic dot com
  2015-06-25 19:29 ` [Bug c/66673] " mpolacek at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: joe.carnuccio at qlogic dot com @ 2015-06-25 19:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 66673
           Summary: swapping variables via chained xor fails
           Product: gcc
           Version: 4.4.6
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: joe.carnuccio at qlogic dot com
  Target Milestone: ---

This is the same as 39121 which has been marked RESOLVED INVALID (to which I
strongly disagree):

this produces incorrect executable: *p ^= *q ^= *p ^= *q;
( if gcc option "-Os" is used, then that produces correct executable )

( by contrast, this always produces correct executable: a ^= b ^= a ^= b; )


root@elab305:/home/joe/test/c# cat x.c
#include <stdio.h>

int main(int  argc, char **argv)
{
        int a = 0x32, b = 0x45;
        int *p = &a, *q = &b;

        *p ^= *q ^= *p ^= *q;
        printf("%x %x\n", a, b);

        return 0;
}
root@elab305:/home/joe/test/c# make -B x
cc    -c -o x.o x.c
cc   x.o   -o x
root@elab305:/home/joe/test/c# ./x
0 32                          <--INCORRECT
root@elab305:/home/joe/test/c# make -B x CFLAGS+='-Os'
cc -Os   -c -o x.o x.c
cc   x.o   -o x
root@elab305:/home/joe/test/c# ./x
45 32                         <--CORRECT
root@elab305:/home/joe/test/c#


Notice that when -Os (optimize for space rather than speed) is used, the
executable produces the correct result.


Also, doing the chained xor on the integer variables a and b themselves always
produces the correct result (regardless of optimization).


root@elab305:/home/joe/test/c# gcc --version
gcc (GCC) 4.4.6 20110731 (Red Hat 4.4.6-3)
. . .

root@elab305:/home/joe/test/c# uname -a
Linux elab305 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64
x86_64 x86_64 GNU/Linux

root@elab305:/home/joe/test/c# cat /etc/issue
Red Hat Enterprise Linux Server release 6.2 (Santiago)
Kernel \r on an \m


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

* [Bug c/66673] swapping variables via chained xor fails
  2015-06-25 19:23 [Bug c/66673] New: swapping variables via chained xor fails joe.carnuccio at qlogic dot com
@ 2015-06-25 19:29 ` mpolacek at gcc dot gnu.org
  2015-06-25 19:41 ` joe.carnuccio at qlogic dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-06-25 19:29 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

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

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
And again you're invoking undefined behavior.  Use -Wall.


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

* [Bug c/66673] swapping variables via chained xor fails
  2015-06-25 19:23 [Bug c/66673] New: swapping variables via chained xor fails joe.carnuccio at qlogic dot com
  2015-06-25 19:29 ` [Bug c/66673] " mpolacek at gcc dot gnu.org
@ 2015-06-25 19:41 ` joe.carnuccio at qlogic dot com
  2015-06-25 19:42 ` joe.carnuccio at qlogic dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: joe.carnuccio at qlogic dot com @ 2015-06-25 19:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from joe.carnuccio at qlogic dot com ---
-Wall produces no warnings...

root@elab305:/home/joe/test/c# make -B x -Wall
cc    -c -o x.o x.c
cc   x.o   -o x
root@elab305:/home/joe/test/c# ./x
0 32


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

* [Bug c/66673] swapping variables via chained xor fails
  2015-06-25 19:23 [Bug c/66673] New: swapping variables via chained xor fails joe.carnuccio at qlogic dot com
  2015-06-25 19:29 ` [Bug c/66673] " mpolacek at gcc dot gnu.org
  2015-06-25 19:41 ` joe.carnuccio at qlogic dot com
@ 2015-06-25 19:42 ` joe.carnuccio at qlogic dot com
  2015-06-25 19:46 ` joe.carnuccio at qlogic dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: joe.carnuccio at qlogic dot com @ 2015-06-25 19:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from joe.carnuccio at qlogic dot com ---
Sorry, I ment this:

root@elab305:/home/joe/test/c# make -B x CFLAGS+='-Wall'
cc -Wall   -c -o x.o x.c
cc   x.o   -o x
root@elab305:/home/joe/test/c# ./x
0 32


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

* [Bug c/66673] swapping variables via chained xor fails
  2015-06-25 19:23 [Bug c/66673] New: swapping variables via chained xor fails joe.carnuccio at qlogic dot com
                   ` (2 preceding siblings ...)
  2015-06-25 19:42 ` joe.carnuccio at qlogic dot com
@ 2015-06-25 19:46 ` joe.carnuccio at qlogic dot com
  2015-06-25 19:54 ` trippels at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: joe.carnuccio at qlogic dot com @ 2015-06-25 19:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from joe.carnuccio at qlogic dot com ---
if I do a ^= b ^= a ^= b it always work correctly;

doing *p ^= *q ^= *p ^= *q fails (unless -Os is used);


i.e. dereferenced pointers are being treated differently

int a = 0x32, b = 0x45;
int *p = &a, *q = &b;


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

* [Bug c/66673] swapping variables via chained xor fails
  2015-06-25 19:23 [Bug c/66673] New: swapping variables via chained xor fails joe.carnuccio at qlogic dot com
                   ` (3 preceding siblings ...)
  2015-06-25 19:46 ` joe.carnuccio at qlogic dot com
@ 2015-06-25 19:54 ` trippels at gcc dot gnu.org
  2015-06-25 19:56 ` mpolacek at gcc dot gnu.org
  2015-06-25 21:09 ` [Bug c/66673] Wsequence-point warning missing when swapping variables via chained xor trippels at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-06-25 19:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Markus Trippelsdorf <trippels at gcc dot gnu.org> ---
https://www.google.com/?#q=nasal%20demons&lang=en


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

* [Bug c/66673] swapping variables via chained xor fails
  2015-06-25 19:23 [Bug c/66673] New: swapping variables via chained xor fails joe.carnuccio at qlogic dot com
                   ` (4 preceding siblings ...)
  2015-06-25 19:54 ` trippels at gcc dot gnu.org
@ 2015-06-25 19:56 ` mpolacek at gcc dot gnu.org
  2015-06-25 21:09 ` [Bug c/66673] Wsequence-point warning missing when swapping variables via chained xor trippels at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-06-25 19:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
(In reply to joe.carnuccio from comment #2)
> -Wall produces no warnings...

Oh, you're using too old GCC.  Please try a newer version.


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

* [Bug c/66673] Wsequence-point warning missing when swapping variables via chained xor
  2015-06-25 19:23 [Bug c/66673] New: swapping variables via chained xor fails joe.carnuccio at qlogic dot com
                   ` (5 preceding siblings ...)
  2015-06-25 19:56 ` mpolacek at gcc dot gnu.org
@ 2015-06-25 21:09 ` trippels at gcc dot gnu.org
  6 siblings, 0 replies; 8+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-06-25 21:09 UTC (permalink / raw)
  To: gcc-bugs

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

Markus Trippelsdorf <trippels at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|FIXED                       |INVALID


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

end of thread, other threads:[~2015-06-25 21:09 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-25 19:23 [Bug c/66673] New: swapping variables via chained xor fails joe.carnuccio at qlogic dot com
2015-06-25 19:29 ` [Bug c/66673] " mpolacek at gcc dot gnu.org
2015-06-25 19:41 ` joe.carnuccio at qlogic dot com
2015-06-25 19:42 ` joe.carnuccio at qlogic dot com
2015-06-25 19:46 ` joe.carnuccio at qlogic dot com
2015-06-25 19:54 ` trippels at gcc dot gnu.org
2015-06-25 19:56 ` mpolacek at gcc dot gnu.org
2015-06-25 21:09 ` [Bug c/66673] Wsequence-point warning missing when swapping variables via chained xor trippels 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).