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

Commit 4b65269

Browse files
committed
Fix memory leaks from incorrect strsep() uses
Commit 5d2e1cc introduced some strsep() uses, but it did the memory management wrong in some cases. We need to keep a separate pointer to the allocate memory so that we can free it later, because strsep() advances the pointer we pass to it, and it at the end it will be NULL, so any free() calls won't do anything. (This fixes two of the four places changed in commit 5d2e1cc. The other two don't have this problem.) Reported-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://www.postgresql.org/message-id/flat/79692bf9-17d3-41e6-b9c9-fc8c3944222a@eisentraut.org
1 parent 24a36f9 commit 4b65269

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/common/logging.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ pg_logging_init(const char *argv0)
120120
if (colors)
121121
{
122122
char *token;
123+
char *cp = colors;
123124

124-
while ((token = strsep(&colors, ":")))
125+
while ((token = strsep(&cp, ":")))
125126
{
126127
char *e = strchr(token, '=');
127128

src/test/regress/pg_regress.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,17 @@ free_stringlist(_stringlist **listhead)
233233
static void
234234
split_to_stringlist(const char *s, const char *delim, _stringlist **listhead)
235235
{
236-
char *sc = pg_strdup(s);
237236
char *token;
237+
char *sc;
238+
char *tofree;
239+
240+
tofree = sc = pg_strdup(s);
238241

239242
while ((token = strsep(&sc, delim)))
240243
{
241244
add_stringlist_item(listhead, token);
242245
}
243-
free(sc);
246+
free(tofree);
244247
}
245248

246249
/*

0 commit comments

Comments
 (0)