Skip to content

Commit cb8ef68

Browse files
committed
Fix unportable definition of BSWAP64() macro.
We have a portable way of writing uint64 constants, but whoever wrote this macro didn't know about it. While at it, fix unsafe under-parenthesization of arguments. That might be moot, because there are already good reasons not to use the macro on anything more complicated than a simple variable, but it's still poor practice. Per buildfarm warnings.
1 parent 16500d2 commit cb8ef68

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/include/port/pg_bswap.h

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Macros for reversing the byte order of 32-bit and 64-bit unsigned integers.
77
* For example, 0xAABBCCDD becomes 0xDDCCBBAA. These are just wrappers for
88
* built-in functions provided by the compiler where support exists.
9+
* Elsewhere, beware of multiple evaluations of the arguments!
910
*
1011
* Note that the GCC built-in functions __builtin_bswap32() and
1112
* __builtin_bswap64() are documented as accepting single arguments of type
@@ -24,23 +25,23 @@
2425
#ifdef HAVE__BUILTIN_BSWAP32
2526
#define BSWAP32(x) __builtin_bswap32(x)
2627
#else
27-
#define BSWAP32(x) (((x << 24) & 0xff000000) | \
28-
((x << 8) & 0x00ff0000) | \
29-
((x >> 8) & 0x0000ff00) | \
30-
((x >> 24) & 0x000000ff))
28+
#define BSWAP32(x) ((((x) << 24) & 0xff000000) | \
29+
(((x) << 8) & 0x00ff0000) | \
30+
(((x) >> 8) & 0x0000ff00) | \
31+
(((x) >> 24) & 0x000000ff))
3132
#endif /* HAVE__BUILTIN_BSWAP32 */
3233

3334
#ifdef HAVE__BUILTIN_BSWAP64
3435
#define BSWAP64(x) __builtin_bswap64(x)
3536
#else
36-
#define BSWAP64(x) (((x << 56) & 0xff00000000000000UL) | \
37-
((x << 40) & 0x00ff000000000000UL) | \
38-
((x << 24) & 0x0000ff0000000000UL) | \
39-
((x << 8) & 0x000000ff00000000UL) | \
40-
((x >> 8) & 0x00000000ff000000UL) | \
41-
((x >> 24) & 0x0000000000ff0000UL) | \
42-
((x >> 40) & 0x000000000000ff00UL) | \
43-
((x >> 56) & 0x00000000000000ffUL))
37+
#define BSWAP64(x) ((((x) << 56) & UINT64CONST(0xff00000000000000)) | \
38+
(((x) << 40) & UINT64CONST(0x00ff000000000000)) | \
39+
(((x) << 24) & UINT64CONST(0x0000ff0000000000)) | \
40+
(((x) << 8) & UINT64CONST(0x000000ff00000000)) | \
41+
(((x) >> 8) & UINT64CONST(0x00000000ff000000)) | \
42+
(((x) >> 24) & UINT64CONST(0x0000000000ff0000)) | \
43+
(((x) >> 40) & UINT64CONST(0x000000000000ff00)) | \
44+
(((x) >> 56) & UINT64CONST(0x00000000000000ff)))
4445
#endif /* HAVE__BUILTIN_BSWAP64 */
4546

4647
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy