Skip to content

Commit

Permalink
Don't include sys/param.h for _BYTE_ORDER
Browse files Browse the repository at this point in the history
It includes a bunch of other files including a lot of macros which can
reduce the namespace available for actual protobuf.

For example, create a protobuf with a member called SIGSEGV. Since
macros cannot be namespaced in C++ this results in code which cannot be
compiled.

Fix this by just directly including endian.h
  • Loading branch information
giannitedesco authored and acozzette committed Dec 2, 2020
1 parent e8906e4 commit a91adf1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/google/protobuf/io/coded_stream.h
Expand Up @@ -131,7 +131,11 @@
#pragma runtime_checks("c", off)
#endif
#else
#include <sys/param.h> // __BYTE_ORDER
#ifdef __APPLE__
#include <machine/endian.h> // __BYTE_ORDER
#else
#include <endian.h> // __BYTE_ORDER
#endif
#if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
(defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN)) && \
!defined(PROTOBUF_DISABLE_LITTLE_ENDIAN_OPT_FOR_TEST)
Expand Down
7 changes: 4 additions & 3 deletions src/google/protobuf/stubs/port.h
Expand Up @@ -57,9 +57,10 @@
#pragma runtime_checks("c", off)
#endif
#else
#include <sys/param.h> // __BYTE_ORDER
#if defined(__OpenBSD__)
#include <endian.h>
#ifdef __APPLE__
#include <machine/endian.h> // __BYTE_ORDER
#else
#include <endian.h> // __BYTE_ORDER
#endif
#if ((defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)) || \
(defined(__BYTE_ORDER) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
Expand Down

1 comment on commit a91adf1

@1480c1
Copy link

@1480c1 1480c1 commented on a91adf1 Dec 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this commit breaks mingw-w64 gcc and mingw-w64 clang compilation as endian.h is not available but the macros are

uname -a
MINGW64_NT-10.0-19042 ccom-laptop2 3.1.7-340.x86_64 2020-11-08 12:32 UTC x86_64 Msys
clang -dM -E -x c - </dev/null  | grep -i endian
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
#define __LITTLE_ENDIAN__ 1
#define __ORDER_BIG_ENDIAN__ 4321
#define __ORDER_LITTLE_ENDIAN__ 1234
#define __ORDER_PDP_ENDIAN__ 3412

Please sign in to comment.