Skip to content

Commit 5f81a48

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 675cd76 commit 5f81a48

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
@@ -1912,14 +1912,15 @@ pg_stat_get_slru(PG_FUNCTION_ARGS)
19121912
/* for each row */
19131913
Datum values[PG_STAT_GET_SLRU_COLS];
19141914
bool nulls[PG_STAT_GET_SLRU_COLS];
1915-
PgStat_SLRUStats stat = stats[i];
1915+
PgStat_SLRUStats stat;
19161916
const char *name;
19171917

19181918
name = pgstat_slru_name(i);
19191919

19201920
if (!name)
19211921
break;
19221922

1923+
stat = stats[i];
19231924
MemSet(values, 0, sizeof(values));
19241925
MemSet(nulls, 0, sizeof(nulls));
19251926

src/test/regress/expected/sysviews.out

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

79+
-- There will surely be at least one SLRU cache
80+
select count(*) > 0 as ok from pg_stat_slru;
81+
ok
82+
----
83+
t
84+
(1 row)
85+
7986
-- There must be only one record
8087
select count(*) = 1 as ok from pg_stat_wal;
8188
ok

src/test/regress/sql/sysviews.sql

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

40+
-- There will surely be at least one SLRU cache
41+
select count(*) > 0 as ok from pg_stat_slru;
42+
4043
-- There must be only one record
4144
select count(*) = 1 as ok from pg_stat_wal;
4245

0 commit comments

Comments
 (0)