17
17
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
18
18
* Portions Copyright (c) 1994, Regents of the University of California
19
19
*
20
- * $PostgreSQL: pgsql/src/interfaces/libpq/pqexpbuffer.c,v 1.16 2003/11/29 19:52:12 pgsql Exp $
20
+ * $PostgreSQL: pgsql/src/interfaces/libpq/pqexpbuffer.c,v 1.17 2004/05/14 00:20:38 tgl Exp $
21
21
*
22
22
*-------------------------------------------------------------------------
23
23
*/
24
24
25
25
#include "postgres_fe.h"
26
26
27
+ #include <limits.h>
28
+
27
29
#include "pqexpbuffer.h"
28
30
29
31
#ifdef WIN32
@@ -132,7 +134,18 @@ enlargePQExpBuffer(PQExpBuffer str, size_t needed)
132
134
size_t newlen ;
133
135
char * newdata ;
134
136
137
+ /*
138
+ * Guard against ridiculous "needed" values, which can occur if we're
139
+ * fed bogus data. Without this, we can get an overflow or infinite
140
+ * loop in the following.
141
+ */
142
+ if (needed >= ((size_t ) INT_MAX - str -> len ))
143
+ return 0 ;
144
+
135
145
needed += str -> len + 1 ; /* total space required now */
146
+
147
+ /* Because of the above test, we now have needed <= INT_MAX */
148
+
136
149
if (needed <= str -> maxlen )
137
150
return 1 ; /* got enough space already */
138
151
@@ -146,6 +159,14 @@ enlargePQExpBuffer(PQExpBuffer str, size_t needed)
146
159
while (needed > newlen )
147
160
newlen = 2 * newlen ;
148
161
162
+ /*
163
+ * Clamp to INT_MAX in case we went past it. Note we are assuming
164
+ * here that INT_MAX <= UINT_MAX/2, else the above loop could
165
+ * overflow. We will still have newlen >= needed.
166
+ */
167
+ if (newlen > (size_t ) INT_MAX )
168
+ newlen = (size_t ) INT_MAX ;
169
+
149
170
newdata = (char * ) realloc (str -> data , newlen );
150
171
if (newdata != NULL )
151
172
{
0 commit comments