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

Commit 7be39bb

Browse files
committed
Tigthen binary receive functions so that they reject values that the text
input functions don't accept either. While the backend can handle such values fine, they can cause trouble in clients and in pg_dump/restore. This is followup to the original issue on time datatype reported by Andrew McNamara a while ago. Like that one, none of these seem worth back-patching.
1 parent 237859e commit 7be39bb

File tree

7 files changed

+63
-17
lines changed

7 files changed

+63
-17
lines changed

src/backend/utils/adt/arrayfuncs.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.160 2009/06/22 04:37:18 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/arrayfuncs.c,v 1.161 2009/09/04 11:20:22 heikki Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1207,8 +1207,17 @@ array_recv(PG_FUNCTION_ARGS)
12071207

12081208
for (i = 0; i < ndim; i++)
12091209
{
1210+
int ub;
1211+
12101212
dim[i] = pq_getmsgint(buf, 4);
12111213
lBound[i] = pq_getmsgint(buf, 4);
1214+
1215+
ub = lBound[i] + dim[i] - 1;
1216+
/* overflow? */
1217+
if (lBound[i] > ub)
1218+
ereport(ERROR,
1219+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1220+
errmsg("integer out of range")));
12121221
}
12131222

12141223
/* This checks for overflow of array dimensions */

src/backend/utils/adt/date.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.147 2009/07/29 22:19:18 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.148 2009/09/04 11:20:22 heikki Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -203,8 +203,17 @@ Datum
203203
date_recv(PG_FUNCTION_ARGS)
204204
{
205205
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
206+
DateADT result;
206207

207-
PG_RETURN_DATEADT((DateADT) pq_getmsgint(buf, sizeof(DateADT)));
208+
result = (DateADT) pq_getmsgint(buf, sizeof(DateADT));
209+
210+
/* Limit to the same range that date_in() accepts. */
211+
if (result < 0 || result > JULIAN_MAX)
212+
ereport(ERROR,
213+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
214+
errmsg("date out of range")));
215+
216+
PG_RETURN_DATEADT(result);
208217
}
209218

210219
/*

src/backend/utils/adt/int.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.85 2009/09/03 18:48:14 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.86 2009/09/04 11:20:22 heikki Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -225,13 +225,21 @@ int2vectorrecv(PG_FUNCTION_ARGS)
225225

226226
Assert(!locfcinfo.isnull);
227227

228-
/* sanity checks: int2vector must be 1-D, no nulls */
228+
/* sanity checks: int2vector must be 1-D, 0-based, no nulls */
229229
if (ARR_NDIM(result) != 1 ||
230230
ARR_HASNULL(result) ||
231-
ARR_ELEMTYPE(result) != INT2OID)
231+
ARR_ELEMTYPE(result) != INT2OID ||
232+
ARR_LBOUND(result)[0] != 0)
232233
ereport(ERROR,
233234
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
234235
errmsg("invalid int2vector data")));
236+
237+
/* check length for consistency with int2vectorin() */
238+
if (ARR_DIMS(result)[0] > FUNC_MAX_ARGS)
239+
ereport(ERROR,
240+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
241+
errmsg("oidvector has too many elements")));
242+
235243
PG_RETURN_POINTER(result);
236244
}
237245

src/backend/utils/adt/nabstime.c

