[Bug c/66673] warning missing for undefined behavior when swapping variables via chained xor

manu at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Thu Jun 25 20:19:00 GMT 2015


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

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
   Last reconfirmed|                            |2015-06-25
                 CC|                            |manu at gcc dot gnu.org
         Resolution|INVALID                     |---
            Summary|swapping variables via      |warning missing for
                   |chained xor fails           |undefined behavior when
                   |                            |swapping variables via
                   |                            |chained xor
     Ever confirmed|0                           |1

--- Comment #7 from Manuel López-Ibáñez <manu at gcc dot gnu.org> ---
(In reply to Marek Polacek from comment #6)
> (In reply to joe.carnuccio from comment #2)
> > -Wall produces no warnings...
> 
> Oh, you're using too old GCC.  Please try a newer version.

To be fair, GCC 5.1 does not generate a warning either with -Wall -Wextra.
Neither does clang 3.7.

For this testcase:

void swap(int *a, int *b) {
    *a ^= *b ^= *a ^= *b;
}

int main() {
    int a = 5;
    int b = 8;
    printf("%d, %d\n", a, b);
    a ^= b ^= a ^= b;
    __builtin_printf("%d, %d\n", a, b);
    swap(&a, &b);
    __builtin_printf("%d, %d\n", a, b);
}

Clang warns:
20 : warning: unsequenced modification and access to 'a' [-Wunsequenced]

a ^= b ^= a ^= b;

but gcc is silent.


More information about the Gcc-bugs mailing list