From f06d718e119cbf17ac9a7aecb41eed385a4f4f41 Mon Sep 17 00:00:00 2001 From: "Chao Li (Evan)" Date: Wed, 4 Feb 2026 16:53:00 +0800 Subject: [PATCH v1] pg_upgrade: fix memory leak in SLRU I/O code SlruSegState->dir is allocated in AllocSlruSegState() but was never freed. Add a common cleanup helper to release all resources associated with an SLRU segment state and use it for both readers and writers. Author: Chao Li Reviewed-by: Discussion: https://postgr.es/m/ --- src/bin/pg_upgrade/slru_io.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/bin/pg_upgrade/slru_io.c b/src/bin/pg_upgrade/slru_io.c index ae3e224d7b1..efc0fb71dda 100644 --- a/src/bin/pg_upgrade/slru_io.c +++ b/src/bin/pg_upgrade/slru_io.c @@ -19,6 +19,7 @@ #include "slru_io.h" static SlruSegState *AllocSlruSegState(const char *dir); +static void FreeSlruSegState(SlruSegState *state); static char *SlruFileName(SlruSegState *state, int64 segno); static void SlruFlush(SlruSegState *state); @@ -28,7 +29,7 @@ AllocSlruSegState(const char *dir) { SlruSegState *state = pg_malloc(sizeof(*state)); - state->dir = pstrdup(dir); + state->dir = pg_strdup(dir); state->fn = NULL; state->fd = -1; state->segno = -1; @@ -69,6 +70,17 @@ AllocSlruRead(const char *dir, bool long_segment_names) return state; } +static void +FreeSlruSegState(SlruSegState *state) +{ + if (state->fd != -1) + close(state->fd); + + pg_free(state->fn); + pg_free(state->dir); + pg_free(state); +} + /* * Read the given page into memory buffer. * @@ -154,9 +166,7 @@ FreeSlruRead(SlruSegState *state) { Assert(!state->writing); /* read only mode */ - if (state->fd != -1) - close(state->fd); - pg_free(state); + FreeSlruSegState(state); } /* @@ -263,7 +273,5 @@ FreeSlruWrite(SlruSegState *state) SlruFlush(state); - if (state->fd != -1) - close(state->fd); - pg_free(state); + FreeSlruSegState(state); } -- 2.50.1 (Apple Git-155)