diff options
author | Andres Freund <andres@anarazel.de> | 2021-03-17 16:18:37 -0700 |
---|---|---|
committer | Andres Freund <andres@anarazel.de> | 2021-03-17 16:21:46 -0700 |
commit | 5f79580ad69f6e696365bdc63bc265f45bd77211 (patch) | |
tree | e00826de1ff309ad8bddbdec330131058209ff4d /src | |
parent | 70945649d734d16be22c3d1d90dd8c3d3c1e9d89 (diff) | |
download | postgresql-5f79580ad69f6e696365bdc63bc265f45bd77211.tar.gz postgresql-5f79580ad69f6e696365bdc63bc265f45bd77211.zip |
Fix memory lifetime issues of replication slot stats.
When accessing replication slot stats, introduced in 98681675002d,
pgstat_read_statsfiles() reads the data into newly allocated
memory. Unfortunately the current memory context at that point is the
callers, leading to leaks and use-after-free dangers.
The fix is trivial, explicitly use pgStatLocalContext. There's some
potential for further improvements, but that's outside of the scope of
this bugfix.
No backpatch necessary, feature is only in HEAD.
Author: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/20210317230447.c7uc4g3vbs4wi32i@alap3.anarazel.de
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/postmaster/pgstat.c | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/src/backend/postmaster/pgstat.c b/src/backend/postmaster/pgstat.c index b1e2d94951d..208a33692f3 100644 --- a/src/backend/postmaster/pgstat.c +++ b/src/backend/postmaster/pgstat.c @@ -5568,7 +5568,9 @@ pgstat_read_statsfiles(Oid onlydb, bool permanent, bool deep) HASH_ELEM | HASH_BLOBS | HASH_CONTEXT); /* Allocate the space for replication slot statistics */ - replSlotStats = palloc0(max_replication_slots * sizeof(PgStat_ReplSlotStats)); + replSlotStats = MemoryContextAllocZero(pgStatLocalContext, + max_replication_slots + * sizeof(PgStat_ReplSlotStats)); nReplSlotStats = 0; /* @@ -6323,6 +6325,8 @@ pgstat_clear_snapshot(void) pgStatDBHash = NULL; localBackendStatusTable = NULL; localNumBackends = 0; + replSlotStats = NULL; + nReplSlotStats = 0; } |