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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 1 addition & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 8 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)