Skip to content

Commit 42f50cb

Browse files
committed
Fix overflow check in StringInfo; add missing casts
A few thinkos I introduced in fa2fa99. Also, amend a similarly broken comment. Report by Daniel Vérité. Authors: Daniel Vérité, Álvaro Herrera Discussion: https://postgr.es/m/1706e85e-60d2-494e-8a64-9af1e1b2186e@manitou-mail.org
1 parent e898437 commit 42f50cb

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/backend/lib/stringinfo.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,19 +313,20 @@ enlargeStringInfo(StringInfo str, int needed)
313313
* for efficiency, double the buffer size each time it overflows.
314314
* Actually, we might need to more than double it if 'needed' is big...
315315
*/
316-
newlen = 2 * str->maxlen;
317-
while (needed > newlen)
316+
newlen = 2 * (Size) str->maxlen;
317+
while ((Size) needed > newlen)
318318
newlen = 2 * newlen;
319319

320320
/*
321-
* Clamp to the limit in case we went past it. Note we are assuming here
322-
* that limit <= INT_MAX/2, else the above loop could overflow. We will
323-
* still have newlen >= needed.
321+
* Clamp to the limit in case we went past it. (We used to depend on
322+
* limit <= INT32_MAX/2, to avoid overflow in the loop above; we no longer
323+
* depend on that, but if "needed" and str->maxlen ever become wider, we
324+
* will need similar caution here.) We will still have newlen >= needed.
324325
*/
325326
if (newlen > limit)
326327
newlen = limit;
327328

328-
str->data = (char *) repalloc_huge(str->data, (Size) newlen);
329+
str->data = (char *) repalloc_huge(str->data, newlen);
329330

330331
str->maxlen = newlen;
331332
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy