Skip to content

Commit edcaa8f

Browse files
committed
Fix off-by-one memory allocation, as reported by Rod Taylor. Also
avoid repalloc'ing twice when once is sufficient.
1 parent 6444bc3 commit edcaa8f

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

src/backend/libpq/hba.c

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/libpq/hba.c,v 1.133 2004/10/12 21:54:38 petere Exp $
13+
* $PostgreSQL: pgsql/src/backend/libpq/hba.c,v 1.134 2004/11/17 19:54:24 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -218,6 +218,7 @@ next_token_expand(FILE *file)
218218
char *comma_str = pstrdup("");
219219
bool trailing_comma;
220220
char *incbuf;
221+
int needed;
221222

222223
do
223224
{
@@ -239,16 +240,14 @@ next_token_expand(FILE *file)
239240
else
240241
incbuf = pstrdup(buf);
241242

242-
comma_str = repalloc(comma_str,
243-
strlen(comma_str) + strlen(incbuf) + 1);
243+
needed = strlen(comma_str) + strlen(incbuf) + 1;
244+
if (trailing_comma)
245+
needed++;
246+
comma_str = repalloc(comma_str, needed);
244247
strcat(comma_str, incbuf);
245-
pfree(incbuf);
246-
247248
if (trailing_comma)
248-
{
249-
comma_str = repalloc(comma_str, strlen(comma_str) + 1 + 1);
250249
strcat(comma_str, MULTI_VALUE_SEP);
251-
}
250+
pfree(incbuf);
252251
} while (trailing_comma);
253252

254253
return comma_str;
@@ -327,29 +326,31 @@ tokenize_inc_file(const char *inc_filename)
327326
pfree(inc_fullname);
328327

329328
/* return empty string, it matches nothing */
330-
return pstrdup("");
329+
return comma_str;
331330
}
332331
pfree(inc_fullname);
333332

334333
/* There is possible recursion here if the file contains @ */
335334
tokenize_file(inc_file, &inc_lines, &inc_line_nums);
336335
FreeFile(inc_file);
337336

338-
/* Create comma-separate string from List */
337+
/* Create comma-separated string from List */
339338
foreach(line, inc_lines)
340339
{
341340
List *token_list = (List *) lfirst(line);
342341
ListCell *token;
343342

344343
foreach(token, token_list)
345344
{
346-
if (strlen(comma_str))
347-
{
348-
comma_str = repalloc(comma_str, strlen(comma_str) + 1);
345+
int oldlen = strlen(comma_str);
346+
int needed;
347+
348+
needed = oldlen + strlen(lfirst(token)) + 1;
349+
if (oldlen > 0)
350+
needed++;
351+
comma_str = repalloc(comma_str, needed);
352+
if (oldlen > 0)
349353
strcat(comma_str, MULTI_VALUE_SEP);
350-
}
351-
comma_str = repalloc(comma_str,
352-
strlen(comma_str) + strlen(lfirst(token)) + 1);
353354
strcat(comma_str, lfirst(token));
354355
}
355356
}

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