Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 83ff161

Browse files
committed
Centralize definition of integer limits.
Several submitted and even committed patches have run into the problem that C89, our baseline, does not provide minimum/maximum values for various integer datatypes. C99's stdint.h does, but we can't rely on it. Several parts of the code defined limits locally, so instead centralize the definitions to c.h. This patch also changes the more obvious usages of literal limit values; there's more places that could be changed, but it's less clear whether it's beneficial to change those. Author: Andrew Gierth Discussion: 87619tc5wc.fsf@news-spur.riddles.org.uk
1 parent bdc3d7f commit 83ff161

File tree

18 files changed

+68
-36
lines changed

18 files changed

+68
-36
lines changed

contrib/btree_gist/btree_ts.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ ts_dist(PG_FUNCTION_ARGS)
153153
p->day = INT_MAX;
154154
p->month = INT_MAX;
155155
#ifdef HAVE_INT64_TIMESTAMP
156-
p->time = INT64CONST(0x7FFFFFFFFFFFFFFF);
156+
p->time = INT64_MAX;
157157
#else
158158
p->time = DBL_MAX;
159159
#endif
@@ -181,7 +181,7 @@ tstz_dist(PG_FUNCTION_ARGS)
181181
p->day = INT_MAX;
182182
p->month = INT_MAX;
183183
#ifdef HAVE_INT64_TIMESTAMP
184-
p->time = INT64CONST(0x7FFFFFFFFFFFFFFF);
184+
p->time = INT64_MAX;
185185
#else
186186
p->time = DBL_MAX;
187187
#endif

contrib/intarray/_int_gist.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
*/
44
#include "postgres.h"
55

6+
#include <limits.h>
7+
68
#include "access/gist.h"
79
#include "access/skey.h"
810

