summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Eisentraut2024-10-18 09:28:54 +0000
committerPeter Eisentraut2024-10-18 09:29:20 +0000
commit4b652692e97923298d0c7b9c1c303fc648984d92 (patch)
tree1f9ec48e64d60c32020f1f2a0d01017a58b1bc30
parent24a36f91e32d6cbb8182c4cc6529d6e47587c988 (diff)
Fix memory leaks from incorrect strsep() uses
Commit 5d2e1cc117b 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 5d2e1cc117b. The other two don't have this problem.) Reported-by: Alexander Lakhin <[email protected]> Discussion: https://www.postgresql.org/message-id/flat/[email protected]
-rw-r--r--src/common/logging.c3
-rw-r--r--src/test/regress/pg_regress.c7
2 files changed, 7 insertions, 3 deletions
diff --git a/src/common/logging.c b/src/common/logging.c
index aedd1ae2d8c..3cf119090a5 100644
--- a/src/common/logging.c
+++ b/src/common/logging.c
@@ -120,8 +120,9 @@ pg_logging_init(const char *argv0)
if (colors)
{
char *token;
+ char *cp = colors;
- while ((token = strsep(&colors, ":")))
+ while ((token = strsep(&cp, ":")))
{
char *e = strchr(token, '=');
diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c
index 5157629b1cc..6c188954b14 100644
--- a/src/test/regress/pg_regress.c
+++ b/src/test/regress/pg_regress.c
@@ -233,14 +233,17 @@ free_stringlist(_stringlist **listhead)
static void
split_to_stringlist(const char *s, const char *delim, _stringlist **listhead)
{
- char *sc = pg_strdup(s);
char *token;
+ char *sc;
+ char *tofree;
+
+ tofree = sc = pg_strdup(s);
while ((token = strsep(&sc, delim)))
{
add_stringlist_item(listhead, token);
}
- free(sc);
+ free(tofree);
}
/*