+11-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.161 2009/06/11 14:49:03 momjian Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/adt/nabstime.c,v 1.162 2009/09/04 11:20:22 heikki Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -786,20 +786,25 @@ tintervalrecv(PG_FUNCTION_ARGS)
786786
{
787787
StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
788788
TimeInterval tinterval;
789+
int32 status;
789790

790791
tinterval = (TimeInterval) palloc(sizeof(TimeIntervalData));
791792

792793
tinterval->status = pq_getmsgint(buf, sizeof(tinterval->status));
794+
tinterval->data[0] = pq_getmsgint(buf, sizeof(tinterval->data[0]));
795+
tinterval->data[1] = pq_getmsgint(buf, sizeof(tinterval->data[1]));
796+
797+
if (tinterval->data[0] == INVALID_ABSTIME ||
798+
tinterval->data[1] == INVALID_ABSTIME)
799+
status = T_INTERVAL_INVAL; /* undefined */
800+
else
801+
status = T_INTERVAL_VALID;
793802

794-
if (!(tinterval->status == T_INTERVAL_INVAL ||
795-
tinterval->status == T_INTERVAL_VALID))
803+
if (status != tinterval->status)
796804
ereport(ERROR,
797805
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
798806
errmsg("invalid status in external \"tinterval\" value")));
799807

800-
tinterval->data[0] = pq_getmsgint(buf, sizeof(tinterval->data[0]));
801-
tinterval->data[1] = pq_getmsgint(buf, sizeof(tinterval->data[1]));
802-
803808
PG_RETURN_TIMEINTERVAL(tinterval);
804809
}
805810

src/backend/utils/adt/oid.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/oid.c,v 1.74 2009/01/01 17:23:49 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/oid.c,v 1.75 2009/09/04 11:20:22 heikki Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -276,13 +276,21 @@ oidvectorrecv(PG_FUNCTION_ARGS)
276276

277277
Assert(!locfcinfo.isnull);
278278

279-
/* sanity checks: oidvector must be 1-D, no nulls */
279+
/* sanity checks: oidvector must be 1-D, 0-based, no nulls */
280280
if (ARR_NDIM(result) != 1 ||
281281
ARR_HASNULL(result) ||
282-
ARR_ELEMTYPE(result) != OIDOID)
282+
ARR_ELEMTYPE(result) != OIDOID ||
283+
ARR_LBOUND(result)[0] != 0)
283284
ereport(ERROR,
284285
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
285286
errmsg("invalid oidvector data")));
287+
288+
/* check length for consistency with oidvectorin() */
289+
if (ARR_DIMS(result)[0] > FUNC_MAX_ARGS)
290+
ereport(ERROR,
291+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
292+
errmsg("oidvector has too many elements")));
293+
286294
PG_RETURN_POINTER(result);
287295
}
288296

src/backend/utils/adt/timestamp.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.202 2009/07/06 20:29:23 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.203 2009/09/04 11:20:22 heikki Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -253,6 +253,11 @@ timestamp_recv(PG_FUNCTION_ARGS)
253253
timestamp = (Timestamp) pq_getmsgint64(buf);
254254
#else
255255
timestamp = (Timestamp) pq_getmsgfloat8(buf);
256+
257+
if (isnan(timestamp))
258+
ereport(ERROR,
259+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
260+
errmsg("timestamp cannot be NaN")));
256261
#endif
257262

258263
/* rangecheck: see if timestamp_out would like it */

src/include/utils/datetime.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
1010
* Portions Copyright (c) 1994, Regents of the University of California
1111
*
12-
* $PostgreSQL: pgsql/src/include/utils/datetime.h,v 1.75 2009/06/11 14:49:13 momjian Exp $
12+
* $PostgreSQL: pgsql/src/include/utils/datetime.h,v 1.76 2009/09/04 11:20:23 heikki Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -262,6 +262,8 @@ extern const int day_tab[2][13];
262262
|| (((m) == JULIAN_MINMONTH) && ((d) >= JULIAN_MINDAY))))) \
263263
&& ((y) < JULIAN_MAXYEAR))
264264

265+
#define JULIAN_MAX (2145031948) /* == date2j(JULIAN_MAXYEAR, 1 ,1) */
266+
265267
/* Julian-date equivalents of Day 0 in Unix and Postgres reckoning */
266268
#define UNIX_EPOCH_JDATE 2440588 /* == date2j(1970, 1, 1) */
267269
#define POSTGRES_EPOCH_JDATE 2451545 /* == date2j(2000, 1, 1) */

0 commit comments

Comments
 (0)