This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix htonl, htons, ntohl, ntohs functions on big endian systems


The htonl, htons, ntohl and ntohs funtions are defined by POSIX as:

    uint32_t htonl(uint32_t hostlong);
    uint16_t htons(uint16_t hostshort);
    uint32_t ntohl(uint32_t netlong);
    uint16_t ntohs(uint16_t netshort);

They take a uint16_t or uint32_t argument and return a value with the
same type. This means for example that calling htonl on a 64-bit value
should return a 32-bit value.

The GNU libc implements these functions as macros when optimizations are
enabled, and this is explicitely allowed by POSIX. However on big endian
systems there are then defined as:

  # define ntohl(x)      (x)
  # define ntohs(x)      (x)
  # define htonl(x)      (x)
  # define htons(x)      (x)

This means that the values are not casted if the argument is bigger. In
turns that means that:
- the behaviour is different between little and big endian systems;
- the behaviour depends on the optimization level.

This patch attempts to fix that. It adds an implicit cast for ntohl and
htonl using a GCC extension or an explicit one when not using GCC. It
adds an explicit cast for ntohs and htons as the call to __bswap_16
contains an explicit cast.

Changelog:
	* inet/netinet/in.h [__OPTIMIZE__ && (__BYTE_ORDER == __BIG_ENDIAN)]
	(ntohl): Add an implicit uint32_t cast if [__GNUC__ >= 2] or an
	explicit uint32_t cast if [!__GNUC__ >= 2].
	[__OPTIMIZE__ && (__BYTE_ORDER == __BIG_ENDIAN)] (htonl): Likewise.
	[__OPTIMIZE__ && (__BYTE_ORDER == __BIG_ENDIAN)] (ntohs): Add a uint16_t
	cast.
	[__OPTIMIZE__ && (__BYTE_ORDER == __BIG_ENDIAN)] (htons): Likewise.
---
 ChangeLog         | 10 ++++++++++
 inet/netinet/in.h | 19 +++++++++++++++----
 2 files changed, 25 insertions(+), 4 deletions(-)

Note: the macros in <endian.h> also suffer from the same issue. I'll
send a patch for them if this change is considered acceptable.

diff --git a/ChangeLog b/ChangeLog
index f148ac8..ee1b091 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2016-07-31  Aurelien Jarno  <aurelien@aurel32.net>
+
+	* inet/netinet/in.h [__OPTIMIZE__ && (__BYTE_ORDER == __BIG_ENDIAN)]
+	(ntohl): Add an implicit uint32_t cast if [__GNUC__ >= 2] or an
+	explicit uint32_t cast if [!__GNUC__ >= 2].
+	[__OPTIMIZE__ && (__BYTE_ORDER == __BIG_ENDIAN)] (htonl): Likewise.
+	[__OPTIMIZE__ && (__BYTE_ORDER == __BIG_ENDIAN)] (ntohs): Add a uint16_t
+	cast.
+	[__OPTIMIZE__ && (__BYTE_ORDER == __BIG_ENDIAN)] (htons): Likewise.
+
 2016-07-27  H.J. Lu  <hongjiu.lu@intel.com>
 
 	[BZ #20384]
diff --git a/inet/netinet/in.h b/inet/netinet/in.h
index c801593..86e812a 100644
--- a/inet/netinet/in.h
+++ b/inet/netinet/in.h
@@ -393,10 +393,21 @@ extern uint16_t htons (uint16_t __hostshort)
 # if __BYTE_ORDER == __BIG_ENDIAN
 /* The host byte order is the same as network byte order,
    so these functions are all just identity.  */
-# define ntohl(x)	(x)
-# define ntohs(x)	(x)
-# define htonl(x)	(x)
-# define htons(x)	(x)
+#  if defined __GNUC__ && __GNUC__ >= 2
+#   define ntohl(x) \
+    (__extension__							      \
+     ({ uint32_t __x = x;						      \
+      __x; }))
+#   define htonl(x) \
+    (__extension__							      \
+     ({ uint32_t __x = x;						      \
+      __x; }))
+#  else
+#   define ntohl(x)	((uint32_t) (x))
+#   define htonl(x)	((uint32_t) (x))
+#  endif
+#  define ntohs(x)	((uint16_t) (x))
+#  define htons(x)	((uint16_t) (x))
 # else
 #  if __BYTE_ORDER == __LITTLE_ENDIAN
 #   define ntohl(x)	__bswap_32 (x)
-- 
2.8.1


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]