From e50d401a83968436c5d794a77b2cc8fa0a31906d Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Fri, 1 Sep 2017 13:52:53 -0400
Subject: [PATCH] Ensure SIZE_MAX can be used throughout our code.

Pre-C99 platforms may lack <stdint.h> and thereby SIZE_MAX.  We have
a couple of places using the hack "(size_t) -1" as a fallback, but
it wasn't universally available; which means the code added in commit
2e70d6b5e fails to compile everywhere.  Move that hack to c.h so that
we can rely on having SIZE_MAX everywhere.

Per discussion, it'd be a good idea to make the macro's value safe
for use in #if-tests, but that will take a bit more work.  This is
just a quick expedient to get the buildfarm green again.

Back-patch to all supported branches, like the previous commit.

Discussion: https://postgr.es/m/15883.1504278595@sss.pgh.pa.us
---
 src/include/c.h              | 5 +++++
 src/include/utils/memutils.h | 2 +-
 src/timezone/private.h       | 4 ----
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/src/include/c.h b/src/include/c.h
index 726f0f3f88d..f1f8b11877a 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -340,6 +340,11 @@ typedef unsigned PG_INT128_TYPE uint128;
 #define PG_INT64_MAX	INT64CONST(0x7FFFFFFFFFFFFFFF)
 #define PG_UINT64_MAX	UINT64CONST(0xFFFFFFFFFFFFFFFF)
 
+/* Max value of size_t might also be missing if we don't have stdint.h */
+#ifndef SIZE_MAX
+#define SIZE_MAX ((size_t) -1)
+#endif
+
 /* Select timestamp representation (float8 or int64) */
 #ifdef USE_INTEGER_DATETIMES
 #define HAVE_INT64_TIMESTAMP
diff --git a/src/include/utils/memutils.h b/src/include/utils/memutils.h
index e6334a20385..5125833b6a9 100644
--- a/src/include/utils/memutils.h
+++ b/src/include/utils/memutils.h
@@ -41,7 +41,7 @@
 
 #define AllocSizeIsValid(size)	((Size) (size) <= MaxAllocSize)
 
-#define MaxAllocHugeSize	((Size) -1 >> 1)	/* SIZE_MAX / 2 */
+#define MaxAllocHugeSize	(SIZE_MAX / 2)
 
 #define AllocHugeSizeIsValid(size)	((Size) (size) <= MaxAllocHugeSize)
 
diff --git a/src/timezone/private.h b/src/timezone/private.h
index f031b17b7ef..f78053660e8 100644
--- a/src/timezone/private.h
+++ b/src/timezone/private.h
@@ -48,10 +48,6 @@
 /* Unlike <ctype.h>'s isdigit, this also works if c < 0 | c > UCHAR_MAX. */
 #define is_digit(c) ((unsigned)(c) - '0' <= 9)
 
-#ifndef SIZE_MAX
-#define SIZE_MAX ((size_t) -1)
-#endif
-
 /*
  * SunOS 4.1.1 libraries lack remove.
  */
-- 
GitLab