@@ -191,7 +193,7 @@ g_int_compress(PG_FUNCTION_ARGS)
191193
cand = 1;
192194
while (len > MAXNUMRANGE * 2)
193195
{
194-
min = 0x7fffffff;
196+
min = INT_MAX;
195197
for (i = 2; i < len; i += 2)
196198
if (min > (dr[i] - dr[i - 1]))
197199
{

contrib/pgbench/pgbench.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@
4949
#include <sys/resource.h> /* for getrlimit */
5050
#endif
5151

52-
#ifndef INT64_MAX
53-
#define INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
54-
#endif
55-
5652
#ifndef M_PI
5753
#define M_PI 3.14159265358979323846
5854
#endif
@@ -453,7 +449,7 @@ strtoint64(const char *str)
453449
*/
454450
if (strncmp(ptr, "9223372036854775808", 19) == 0)
455451
{
456-
result = -INT64CONST(0x7fffffffffffffff) - 1;
452+
result = INT64_MIN;
457453
ptr += 19;
458454
goto gotdigits;
459455
}

src/backend/access/transam/xlog.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ WALInsertLockAcquireExclusive(void)
14081408
{
14091409
LWLockAcquireWithVar(&WALInsertLocks[i].l.lock,
14101410
&WALInsertLocks[i].l.insertingAt,
1411-
UINT64CONST(0xFFFFFFFFFFFFFFFF));
1411+
UINT64_MAX);
14121412
}
14131413
LWLockAcquireWithVar(&WALInsertLocks[i].l.lock,
14141414
&WALInsertLocks[i].l.insertingAt,

src/backend/tsearch/wparser_def.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "postgres.h"
1616

17+
#include <limits.h>
18+
1719
#include "catalog/pg_collation.h"
1820
#include "commands/defrem.h"
1921
#include "tsearch/ts_locale.h"
@@ -2047,7 +2049,7 @@ hlCover(HeadlineParsedText *prs, TSQuery query, int *p, int *q)
20472049
int pos = *p;
20482050

20492051
*q = -1;
2050-
*p = 0x7fffffff;
2052+
*p = INT_MAX;
20512053

20522054
for (j = 0; j < query->size; j++)
20532055
{
@@ -2258,7 +2260,7 @@ mark_hl_fragments(HeadlineParsedText *prs, TSQuery query, int highlight,
22582260
for (f = 0; f < max_fragments; f++)
22592261
{
22602262
maxitems = 0;
2261-
minwords = 0x7fffffff;
2263+
minwords = INT32_MAX;
22622264
minI = -1;
22632265

22642266
/*

src/backend/utils/adt/int8.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ scanint8(const char *str, bool errorOK, int64 *result)
7878
*/
7979
if (strncmp(ptr, "9223372036854775808", 19) == 0)
8080
{
81-
tmp = -INT64CONST(0x7fffffffffffffff) - 1;
81+
tmp = INT64_MIN;
8282
ptr += 19;
8383
goto gotdigits;
8484
}

src/backend/utils/adt/numutils.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pg_lltoa(int64 value, char *a)
190190
* Avoid problems with the most negative integer not being representable
191191
* as a positive integer.
192192
*/
193-
if (value == (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1))
193+
if (value == INT64_MIN)
194194
{
195195
memcpy(a, "-9223372036854775808", 21);
196196
return;

src/backend/utils/adt/timestamp.c

-8
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@
4444

4545
#define SAMESIGN(a,b) (((a) < 0) == ((b) < 0))
4646

47-
#ifndef INT64_MAX
48-
#define INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
49-
#endif
50-
51-
#ifndef INT64_MIN
52-
#define INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
53-
#endif
54-
5547
/* Set at postmaster start */
5648
TimestampTz PgStartTime;
5749

src/backend/utils/adt/tsrank.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
#include "postgres.h"
1515

16+
#include <limits.h>
1617
#include <math.h>
1718

1819
#include "tsearch/ts_utils.h"
@@ -555,7 +556,7 @@ Cover(DocRepresentation *doc, int len, QueryRepresentation *qr, CoverExt *ext)
555556

556557
memset(qr->operandexist, 0, sizeof(bool) * qr->query->size);
557558

558-
ext->p = 0x7fffffff;
559+
ext->p = INT_MAX;
559560
ext->q = 0;
560561
ptr = doc + ext->pos;
561562

src/backend/utils/adt/txid.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535

3636
/* txid will be signed int8 in database, so must limit to 63 bits */
37-
#define MAX_TXID UINT64CONST(0x7FFFFFFFFFFFFFFF)
37+
#define MAX_TXID ((uint64) INT64_MAX)
3838

3939
/* Use unsigned variant internally */
4040
typedef uint64 txid;

src/include/c.h

+41
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,36 @@ typedef uint8 bits8; /* >= 8 bits */
249249
typedef uint16 bits16; /* >= 16 bits */
250250
typedef uint32 bits32; /* >= 32 bits */
251251

252+
/* should be defined in stdint.h, but we guarantee them here */
253+
#ifndef INT8_MIN
254+
#define INT8_MIN (-0x7F-1)
255+
#endif
256+
#ifndef INT8_MAX
257+
#define INT8_MAX (0x7F)
258+
#endif
259+
#ifndef INT16_MIN
260+
#define INT16_MIN (-0x7FFF-1)
261+
#endif
262+
#ifndef INT16_MAX
263+
#define INT16_MAX (0x7FFF)
264+
#endif
265+
#ifndef INT32_MIN
266+
#define INT32_MIN (-0x7FFFFFFF-1)
267+
#endif
268+
#ifndef INT32_MAX
269+
#define INT32_MAX (0x7FFFFFFF)
270+
#endif
271+
272+
#ifndef UINT8_MAX
273+
#define UINT8_MAX (0xFF)
274+
#endif
275+
#ifndef UINT16_MAX
276+
#define UINT16_MAX (0xFFFF)
277+
#endif
278+
#ifndef UINT32_MAX
279+
#define UINT32_MAX (0xFFFFFFFF)
280+
#endif
281+
252282
/*
253283
* 64-bit integers
254284
*/
@@ -284,6 +314,17 @@ typedef unsigned long long int uint64;
284314
#define UINT64CONST(x) ((uint64) x)
285315
#endif
286316

317+
/* should be defined in stdint.h, but we guarantee them here */
318+
#ifndef INT64_MIN
319+
#define INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
320+
#endif
321+
#ifndef INT64_MAX
322+
#define INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
323+
#endif
324+
#ifndef UINT64_MAX
325+
#define UINT64_MAX UINT64CONST(0xFFFFFFFFFFFFFFFF)
326+
#endif
327+
287328
/* snprintf format strings to use for 64-bit integers */
288329
#define INT64_FORMAT "%" INT64_MODIFIER "d"
289330
#define UINT64_FORMAT "%" INT64_MODIFIER "u"

src/include/datatype/timestamp.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ typedef struct
119119
* DT_NOBEGIN represents timestamp -infinity; DT_NOEND represents +infinity
120120
*/
121121
#ifdef HAVE_INT64_TIMESTAMP
122-
#define DT_NOBEGIN (-INT64CONST(0x7fffffffffffffff) - 1)
123-
#define DT_NOEND (INT64CONST(0x7fffffffffffffff))
122+
#define DT_NOBEGIN INT64_MIN
123+
#define DT_NOEND INT64_MAX
124124
#else /* !HAVE_INT64_TIMESTAMP */
125125
#ifdef HUGE_VAL
126126
#define DT_NOBEGIN (-HUGE_VAL)

src/include/executor/instrument.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ typedef enum InstrumentOption
3838
INSTRUMENT_TIMER = 1 << 0, /* needs timer (and row counts) */
3939
INSTRUMENT_BUFFERS = 1 << 1, /* needs buffer usage */
4040
INSTRUMENT_ROWS = 1 << 2, /* needs row count */
41-
INSTRUMENT_ALL = 0x7FFFFFFF
41+
INSTRUMENT_ALL = INT32_MAX
4242
} InstrumentOption;
4343

4444
typedef struct Instrumentation

src/include/nodes/parsenodes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ typedef enum TableLikeOption
587587
CREATE_TABLE_LIKE_INDEXES = 1 << 2,
588588
CREATE_TABLE_LIKE_STORAGE = 1 << 3,
589589
CREATE_TABLE_LIKE_COMMENTS = 1 << 4,
590-
CREATE_TABLE_LIKE_ALL = 0x7FFFFFFF
590+
CREATE_TABLE_LIKE_ALL = INT32_MAX
591591
} TableLikeOption;
592592

593593
/*

src/include/pg_config_manual.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
/*
4949
* Set the upper and lower bounds of sequence values.
5050
*/
51-
#define SEQ_MAXVALUE INT64CONST(0x7FFFFFFFFFFFFFFF)
51+
#define SEQ_MAXVALUE INT64_MAX
5252
#define SEQ_MINVALUE (-SEQ_MAXVALUE)
5353

5454
/*
@@ -185,7 +185,7 @@
185185
* the older rand() function, which is often different from --- and
186186
* considerably inferior to --- random().
187187
*/
188-
#define MAX_RANDOM_VALUE (0x7FFFFFFF)
188+
#define MAX_RANDOM_VALUE INT32_MAX
189189

190190
/*
191191
* On PPC machines, decide whether to use the mutex hint bit in LWARX

src/include/port/atomics.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ STATIC_IF_INLINE uint64
489489
pg_atomic_fetch_sub_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
490490
{
491491
AssertPointerAlignment(ptr, 8);
492-
Assert(sub_ != -INT64CONST(0x7FFFFFFFFFFFFFFF) - 1);
492+
Assert(sub_ != INT64_MIN);
493493
return pg_atomic_fetch_sub_u64_impl(ptr, sub_);
494494
}
495495

@@ -518,7 +518,7 @@ STATIC_IF_INLINE uint64
518518
pg_atomic_sub_fetch_u64(volatile pg_atomic_uint64 *ptr, int64 sub_)
519519
{
520520
AssertPointerAlignment(ptr, 8);
521-
Assert(sub_ != -INT64CONST(0x7FFFFFFFFFFFFFFF) - 1);
521+
Assert(sub_ != INT64_MIN);
522522
return pg_atomic_sub_fetch_u64_impl(ptr, sub_);
523523
}
524524

src/include/storage/predicate_internals.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef uint64 SerCommitSeqNo;
3333
* at that point. It's earlier than all normal sequence numbers,
3434
* and is only used by recovered prepared transactions
3535
*/
36-
#define InvalidSerCommitSeqNo ((SerCommitSeqNo) UINT64CONST(0xFFFFFFFFFFFFFFFF))
36+
#define InvalidSerCommitSeqNo ((SerCommitSeqNo) UINT64_MAX)
3737
#define RecoverySerCommitSeqNo ((SerCommitSeqNo) 1)
3838
#define FirstNormalSerCommitSeqNo ((SerCommitSeqNo) 2)
3939

src/include/utils/date.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ typedef struct
3535

3636
/*
3737
* Infinity and minus infinity must be the max and min values of DateADT.
38-
* We could use INT_MIN and INT_MAX here, but seems better to not assume that
39-
* int32 == int.
4038
*/
41-
#define DATEVAL_NOBEGIN ((DateADT) (-0x7fffffff - 1))
42-
#define DATEVAL_NOEND ((DateADT) 0x7fffffff)
39+
#define DATEVAL_NOBEGIN ((DateADT) INT32_MIN)
40+
#define DATEVAL_NOEND ((DateADT) INT32_MAX)
4341

4442
#define DATE_NOBEGIN(j) ((j) = DATEVAL_NOBEGIN)
4543
#define DATE_IS_NOBEGIN(j) ((j) == DATEVAL_NOBEGIN)

0 commit comments

Comments
 (0)