Skip to content

Commit f60a236

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 0bfcda9 commit f60a236

File tree

5 files changed

+10
-88
lines changed

5 files changed

+10
-88
lines changed

configure

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24786,60 +24786,6 @@ $as_echo "$as_me: error: Cannot find a working 64-bit integer type." >&2;}
2478624786
fi
2478724787

2478824788

24789-
24790-
if test x"$HAVE_LONG_LONG_INT_64" = xyes ; then
24791-
cat >conftest.$ac_ext <<_ACEOF
24792-
/* confdefs.h. */
24793-
_ACEOF
24794-
cat confdefs.h >>conftest.$ac_ext
24795-
cat >>conftest.$ac_ext <<_ACEOF
24796-
/* end confdefs.h. */
24797-
24798-
#define INT64CONST(x) x##LL
24799-
long long int foo = INT64CONST(0x1234567890123456);
24800-
24801-
int
24802-
main ()
24803-
{
24804-
24805-
;
24806-
return 0;
24807-
}
24808-
_ACEOF
24809-
rm -f conftest.$ac_objext
24810-
if { (ac_try="$ac_compile"
24811-
case "(($ac_try" in
24812-
*\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
24813-
*) ac_try_echo=$ac_try;;
24814-
esac
24815-
eval ac_try_echo="\"\$as_me:$LINENO: $ac_try_echo\""
24816-
$as_echo "$ac_try_echo") >&5
24817-
(eval "$ac_compile") 2>conftest.er1
24818-
ac_status=$?
24819-
grep -v '^ *+' conftest.er1 >conftest.err
24820-
rm -f conftest.er1
24821-
cat conftest.err >&5
24822-
$as_echo "$as_me:$LINENO: \$? = $ac_status" >&5
24823-
(exit $ac_status); } && {
24824-
test -z "$ac_c_werror_flag" ||
24825-
test ! -s conftest.err
24826-
} && test -s conftest.$ac_objext; then
24827-
24828-
cat >>confdefs.h <<\_ACEOF
24829-
#define HAVE_LL_CONSTANTS 1
24830-
_ACEOF
24831-
24832-
else
24833-
$as_echo "$as_me: failed program was:" >&5
24834-
sed 's/^/| /' conftest.$ac_ext >&5
24835-
24836-
24837-
fi
24838-
24839-
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
24840-
fi
24841-
24842-
2484324789
# If we found "long int" is 64 bits, assume snprintf handles it. If
2484424790
# we found we need to use "long long int", better check. We cope with
2484524791
# 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
@@ -1636,19 +1636,6 @@ if test x"$HAVE_LONG_INT_64" = x"no" ; then
16361636
fi
16371637

16381638

1639-
dnl If we need to use "long long int", figure out whether nnnLL notation works.
1640-
1641-
if test x"$HAVE_LONG_LONG_INT_64" = xyes ; then
1642-
AC_TRY_COMPILE([
1643-
#define INT64CONST(x) x##LL
1644-
long long int foo = INT64CONST(0x1234567890123456);
1645-
],
1646-
[],
1647-
[AC_DEFINE(HAVE_LL_CONSTANTS, 1, [Define to 1 if constants of type 'long long int' should have the suffix LL.])],
1648-
[])
1649-
fi
1650-
1651-
16521639
# If we found "long int" is 64 bits, assume snprintf handles it. If
16531640
# we found we need to use "long long int", better check. We cope with
16541641
# 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
@@ -267,6 +267,8 @@ typedef long int int64;
267267
#ifndef HAVE_UINT64
268268
typedef unsigned long int uint64;
269269
#endif
270+
#define INT64CONST(x) (x##L)
271+
#define UINT64CONST(x) (x##UL)
270272
#elif defined(HAVE_LONG_LONG_INT_64)
271273
/* We have working support for "long long int", use that */
272274

@@ -276,23 +278,20 @@ typedef long long int int64;
276278
#ifndef HAVE_UINT64
277279
typedef unsigned long long int uint64;
278280
#endif
281+
#define INT64CONST(x) (x##LL)
282+
#define UINT64CONST(x) (x##ULL)
279283
#else
280284
/* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
281285
#error must have a working 64-bit integer datatype
282286
#endif
283287

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

298297
/* 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
@@ -326,10 +326,6 @@
326326
/* Define to 1 if you have the `z' library (-lz). */
327327
#undef HAVE_LIBZ
328328

329-
/* Define to 1 if constants of type 'long long int' should have the suffix LL.
330-
*/
331-
#undef HAVE_LL_CONSTANTS
332-
333329
/* Define to 1 if the system has the type `locale_t'. */
334330
#undef HAVE_LOCALE_T
335331

src/include/pg_config.h.win32

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

235-
/* Define to 1 if constants of type 'long long int' should have the suffix LL.
236-
*/
237-
#if (_MSC_VER > 1200)
238-
#define HAVE_LL_CONSTANTS 1
239-
#endif
240-
241235
/* Define to 1 if the system has the type `locale_t'. */
242236
#define HAVE_LOCALE_T 1
243237

@@ -246,7 +240,7 @@
246240

247241
/* Define to 1 if `long long int' works and is 64 bits. */
248242
#if (_MSC_VER > 1200)
249-
#define HAVE_LONG_LONG_INT_64
243+
#define HAVE_LONG_LONG_INT_64 1
250244
#endif
251245

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

0 commit comments

Comments
 (0)