Skip to content

Commit a691a22

Browse files
committed
Fix memory overrun when querying pg_stat_slru
pg_stat_get_slru() in pgstatfuncs.c would point to one element after the end of the array PgStat_SLRUStats when finishing to scan its entries. This had no direct consequences as no data from the extra memory area was read, but static analyzers would rightfully complain here. So let's be clean. While on it, this adds one regression test in the area reserved for system views. Reported-by: Alexander Kozhemyakin, via AddressSanitizer Author: Kyotaro Horiguchi Discussion: https://postgr.es/m/17280-37da556e86032070@postgresql.org Backpatch-through: 13
1 parent d4e9d69 commit a691a22

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/backend/utils/adt/pgstatfuncs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,14 +1744,15 @@ pg_stat_get_slru(PG_FUNCTION_ARGS)
17441744
/* for each row */
17451745
Datum values[PG_STAT_GET_SLRU_COLS];
17461746
bool nulls[PG_STAT_GET_SLRU_COLS];
1747-
PgStat_SLRUStats stat = stats[i];
1747+
PgStat_SLRUStats stat;
17481748
const char *name;
17491749

17501750
name = pgstat_slru_name(i);
17511751

17521752
if (!name)
17531753
break;
17541754

1755+
stat = stats[i];
17551756
MemSet(values, 0, sizeof(values));
17561757
MemSet(nulls, 0, sizeof(nulls));
17571758

src/test/regress/expected/sysviews.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ select count(*) >= 0 as ok from pg_prepared_xacts;
6767
t
6868
(1 row)
6969

70+
-- There will surely be at least one SLRU cache
71+
select count(*) > 0 as ok from pg_stat_slru;
72+
ok
73+
----
74+
t
75+
(1 row)
76+
7077
-- We expect no walreceiver running in this test
7178
select count(*) = 0 as ok from pg_stat_wal_receiver;
7279
ok

src/test/regress/sql/sysviews.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ select count(*) = 0 as ok from pg_prepared_statements;
3232
-- See also prepared_xacts.sql
3333
select count(*) >= 0 as ok from pg_prepared_xacts;
3434

35+
-- There will surely be at least one SLRU cache
36+
select count(*) > 0 as ok from pg_stat_slru;
37+
3538
-- We expect no walreceiver running in this test
3639
select count(*) = 0 as ok from pg_stat_wal_receiver;
3740

0 commit comments

Comments
 (0)