Skip to content

Commit 5588c55

Browse files
committed
Cleanups for int8: guard against null inputs in comparison
operators (and some other places), fix rangechecks in int8 to int4 conversion (same problem we recently figured out in pg_atoi).
1 parent d91baea commit 5588c55

File tree

1 file changed

+87
-19
lines changed

1 file changed

+87
-19
lines changed

src/backend/utils/adt/int8.c

Lines changed: 87 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,30 @@
99
#include <time.h>
1010
#include <math.h>
1111
#include <float.h>
12-
#include <limits.h>
1312

1413
#include "postgres.h"
14+
15+
#ifdef HAVE_LIMITS_H
16+
#include <limits.h>
17+
#endif
18+
1519
#include "utils/int8.h"
1620

1721
#define MAXINT8LEN 25
1822

23+
#ifndef INT_MAX
24+
#define INT_MAX (0x7FFFFFFFL)
25+
#endif
26+
#ifndef INT_MIN
27+
#define INT_MIN (-INT_MAX-1)
28+
#endif
29+
#ifndef SHRT_MAX
30+
#define SHRT_MAX (0x7FFF)
31+
#endif
32+
#ifndef SHRT_MIN
33+
#define SHRT_MIN (-SHRT_MAX-1)
34+
#endif
35+
1936

2037
/***********************************************************************
2138
**
@@ -38,7 +55,7 @@ int8in(char *str)
3855
int sign = 1;
3956

4057
if (!PointerIsValid(str))
41-
elog(ERROR, "Bad (null) int8 external representation", NULL);
58+
elog(ERROR, "Bad (null) int8 external representation");
4259

4360
/*
4461
* Do our own scan, rather than relying on sscanf which might be
@@ -78,7 +95,7 @@ int8out(int64 *val)
7895
return NULL;
7996

8097
if ((len = snprintf(buf, MAXINT8LEN, INT64_FORMAT, *val)) < 0)
81-
elog(ERROR, "Unable to format int8", NULL);
98+
elog(ERROR, "Unable to format int8");
8299

83100
result = palloc(len + 1);
84101

@@ -98,36 +115,54 @@ int8out(int64 *val)
98115
bool
99116
int8eq(int64 *val1, int64 *val2)
100117
{
118+
if (!val1 || !val2)
119+
return 0;
120+
101121
return *val1 == *val2;
102122
} /* int8eq() */
103123

104124
bool
105125
int8ne(int64 *val1, int64 *val2)
106126
{
127+
if (!val1 || !val2)
128+
return 0;
129+
107130
return *val1 != *val2;
108131
} /* int8ne() */
109132

110133
bool
111134
int8lt(int64 *val1, int64 *val2)
112135
{
136+
if (!val1 || !val2)
137+
return 0;
138+
113139
return *val1 < *val2;
114140
} /* int8lt() */
115141

116142
bool
117143
int8gt(int64 *val1, int64 *val2)
118144
{
145+
if (!val1 || !val2)
146+
return 0;
147+
119148
return *val1 > *val2;
120149
} /* int8gt() */
121150

122151
bool
123152
int8le(int64 *val1, int64 *val2)
124153
{
154+
if (!val1 || !val2)
155+
return 0;
156+
125157
return *val1 <= *val2;
126158
} /* int8le() */
127159

128160
bool
129161
int8ge(int64 *val1, int64 *val2)
130162
{
163+
if (!val1 || !val2)
164+
return 0;
165+
131166
return *val1 >= *val2;
132167
} /* int8ge() */
133168

@@ -138,36 +173,54 @@ int8ge(int64 *val1, int64 *val2)
138173
bool
139174
int84eq(int64 *val1, int32 val2)
140175
{
176+
if (!val1)
177+
return 0;
178+
141179
return *val1 == val2;
142180
} /* int84eq() */
143181

144182
bool
145183
int84ne(int64 *val1, int32 val2)
146184
{
185+
if (!val1)
186+
return 0;
187+
147188
return *val1 != val2;
148189
} /* int84ne() */
149190

150191
bool
151192
int84lt(int64 *val1, int32 val2)
152193
{
194+
if (!val1)
195+
return 0;
196+
153197
return *val1 < val2;
154198
} /* int84lt() */
155199

156200
bool
157201
int84gt(int64 *val1, int32 val2)
158202
{
203+
if (!val1)
204+
return 0;
205+
159206
return *val1 > val2;
160207
} /* int84gt() */
161208

162209
bool
163210
int84le(int64 *val1, int32 val2)
164211
{
212+
if (!val1)
213+
return 0;
214+
165215
return *val1 <= val2;
166216
} /* int84le() */
167217

