glibc/bits
Joseph Myers 6a1cefac19 Make endian-conversion macros always return correct types (bug 16458).
Bug 16458 reports that the endian-conversion macros in <endian.h> and
<netinet/in.h>, in the case where no endian conversion is needed, just
return their arguments without converting to the expected return type,
so failing to act as expected for a macro version of a function.  (The
<netinet/in.h> macros, in particular, are described with prototypes in
POSIX so should act like correspondingly prototyped functions.)

Where previously this was a fairly obscure issue, it now results in
glibc build with GCC mainline breaking for big-endian systems:

nss_hesiod/hesiod-service.c: In function '_nss_hesiod_getservbyport_r':
nss_hesiod/hesiod-service.c:142:39: error: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 6 [-Werror=format-truncation=]
   snprintf (portstr, sizeof portstr, "%d", ntohs (port));
                                       ^~
nss_hesiod/hesiod-service.c:142:38: note: using the range [1, -2147483648] for directive argument
   snprintf (portstr, sizeof portstr, "%d", ntohs (port));
                                      ^~~~
nss_hesiod/hesiod-service.c:142:3: note: format output between 2 and 12 bytes into a destination of size 6
   snprintf (portstr, sizeof portstr, "%d", ntohs (port));
   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The port argument is passed as int to this function, so when ntohs
does not convert the compiler cannot tell that the result is within
the range of uint16_t.  (I don't know if in fact it's possible for
out-of-range values to reach this function and so get truncated as
strings without this patch or as integers with it.)

This patch arranges for these macros to use identity functions to
ensure appropriate conversions while having warnings for implicit
conversions of function arguments that might not occur with a cast.

Tested for x86_64 and x86; with build-many-glibcs.py with GCC 6; and
with build-many-glibcs.py with GCC mainline for powerpc to test the
build fix.

	[BZ #16458]
	* bits/uintn-identity.h: New file.
	* inet/netinet/in.h: Include <bits/uintn-identity.h>.
	[__BYTE_ORDER == __BIG_ENDIAN] (ntohl): Use __uint32_identity.
	[__BYTE_ORDER == __BIG_ENDIAN] (ntohs): Use __uint16_identity.
	[__BYTE_ORDER == __BIG_ENDIAN] (htonl): Use __uint32_identity.
	[__BYTE_ORDER == __BIG_ENDIAN] (htohs): Use __uint16_identity.
	* string/endian.h: Include <bits/uintn-identity.h>.
	[__BYTE_ORDER == __LITTLE_ENDIAN] (htole16): Use
	__uint16_identity.
	[__BYTE_ORDER == __LITTLE_ENDIAN] (le16toh): Likewise.
	[__BYTE_ORDER == __LITTLE_ENDIAN] (htole32): Use
	__uint32_identity.
	[__BYTE_ORDER == __LITTLE_ENDIAN] (le32toh): Likewise.
	[__BYTE_ORDER == __LITTLE_ENDIAN] (htole64): Use
	__uint64_identity.
	[__BYTE_ORDER == __LITTLE_ENDIAN] (le64toh): Likewise.
	[__BYTE_ORDER != __LITTLE_ENDIAN] (htobe16): Use
	__uint16_identity.
	[__BYTE_ORDER != __LITTLE_ENDIAN] (be16toh): Likewise.
	[__BYTE_ORDER != __LITTLE_ENDIAN] (htobe32): Use
	__uint32_identity.
	[__BYTE_ORDER != __LITTLE_ENDIAN] (be32toh): Likewise.
	[__BYTE_ORDER != __LITTLE_ENDIAN] (htobe64): Use
	__uint64_identity.
	[__BYTE_ORDER != __LITTLE_ENDIAN] (be64toh): Likewise.
	* string/Makefile (headers): Add bits/uintn-identity.h.
	(tests): Add test-endian-types.
	* string/test-endian-types.c: New file.
	* inet/Makefile (tests): Add test-hnto-types.
	* inet/test-hnto-types.c: New file.
2017-01-11 15:28:08 +00:00
..
byteswap-16.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
byteswap.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
confname.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
dirent.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
dlfcn.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
elfclass.h Update. 1999-07-27 05:53:25 +00:00
endian.h Update. 1997-11-26 04:23:08 +00:00
environments.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
errno.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
fcntl.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
fenv.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
fenvinline.h Update. 1999-11-11 01:05:26 +00:00
flt-eval-method.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
fp-fast.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
fp-logb.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
huge_val.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
huge_valf.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
huge_vall.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
hwcap.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
in.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
inf.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
ioctl-types.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
ioctls.h Update. 1997-11-26 04:23:08 +00:00
ipc.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
ipctypes.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
iscanonical.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
libc-header-start.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
libm-simd-decl-stubs.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
link.h * csu/elf-init.c (__libc_csu_fini): Don't do anything here. 2005-01-06 22:40:27 +00:00
local_lim.h Update. 1997-10-15 05:34:02 +00:00
long-double.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
math-vector.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
mathdef.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
mathinline.h * configure.in (libc_cv_gnu89_inline): Test for -fgnu89-inline. 2007-03-17 17:04:28 +00:00
mman-linux.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
mman.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
mqueue.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
msq.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
nan.h Update. 1997-10-15 05:34:02 +00:00
netdb.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
param.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
poll.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
posix_opt.h Update. 1997-10-15 05:34:02 +00:00
pthreadtypes.h 2002-11-10 Roland McGrath <roland@redhat.com> 2002-11-10 23:22:35 +00:00
resource.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sched.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
select.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sem.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
setjmp.h Update. 1997-11-26 04:23:08 +00:00
shm.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sigaction.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sigcontext.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
siginfo.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
signum.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sigset.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sigstack.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sigthread.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sockaddr.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
socket.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
stat.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
statfs.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
statvfs.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
stdlib-bsearch.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
string.h Add _STRING_INLINE_unaligned and string_private.h 2016-02-18 14:55:29 -02:00
stropts.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sys_errlist.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
syslog-path.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
sysmacros.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
termios.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
time.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
types.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
typesizes.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
uintn-identity.h Make endian-conversion macros always return correct types (bug 16458). 2017-01-11 15:28:08 +00:00
uio.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
ustat.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
utmp.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
utsname.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
waitflags.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
waitstatus.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
wchar.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00
wordsize.h Define wordsize.h macros everywhere 2016-11-04 09:37:44 -07:00
xtitypes.h Update copyright dates with scripts/update-copyrights. 2017-01-01 00:14:16 +00:00