8000 Make [U]INT64CONST safe for use in #if conditions. · abhinav-upadhyay/postgres@1305186 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 1305186

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 bf38702 commit 1305186

File tree

5 files changed

+11
-60
lines changed

5 files changed

+11
-60
lines changed

configure

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

1341413414

13415-
13416-
if test x"$HAVE_LONG_LONG_INT_64" = xyes ; then
13417-
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
13418-
/* end confdefs.h. */
13419-
13420-
#define INT64CONST(x) x##LL
13421-
long long int foo = INT64CONST(0x1234567890123456);
13422-
13423-
int
13424-
main ()
13425-
{
13426-
13427-
;
13428-
return 0;
13429-
}
13430-
_ACEOF
13431-
if ac_fn_c_try_compile "$LINENO"; then :
13432-
13433-
$as_echo "#define HAVE_LL_CONSTANTS 1" >>confdefs.h
13434-
13435-
fi
13436-
rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
13437-
fi
13438-
13439-
1344013415
# If we found "long int" is 64 bits, assume snprintf handles it. If
1344113416
# we found we need to use "long long int", better check. We cope with
1344213417
# 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
@@ -1708,19 +1708,6 @@ fi
17081708
AC_DEFINE_UNQUOTED(PG_INT64_TYPE, $pg_int64_type,
17091709
[Define to the name of a signed 64-bit integer type.])
17101710

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

src/include/c.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ typedef long int int64;
261261
#ifndef HAVE_UINT64
262262
typedef unsigned long int uint64;
263263
#endif
264+
#define INT64CONST(x) (x##L)
265+
#define UINT64CONST(x) (x##UL)
264266
#elif defined(HAVE_LONG_LONG_INT_64)
265267
/* We have working support for "long long int", use that */
266268

@@ -270,20 +272,13 @@ typedef long long int int64;
270272
#ifndef HAVE_UINT64
271273
typedef unsigned long long int uint64;
272274
#endif
275+
#define INT64CONST(x) (x##LL)
276+
#define UINT64CONST(x) (x##ULL)
273277
#else
274278
/* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */
275279
#error must have a working 64-bit integer datatype
276280
#endif
277281

278-
/* Decide if we need to decorate 64-bit constants */
279-
#ifdef HAVE_LL_CONSTANTS
280-
#define INT64CONST(x) ((int64) x##LL)
281-
#define UINT64CONST(x) ((uint64) x##ULL)
282-
#else
283-
#define INT64CONST(x) ((int64) x)
284-
#define UINT64CONST(x) ((uint64) x)
285-
#endif
286-
287282
/* snprintf format strings to use for 64-bit integers */
288283
#define INT64_FORMAT "%" INT64_MODIFIER "d"
289284
#define UINT64_FORMAT "%" INT64_MODIFIER "u"
@@ -311,14 +306,18 @@ typedef unsigned PG_INT128_TYPE uint128;
311306
#define PG_UINT16_MAX (0xFFFF)
312307
#define PG_INT32_MIN (-0x7FFFFFFF-1)
313308
#define PG_INT32_MAX (0x7FFFFFFF)
314-
#define PG_UINT32_MAX (0xFFFFFFFF)
309+
#define PG_UINT32_MAX (0xFFFFFFFFU)
315310
#define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
316311
#define PG_INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
317312
#define PG_UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
318313

319314
/* Max value of size_t might also be missing if we don't have stdint.h */
320315
#ifndef SIZE_MAX
321-
#define SIZE_MAX ((size_t) -1)
316+
#if SIZEOF_SIZE_T == 8
317+
#define SIZE_MAX PG_UINT64_MAX
318+
#else
319+
#define SIZE_MAX PG_UINT32_MAX
320+
#endif
322321
#endif
323322

324323
/* 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
@@ -333,10 +333,6 @@
333333
/* Define to 1 if you have the `z' library (-lz). */
334334
#undef HAVE_LIBZ
335335

336-
/* Define to 1 if constants of type 'long long int' should have the suffix LL.
337-
*/
338-
#undef HAVE_LL_CONSTANTS
339-
340336
/* Define to 1 if the system has the type `locale_t'. */
341337
#undef HAVE_LOCALE_T
342338

src/include/pg_config.h.win32

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

220-
/* Define to 1 if constants of type 'long long int' should have the suffix LL.
221-
*/
222-
#if (_MSC_VER > 1200)
223-
#define HAVE_LL_CONSTANTS 1
224-
#endif
225-
226220
/* Define to 1 if the system has the type `locale_t'. */
227221
#define HAVE_LOCALE_T 1
228222

@@ -231,7 +225,7 @@
231225

232226
/* Define to 1 if `long long int' works and is 64 bits. */
233227
#if (_MSC_VER > 1200)
234-
#define HAVE_LONG_LONG_INT_64
228+
#define HAVE_LONG_LONG_INT_64 1
235229
#endif
236230

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

0 commit comments

Comments
 (0)
0