Bug 22856 - glibc-2.2 code for htonl/etc won't compile with -O outside of a function body
glibc-2.2 code for htonl/etc won't compile with -O outside of a function body
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
i386 Linux
medium Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
David Lawrence
Depends On:
  Show dependency treegraph
Reported: 2000-12-26 17:08 EST by j. alan eldridge
Modified: 2007-04-18 12:30 EDT (History)
1 user (show)

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2004-10-05 18:54:07 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description j. alan eldridge 2000-12-26 17:08:57 EST
ok, this is kind of messy.

first, we have /usr/include/netinet/in.h, which does:

#ifdef __OPTIMIZE__
[ ... ]
#   define ntohl(x)	__bswap_32 (x)
#   define ntohs(x)	__bswap_16 (x)
#   define htonl(x)	__bswap_32 (x)
#   define htons(x)	__bswap_16 (x)
#  endif
[ ... ]

then, in /usr/include/bits/byteswap.h, we get this:

#  define __bswap_32(x) \
     (__extension__							      \
      ({ register unsigned int __v;					      \
	 if (__builtin_constant_p (x))					      \
	   __v = __bswap_constant_32 (x);				      \
	 else								      \
	   __asm__ __volatile__ ("bswap %0"				      \
				 : "=r" (__v)				      \
				 : "0" ((unsigned int) (x)));		      \
	 __v; }))

that's the background.

take a small piece of c++ code that assigns a global scope variable using
htonl() and "houston, we have a problem."

[alane@wozzle /tmp]$ cat t.cc
#include <netinet/in.h>

uint32_t noaddr = htonl(INADDR_NONE);
[alane@wozzle /tmp]$ g++ -c t.cc
[alane@wozzle /tmp]$ g++ -O -c t.cc
t.cc:3: braced-group within expression allowed only inside a function
t.cc:3: ISO C++ forbids declaration of `__v' with no type
t.cc:3: parse error before `}'
[alane@wozzle /tmp]$ 

yes, i know this is *really* obscure. and it could just as well be taken as
a bug in glibc. feel free to recategorize as such if that is warranted.

problem is, it's just *so* weird, and the error message from compiling a
perfectly reasonable piece of code is totally martian. i don't know what
right fix is. i just put parens around the htonl call so it wouldn't macro
expand. fsck. [sigh]
Comment 1 Jakub Jelinek 2000-12-27 06:05:53 EST
I see, but I think also older g++ (e.g. egcs 1.1.2 or gcc 2.95.2) did this and
likewise older glibc (like 2.1.x).
I see possible 4 solutions for this, am just mailing libc-hackers to discuss
which way to go.
Comment 2 Richard Henderson 2004-10-01 17:15:29 EDT
G++ will accept this for gcc 4.0, but no prior versions.  I suspect this
largely due to the re-implementation of statement expressions for
Comment 3 Richard Henderson 2004-10-05 18:54:07 EDT
Pushed upstream as a feature request to

Note You need to log in before you can comment on or make changes to this bug.