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

Commit 02409a4

Browse files
committed
Propagate enlargeStringInfo() fixes into the equivalent code in
pqexpbuffer.c. While a client-side failure doesn't seem like a security issue, it's still a bug.
1 parent 0ec80be commit 02409a4

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/interfaces/libpq/pqexpbuffer.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@
1717
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
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 $
2121
*
2222
*-------------------------------------------------------------------------
2323
*/
2424

2525
#include "postgres_fe.h"
2626

27+
#include <limits.h>
28+
2729
#include "pqexpbuffer.h"
2830

2931
#ifdef WIN32
@@ -132,7 +134,18 @@ enlargePQExpBuffer(PQExpBuffer str, size_t needed)
132134
size_t newlen;
133135
char *newdata;
134136

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+
135145
needed += str->len + 1; /* total space required now */
146+
147+
/* Because of the above test, we now have needed <= INT_MAX */
148+
136149
if (needed <= str->maxlen)
137150
return 1; /* got enough space already */
138151

@@ -146,6 +159,14 @@ enlargePQExpBuffer(PQExpBuffer str, size_t needed)
146159
while (needed > newlen)
147160
newlen = 2 * newlen;
148161

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+
149170
newdata = (char *) realloc(str->data, newlen);
150171
if (newdata != NULL)
151172
{

0 commit comments

Comments
 (0)