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)
3855int sign = 1 ;
3956
4057if (!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)
7895return NULL ;
7996
8097if ((len = snprintf (buf ,MAXINT8LEN ,INT64_FORMAT ,* val ))< 0 )
81- elog (ERROR ,"Unable to format int8" , NULL );
98+ elog (ERROR ,"Unable to format int8" );
8299
83100result = palloc (len + 1 );
84101
@@ -98,36 +115,54 @@ int8out(int64 *val)
98115bool
99116int8eq (int64 * val1 ,int64 * val2 )
100117{
118+ if (!val1 || !val2 )
119+ return 0 ;
120+
101121return * val1 == * val2 ;
102122}/* int8eq() */
103123
104124bool
105125int8ne (int64 * val1 ,int64 * val2 )
106126{
127+ if (!val1 || !val2 )
128+ return 0 ;
129+
107130return * val1 != * val2 ;
108131}/* int8ne() */
109132
110133bool
111134int8lt (int64 * val1 ,int64 * val2 )
112135{
136+ if (!val1 || !val2 )
137+ return 0 ;
138+
113139return * val1 < * val2 ;
114140}/* int8lt() */
115141
116142bool
117143int8gt (int64 * val1 ,int64 * val2 )
118144{
145+ if (!val1 || !val2 )
146+ return 0 ;
147+
119148return * val1 > * val2 ;
120149}/* int8gt() */
121150
122151bool
123152int8le (int64 * val1 ,int64 * val2 )
124153{
154+ if (!val1 || !val2 )
155+ return 0 ;
156+
125157return * val1 <=* val2 ;
126158}/* int8le() */
127159
128160bool
129161int8ge (int64 * val1 ,int64 * val2 )
130162{
163+ if (!val1 || !val2 )
164+ return 0 ;
165+
131166return * val1 >=* val2 ;
132167}/* int8ge() */
133168
@@ -138,36 +173,54 @@ int8ge(int64 *val1, int64 *val2)
138173bool
139174int84eq (int64 * val1 ,int32 val2 )
140175{
176+ if (!val1 )
177+ return 0 ;
178+
141179return * val1 == val2 ;
142180}/* int84eq() */
143181
144182bool
145183int84ne (int64 * val1 ,int32 val2 )
146184{
185+ if (!val1 )
186+ return 0 ;
187+
147188return * val1 != val2 ;
148189}/* int84ne() */
149190
150191bool
151192int84lt (int64 * val1 ,int32 val2 )
152193{
194+ if (!val1 )
195+ return 0 ;
196+
153197return * val1 < val2 ;
154198}/* int84lt() */
155199
156200bool
157201int84gt (int64 * val1 ,int32 val2 )
158202{
203+ if (!val1 )
204+ return 0 ;
205+
159206return * val1 > val2 ;
160207}/* int84gt() */
161208
162209bool
163210int84le (int64 * val1 ,int32 val2 )
164211{
212+ if (!val1 )
213+ return 0 ;
214+
165215return * val1 <=val2 ;
166216}/* int84le() */
167217
168218bool
169219int84ge (int64 * val1 ,int32 val2 )
170220{
221+ if (!val1 )
222+ return 0 ;
223+
171224return * val1 >=val2 ;
172225}/* int84ge() */
173226
@@ -178,36 +231,54 @@ int84ge(int64 *val1, int32 val2)
178231bool
179232int48eq (int32 val1 ,int64 * val2 )
180233{
234+ if (!val2 )
235+ return 0 ;
236+
181237return val1 == * val2 ;
182238}/* int48eq() */
183239
184240bool
185241int48ne (int32 val1 ,int64 * val2 )
186242{
243+ if (!val2 )
244+ return 0 ;
245+
187246return val1 != * val2 ;
188247}/* int48ne() */
189248
190249bool
191250int48lt (int32 val1 ,int64 * val2 )
192251{
252+ if (!val2 )
253+ return 0 ;
254+
193255return val1 < * val2 ;
194256}/* int48lt() */
195257
196258bool
197259int48gt (int32 val1 ,int64 * val2 )
198260{
261+ if (!val2 )
262+ return 0 ;
263+
199264return val1 > * val2 ;
200265}/* int48gt() */
201266
202267bool
203268int48le (int32 val1 ,int64 * val2 )
204269{
270+ if (!val2 )
271+ return 0 ;
272+
205273return val1 <=* val2 ;
206274}/* int48le() */
207275
208276bool
209277int48ge (int32 val1 ,int64 * val2 )
210278{
279+ if (!val2 )
280+ return 0 ;
281+
211282return val1 >=* val2 ;
212283}/* int48ge() */
213284
@@ -436,19 +507,10 @@ int84(int64 *val)
436507int32 result ;
437508
438509if (!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- */
448512if ((* 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
453515result = * val ;
454516
@@ -474,10 +536,10 @@ int82(int64 *val)
474536int16 result ;
475537
476538if (!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
482544result = * val ;
483545
@@ -491,6 +553,9 @@ i8tod(int64 *val)
491553{
492554float64 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
496561return result ;
@@ -511,8 +576,11 @@ dtoi8(float64 val)
511576{
512577int64 * result = palloc (sizeof (int64 ));
513578
579+ if (!PointerIsValid (val ))
580+ elog (ERROR ,"Invalid (null) float8, can't convert to int8" );
581+
514582if ((* 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)
528596char * s ;
529597
530598if (!PointerIsValid (str ))
531- elog (ERROR ,"Bad (null) int8 external representation" , NULL );
599+ elog (ERROR ,"Bad (null) int8 external representation" );
532600
533601len = (VARSIZE (str )- VARHDRSZ );
534602s = palloc (len + 1 );