Skip to content

Commit 53863eb

Browse files
committed
Make [U]INT64CONST safe for use in #if conditions.
Instead of using a cast to force the constant to be the right width, assume we can plaster on an L, UL, LL, or ULL suffix as appropriate. The old approach to this is very hoary, dating from before we were willing to require compilers to have working int64 types. This fix makes the PG_INT64_MIN, PG_INT64_MAX, and PG_UINT64_MAX constants safe to use in preprocessor conditions, where a cast doesn't work. Other symbolic constants that might be defined using [U]INT64CONST are likewise safer than before. Also fix the SIZE_MAX macro to be similarly safe, if we are forced to provide a definition for that. The test added in commit 2e70d6b happens to do what we want even with the hack "(size_t) -1" definition, but we could easily get burnt on other tests in future. Back-patch to all supported branches, like the previous commits. Discussion: https://postgr.es/m/15883.1504278595@sss.pgh.pa.us
1 parent 9a98557 commit 53863eb

File tree

5 files changed

+10
-59
lines changed

5 files changed

+10
-59
lines changed

configure

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13485,31 +13485,6 @@ cat >>confdefs.h <<_ACEOF
1348513485
_ACEOF
1348613486

1348713487

13488-
13489-
if test x"$HAVE_LONG_LONG_INT_64" = xyes ; then
13490-
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
13491-
/* end confdefs.h. */
13492-
13493-
#define INT64CONST(x) x##LL
13494-
long long int foo = INT64CONST(0x1234567890123456);
13495-
13496-
int
13497-
main ()
13498-
{
13499-
13500-
;
13501-
return 0;
13502-
}
13503-
_ACEOF
13504-
if ac_fn_c_try_compile "$LINENO"; then :
13505-
13506-
$as_echo "#define HAVE_LL_CONSTANTS 1" >>confdefs.h
13507-
13508-
fi
13509-
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
13510-
fi
13511-
13512-
1351313488
# If we found "long int" is 64 bits, assume snprintf handles it. If
1351413489
# we found we need to use "long long int", better check. We cope with
1351513490
# snprintfs that use %lld, %qd, or %I64d as the format. If none of these

configure.in

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,19 +1704,6 @@ fi
17041704
AC_DEFINE_UNQUOTED(PG_INT64_TYPE, $pg_int64_type,
17051705
[Define to the name of a signed 64-bit integer type.])
17061706

1707-
dnl If we need to use "long long int", figure out whether nnnLL notation works.
1708-
1709-
if test x"$HAVE_LONG_LONG_INT_64" = xyes ; then
1710-
AC_TRY_COMPILE([
1711-
#define INT64CONST(x) x##LL
1712-
long long int foo = INT64CONST(0x1234567890123456);
1713-
],
1714-
[],
1715-
[AC_DEFINE(HAVE_LL_CONSTANTS, 1, [Define to 1 if constants of type 'long long int' should have the suffix LL.])],
1716-
[])
1717-
fi
1718-
1719-
17201707
# If we found "long int" is 64 bits, assume snprintf handles it. If
17211708
# we found we need to use "long long int", better check. We cope with
17221709
# snprintfs that use %lld, %qd, or %I64d as the format. If none of these

src/include/c.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ typedef long int int64;
265265
#ifndef HAVE_UINT64
266266
typedef unsigned long int uint64;
267267
#endif
268+
#define INT64CONST(x) (x##L)
269+
#define UINT64CONST(x) (x##UL)
268270
#elif defined(HAVE_LONG_LONG_INT_64)
269271
/* We have working support for "long long int", use that */
270272

@@ -274,23 +276,20 @@ typedef long long int int64;
274276
#ifndef HAVE_UINT64
275277
typedef unsigned long long int uint64;
276278
#endif
279+
#define INT64CONST(x) (x##LL)
280+
#define UINT64CONST(x) (x##ULL)
277281
#else
278282
/* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
279283
#error must have a working 64-bit integer datatype
280284
#endif
281285

282-
/* Decide if we need to decorate 64-bit constants */
283-
#ifdef HAVE_LL_CONSTANTS
284-
#define INT64CONST(x) ((int64) x##LL)
285-
#define UINT64CONST(x) ((uint64) x##ULL)
286-
#else
287-
#define INT64CONST(x) ((int64) x)
288-
#define UINT64CONST(x) ((uint64) x)
289-
#endif
290-
291286
/* Max value of size_t might be missing if we don't have stdint.h */
292287
#ifndef SIZE_MAX
293-
#define SIZE_MAX ((size_t) -1)
288+
#if SIZEOF_SIZE_T == 8
289+
#define SIZE_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
290+
#else
291+
#define SIZE_MAX (0xFFFFFFFFU)
292+
#endif
294293
#endif
295294

296295
/* Select timestamp representation (float8 or int64) */

src/include/pg_config.h.in

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,10 +311,6 @@
311311
/* Define to 1 if you have the `z' library (-lz). */
312312
#undef HAVE_LIBZ
313313

314-
/* Define to 1 if constants of type 'long long int' should have the suffix LL.
315-
*/
316-
#undef HAVE_LL_CONSTANTS
317-
318314
/* Define to 1 if the system has the type `locale_t'. */
319315
#undef HAVE_LOCALE_T
320316

src/include/pg_config.h.win32

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,6 @@
220220
/* Define to 1 if you have the `z' library (-lz). */
221221
/* #undef HAVE_LIBZ */
222222

223-
/* Define to 1 if constants of type 'long long int' should have the suffix LL.
224-
*/
225-
#if (_MSC_VER > 1200)
226-
#define HAVE_LL_CONSTANTS 1
227-
#endif
228-
229223
/* Define to 1 if the system has the type `locale_t'. */
230224
#define HAVE_LOCALE_T 1
231225

@@ -234,7 +228,7 @@
234228

235229
/* Define to 1 if `long long int' works and is 64 bits. */
236230
#if (_MSC_VER > 1200)
237-
#define HAVE_LONG_LONG_INT_64
231+
#define HAVE_LONG_LONG_INT_64 1
238232
#endif
239233

240234
/* Define to 1 if you have the `mbstowcs_l' function. */

0 commit comments

Comments
 (0)