Skip to content

Commit a45ed97

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 3354746 commit a45ed97

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/backend/utils/adt/pgstatfuncs.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1911,14 +1911,15 @@ pg_stat_get_slru(PG_FUNCTION_ARGS)
19111911
/* for each row */
19121912
Datum values[PG_STAT_GET_SLRU_COLS];
19131913
bool nulls[PG_STAT_GET_SLRU_COLS];
1914-
PgStat_SLRUStats stat = stats[i];
1914+
PgStat_SLRUStats stat;
19151915
const char *name;
19161916

19171917
name = pgstat_slru_name(i);
19181918

19191919
if (!name)
19201920
break;
19211921

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

src/test/regress/expected/sysviews.out

+7
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

+3
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)