Skip to content

Commit 6a1cefa

Browse files
committed
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.
1 parent bb44015 commit 6a1cefa

File tree

8 files changed

+194
-19
lines changed

8 files changed

+194
-19
lines changed

ChangeLog

+34
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
2017-01-11 Joseph Myers <[email protected]>
2+
3+
[BZ #16458]
4+
* bits/uintn-identity.h: New file.
5+
* inet/netinet/in.h: Include <bits/uintn-identity.h>.
6+
[__BYTE_ORDER == __BIG_ENDIAN] (ntohl): Use __uint32_identity.
7+
[__BYTE_ORDER == __BIG_ENDIAN] (ntohs): Use __uint16_identity.
8+
[__BYTE_ORDER == __BIG_ENDIAN] (htonl): Use __uint32_identity.
9+
[__BYTE_ORDER == __BIG_ENDIAN] (htohs): Use __uint16_identity.
10+
* string/endian.h: Include <bits/uintn-identity.h>.
11+
[__BYTE_ORDER == __LITTLE_ENDIAN] (htole16): Use
12+
__uint16_identity.
13+
[__BYTE_ORDER == __LITTLE_ENDIAN] (le16toh): Likewise.
14+
[__BYTE_ORDER == __LITTLE_ENDIAN] (htole32): Use
15+
__uint32_identity.
16+
[__BYTE_ORDER == __LITTLE_ENDIAN] (le32toh): Likewise.
17+
[__BYTE_ORDER == __LITTLE_ENDIAN] (htole64): Use
18+
__uint64_identity.
19+
[__BYTE_ORDER == __LITTLE_ENDIAN] (le64toh): Likewise.
20+
[__BYTE_ORDER != __LITTLE_ENDIAN] (htobe16): Use
21+
__uint16_identity.
22+
[__BYTE_ORDER != __LITTLE_ENDIAN] (be16toh): Likewise.
23+
[__BYTE_ORDER != __LITTLE_ENDIAN] (htobe32): Use
24+
__uint32_identity.
25+
[__BYTE_ORDER != __LITTLE_ENDIAN] (be32toh): Likewise.
26+
[__BYTE_ORDER != __LITTLE_ENDIAN] (htobe64): Use
27+
__uint64_identity.
28+
[__BYTE_ORDER != __LITTLE_ENDIAN] (be64toh): Likewise.
29+
* string/Makefile (headers): Add bits/uintn-identity.h.
30+
(tests): Add test-endian-types.
31+
* string/test-endian-types.c: New file.
32+
* inet/Makefile (tests): Add test-hnto-types.
33+
* inet/test-hnto-types.c: New file.
34+
135
2016-01-11 Siddhesh Poyarekar <[email protected]>
236

337
* po/be.po: Update from Translation Project.

bits/uintn-identity.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* Inline functions to return unsigned integer values unchanged.
2+
Copyright (C) 2017 Free Software Foundation, Inc.
3+
This file is part of the GNU C Library.
4+
5+
The GNU C Library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
The GNU C Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with the GNU C Library; if not, see
17+
<http://www.gnu.org/licenses/>. */
18+
19+
#if !defined _NETINET_IN_H && !defined _ENDIAN_H
20+
# error "Never use <bits/uintn-identity.h> directly; include <netinet/in.h> or <endian.h> instead."
21+
#endif
22+
23+
#ifndef _BITS_UINTN_IDENTITY_H
24+
#define _BITS_UINTN_IDENTITY_H 1
25+
26+
#include <bits/types.h>
27+
28+
/* These inline functions are to ensure the appropriate type
29+
conversions and associated diagnostics from macros that convert to
30+
a given endianness. */
31+
32+
static __inline __uint16_t
33+
__uint16_identity (__uint16_t __x)
34+
{
35+
return __x;
36+
}
37+
38+
static __inline __uint32_t
39+
__uint32_identity (__uint32_t __x)
40+
{
41+
return __x;
42+
}
43+
44+
static __inline __uint64_t
45+
__uint64_identity (__uint64_t __x)
46+
{
47+
return __x;
48+
}
49+
50+
#endif /* _BITS_UINTN_IDENTITY_H. */

inet/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ aux := check_pf check_native ifreq
5252
tests := htontest test_ifindex tst-ntoa tst-ether_aton tst-network \
5353
tst-gethnm test-ifaddrs bug-if1 test-inet6_opt tst-ether_line \
5454
tst-getni1 tst-getni2 tst-inet6_rth tst-checks tst-checks-posix \
55-
tst-sockaddr tst-inet6_scopeid_pton
55+
tst-sockaddr tst-inet6_scopeid_pton test-hnto-types
5656

5757
include ../Rules
5858

inet/netinet/in.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ extern uint16_t htons (uint16_t __hostshort)
383383

384384
/* Get machine dependent optimized versions of byte swapping functions. */
385385
#include <bits/byteswap.h>
386+
#include <bits/uintn-identity.h>
386387

387388
#ifdef __OPTIMIZE__
388389
/* We can optimize calls to the conversion functions. Either nothing has
@@ -391,10 +392,10 @@ extern uint16_t htons (uint16_t __hostshort)
391392
# if __BYTE_ORDER == __BIG_ENDIAN
392393
/* The host byte order is the same as network byte order,
393394
so these functions are all just identity. */
394-
# define ntohl(x) (x)
395-
# define ntohs(x) (x)
396-
# define htonl(x) (x)
397-
# define htons(x) (x)
395+
# define ntohl(x) __uint32_identity (x)
396+
# define ntohs(x) __uint16_identity (x)
397+
# define htonl(x) __uint32_identity (x)
398+
# define htons(x) __uint16_identity (x)
398399
# else
399400
# if __BYTE_ORDER == __LITTLE_ENDIAN
400401
# define ntohl(x) __bswap_32 (x)

inet/test-hnto-types.c

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Test netinet/in.h endian-conversion macros always return the correct type.
2+
Copyright (C) 2017 Free Software Foundation, Inc.
3+
This file is part of the GNU C Library.
4+
5+
The GNU C Library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
The GNU C Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with the GNU C Library; if not, see
17+
<http://www.gnu.org/licenses/>. */
18+
19+
#include <netinet/in.h>
20+
#include <stdint.h>
21+
22+
int i;
23+
uint16_t u16;
24+
uint32_t u32;
25+
26+
int
27+
do_test (void)
28+
{
29+
/* This is a compilation test. */
30+
extern __typeof (htons (i)) u16;
31+
extern __typeof (ntohs (i)) u16;
32+
extern __typeof (htonl (i)) u32;
33+
extern __typeof (ntohl (i)) u32;
34+
(void) u16;
35+
(void) u32;
36+
return 0;
37+
}
38+
39+
#include <support/test-driver.c>

string/Makefile

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ include ../Makeconfig
2525
headers := string.h strings.h memory.h endian.h bits/endian.h \
2626
argz.h envz.h byteswap.h bits/byteswap.h bits/byteswap-16.h \
2727
bits/string.h bits/string2.h bits/string3.h \
28-
bits/strings_fortified.h
28+
bits/strings_fortified.h bits/uintn-identity.h
2929

3030
routines := strcat strchr strcmp strcoll strcpy strcspn \
3131
strverscmp strdup strndup \
@@ -56,7 +56,8 @@ tests := tester inl-tester noinl-tester testcopy test-ffs \
5656
tst-strtok tst-strxfrm bug-strcoll1 tst-strfry \
5757
bug-strtok1 $(addprefix test-,$(strop-tests)) \
5858
bug-envz1 tst-strxfrm2 tst-endian tst-svc2 \
59-
tst-strtok_r bug-strcoll2 tst-cmp tst-xbzero-opt
59+
tst-strtok_r bug-strcoll2 tst-cmp tst-xbzero-opt \
60+
test-endian-types
6061

6162
xtests = tst-strcoll-overflow
6263

string/endian.h

+13-12
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,38 @@
5858
#if defined __USE_MISC && !defined __ASSEMBLER__
5959
/* Conversion interfaces. */
6060
# include <bits/byteswap.h>
61+
# include <bits/uintn-identity.h>
6162

6263
# if __BYTE_ORDER == __LITTLE_ENDIAN
6364
# define htobe16(x) __bswap_16 (x)
64-
# define htole16(x) (x)
65+
# define htole16(x) __uint16_identity (x)
6566
# define be16toh(x) __bswap_16 (x)
66-
# define le16toh(x) (x)
67+
# define le16toh(x) __uint16_identity (x)
6768

6869
# define htobe32(x) __bswap_32 (x)
69-
# define htole32(x) (x)
70+
# define htole32(x) __uint32_identity (x)
7071
# define be32toh(x) __bswap_32 (x)
71-
# define le32toh(x) (x)
72+
# define le32toh(x) __uint32_identity (x)
7273

7374
# define htobe64(x) __bswap_64 (x)
74-
# define htole64(x) (x)
75+
# define htole64(x) __uint64_identity (x)
7576
# define be64toh(x) __bswap_64 (x)
76-
# define le64toh(x) (x)
77+
# define le64toh(x) __uint64_identity (x)
7778

7879
# else
79-
# define htobe16(x) (x)
80+
# define htobe16(x) __uint16_identity (x)
8081
# define htole16(x) __bswap_16 (x)
81-
# define be16toh(x) (x)
82+
# define be16toh(x) __uint16_identity (x)
8283
# define le16toh(x) __bswap_16 (x)
8384

84-
# define htobe32(x) (x)
85+
# define htobe32(x) __uint32_identity (x)
8586
# define htole32(x) __bswap_32 (x)
86-
# define be32toh(x) (x)
87+
# define be32toh(x) __uint32_identity (x)
8788
# define le32toh(x) __bswap_32 (x)
8889

89-
# define htobe64(x) (x)
90+
# define htobe64(x) __uint64_identity (x)
9091
# define htole64(x) __bswap_64 (x)
91-
# define be64toh(x) (x)
92+
# define be64toh(x) __uint64_identity (x)
9293
# define le64toh(x) __bswap_64 (x)
9394
# endif
9495
#endif

string/test-endian-types.c

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Test endian.h endian-conversion macros always return the correct type.
2+
Copyright (C) 2017 Free Software Foundation, Inc.
3+
This file is part of the GNU C Library.
4+
5+
The GNU C Library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
The GNU C Library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with the GNU C Library; if not, see
17+
<http://www.gnu.org/licenses/>. */
18+
19+
#include <endian.h>
20+
#include <stdint.h>
21+
22+
int i;
23+
uint16_t u16;
24+
uint32_t u32;
25+
uint64_t u64;
26+
27+
int
28+
do_test (void)
29+
{
30+
/* This is a compilation test. */
31+
extern __typeof (htobe16 (i)) u16;
32+
extern __typeof (htole16 (i)) u16;
33+
extern __typeof (be16toh (i)) u16;
34+
extern __typeof (le16toh (i)) u16;
35+
extern __typeof (htobe32 (i)) u32;
36+
extern __typeof (htole32 (i)) u32;
37+
extern __typeof (be32toh (i)) u32;
38+
extern __typeof (le32toh (i)) u32;
39+
extern __typeof (htobe64 (i)) u64;
40+
extern __typeof (htole64 (i)) u64;
41+
extern __typeof (be64toh (i)) u64;
42+
extern __typeof (le64toh (i)) u64;
43+
(void) u16;
44+
(void) u32;
45+
(void) u64;
46+
return 0;
47+
}
48+
49+
#include <support/test-driver.c>

0 commit comments

Comments
 (0)