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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2018-04-03
           Assignee|unassigned at gcc dot gnu.org      |jakub at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Created attachment 43824
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=43824&action=edit
gcc8-pr85161.patch

Untested fix.
The problem is in incorrect macros being tested.

The GCC (and clang) predefined macros for endianity are
__BYTE_ORDER__, that needs to be compared against __ORDER_LITTLE_ENDIAN__ or
__ORDER_BIG_ENDIAN__ or __ORDER_PDP_ENDIAN__.
Then there are endian.h, sys/endian.h and/or machine/endian.h headers, which
define depending on chosen feature test macros __BYTE_ORDER or BYTE_ORDER,
which needs to be compared against __{BIG,LITTLE,PDP}_ENDIAN (or without the __
prefix depending on feature test macros).
The code was comparing __BYTE_ORDER (normally defined by endian.h, sometimes
included indirectly from stdlib.h depending on feature test macros) against
__ORDER_BIG_ENDIAN (always undefined, so in preprocessor assumed to be 0).
So, depending on whether endian.h was included or not, it was either never byte
swapping (wrong on non-little endian) or byte swapping always (wrong on little
endian).

Reply via email to