168218
bool
169219
int84ge(int64 *val1, int32 val2)
170220
{
221+
if (!val1)
222+
return 0;
223+
171224
return *val1 >= val2;
172225
} /* int84ge() */
173226

@@ -178,36 +231,54 @@ int84ge(int64 *val1, int32 val2)
178231
bool
179232
int48eq(int32 val1, int64 *val2)
180233
{
234+
if (!val2)
235+
return 0;
236+
181237
return val1 == *val2;
182238
} /* int48eq() */
183239

184240
bool
185241
int48ne(int32 val1, int64 *val2)
186242
{
243+
if (!val2)
244+
return 0;
245+
187246
return val1 != *val2;
188247
} /* int48ne() */
189248

190249
bool
191250
int48lt(int32 val1, int64 *val2)
192251
{
252+
if (!val2)
253+
return 0;
254+
193255
return val1 < *val2;
194256
} /* int48lt() */
195257

196258
bool
197259
int48gt(int32 val1, int64 *val2)
198260
{
261+
if (!val2)
262+
return 0;
263+
199264
return val1 > *val2;
200265
} /* int48gt() */
201266

202267
bool
203268
int48le(int32 val1, int64 *val2)
204269
{
270+
if (!val2)
271+
return 0;
272+
205273
return val1 <= *val2;
206274
} /* int48le() */
207275

208276
bool
209277
int48ge(int32 val1, int64 *val2)
210278
{
279+
if (!val2)
280+
return 0;
281+
211282
return val1 >= *val2;
212283
} /* int48ge() */
213284

@@ -436,19 +507,10 @@ int84(int64 *val)
436507
int32 result;
437508

438509
if (!PointerIsValid(val))
439-
elog(ERROR, "Invalid (null) int64, can't convert int8 to int4", NULL);
510+
elog(ERROR, "Invalid (null) int64, can't convert int8 to int4");
440511

441-
#if NOT_USED
442-
443-
/*
444-
* Hmm. This conditional always tests true on my i686/linux box. It's
445-
* a gcc compiler bug, or I'm missing something obvious, which is more
446-
* likely... - thomas 1998-06-09
447-
*/
448512
if ((*val < INT_MIN) || (*val > INT_MAX))
449-
#endif
450-
if ((*val < (-pow(2, 31) + 1)) || (*val > (pow(2, 31) - 1)))
451-
elog(ERROR, "int8 conversion to int4 is out of range", NULL);
513+
elog(ERROR, "int8 conversion to int4 is out of range");
452514

453515
result = *val;
454516

@@ -474,10 +536,10 @@ int82(int64 *val)
474536
int16 result;
475537

476538
if (!PointerIsValid(val))
477-
elog(ERROR, "Invalid (null) int8, can't convert to int2", NULL);
539+
elog(ERROR, "Invalid (null) int8, can't convert to int2");
478540

479-
if ((*val < (-pow(2, 15) + 1)) || (*val > (pow(2, 15) - 1)))
480-
elog(ERROR, "int8 conversion to int2 is out of range", NULL);
541+
if ((*val < SHRT_MIN) || (*val > SHRT_MAX))
542+
elog(ERROR, "int8 conversion to int2 is out of range");
481543

482544
result = *val;
483545

@@ -491,6 +553,9 @@ i8tod(int64 *val)
491553
{
492554
float64 result = palloc(sizeof(float64data));
493555

556+
if (!PointerIsValid(val))
557+
elog(ERROR, "Invalid (null) int8, can't convert to float8");
558+
494559
*result = *val;
495560

496561
return result;
@@ -511,8 +576,11 @@ dtoi8(float64 val)
511576
{
512577
int64 *result = palloc(sizeof(int64));
513578

579+
if (!PointerIsValid(val))
580+
elog(ERROR, "Invalid (null) float8, can't convert to int8");
581+
514582
if ((*val < (-pow(2, 63) + 1)) || (*val > (pow(2, 63) - 1)))
515-
elog(ERROR, "Floating point conversion to int64 is out of range", NULL);
583+
elog(ERROR, "Floating point conversion to int64 is out of range");
516584

517585
*result = *val;
518586

@@ -528,7 +596,7 @@ text_int8(text *str)
528596
char *s;
529597

530598
if (!PointerIsValid(str))
531-
elog(ERROR, "Bad (null) int8 external representation", NULL);
599+
elog(ERROR, "Bad (null) int8 external representation");
532600

533601
len = (VARSIZE(str) - VARHDRSZ);
534602
s = palloc(len + 1);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy