Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5588c55

Browse files
committed
Cleanups for int8: guard against null inputs in comparison
operators (and some other places), fix rangechecks in int8 to int4conversion (same problem we recently figured out in pg_atoi).
1 parentd91baea commit5588c55

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+
#ifdefHAVE_LIMITS_H
16+
#include<limits.h>
17+
#endif
18+
1519
#include"utils/int8.h"
1620

1721
#defineMAXINT8LEN25
1822

23+
#ifndefINT_MAX
24+
#defineINT_MAX (0x7FFFFFFFL)
25+
#endif
26+
#ifndefINT_MIN
27+
#defineINT_MIN (-INT_MAX-1)
28+
#endif
29+
#ifndefSHRT_MAX
30+
#defineSHRT_MAX (0x7FFF)
31+
#endif
32+
#ifndefSHRT_MIN
33+
#defineSHRT_MIN (-SHRT_MAX-1)
34+
#endif
35+
1936

2037
/***********************************************************************
2138
**
@@ -38,7 +55,7 @@ int8in(char *str)
3855
intsign=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
returnNULL;
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+
return0;
120+
101121
return*val1==*val2;
102122
}/* int8eq() */
103123

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

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

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

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

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

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

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

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

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

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

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

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

184240
bool
185241
int48ne(int32val1,int64*val2)
186242
{
243+
if (!val2)
244+
return0;
245+
187246
returnval1!=*val2;
188247
}/* int48ne() */
189248

190249
bool
191250
int48lt(int32val1,int64*val2)
192251
{
252+
if (!val2)
253+
return0;
254+
193255
returnval1<*val2;
194256
}/* int48lt() */
195257

196258
bool
197259
int48gt(int32val1,int64*val2)
198260
{
261+
if (!val2)
262+
return0;
263+
199264
returnval1>*val2;
200265
}/* int48gt() */
201266

202267
bool
203268
int48le(int32val1,int64*val2)
204269
{
270+
if (!val2)
271+
return0;
272+
205273
returnval1 <=*val2;
206274
}/* int48le() */
207275

208276
bool
209277
int48ge(int32val1,int64*val2)
210278
{
279+
if (!val2)
280+
return0;
281+
211282
returnval1 >=*val2;
212283
}/* int48ge() */
213284

@@ -436,19 +507,10 @@ int84(int64 *val)
436507
int32result;
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-
#ifNOT_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
int16result;
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
float64result=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
returnresult;
@@ -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)

[8]ページ先頭

©2009-2025 Movatter.jp