Fix memory overrun when querying pg_stat_slru
authorMichael Paquier <michael@paquier.xyz>
Fri, 12 Nov 2021 12:49:21 +0000 (21:49 +0900)
committerMichael Paquier <michael@paquier.xyz>
Fri, 12 Nov 2021 12:49:21 +0000 (21:49 +0900)
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

src/backend/utils/adt/pgstatfuncs.c
src/test/regress/expected/sysviews.out
src/test/regress/sql/sysviews.sql

index ff5aedc99cbdd81dc61f74fad13cb0f0e346d963..e64857e540916214519ca9805b52770adfec4a6d 100644 (file)
@@ -1911,7 +1911,7 @@ pg_stat_get_slru(PG_FUNCTION_ARGS)
                /* for each row */
                Datum           values[PG_STAT_GET_SLRU_COLS];
                bool            nulls[PG_STAT_GET_SLRU_COLS];
-               PgStat_SLRUStats stat = stats[i];
+               PgStat_SLRUStats stat;
                const char *name;
 
                name = pgstat_slru_name(i);
@@ -1919,6 +1919,7 @@ pg_stat_get_slru(PG_FUNCTION_ARGS)
                if (!name)
                        break;
 
+               stat = stats[i];
                MemSet(values, 0, sizeof(values));
                MemSet(nulls, 0, sizeof(nulls));
 
index 6e54f3e15e2de3d21a3199e00d60a4f63a37f58c..2088857615a2976d16797a233938d1c34985218b 100644 (file)
@@ -76,6 +76,13 @@ select count(*) >= 0 as ok from pg_prepared_xacts;
  t
 (1 row)
 
+-- There will surely be at least one SLRU cache
+select count(*) > 0 as ok from pg_stat_slru;
+ ok 
+----
+ t
+(1 row)
+
 -- There must be only one record
 select count(*) = 1 as ok from pg_stat_wal;
  ok 
index dc8c9a3ac23f86b663364d4e2af68cb6247eab0a..b24816e3d5a2c38ddad7c68f189e35ca20f83a79 100644 (file)
@@ -37,6 +37,9 @@ select count(*) = 0 as ok from pg_prepared_statements;
 -- See also prepared_xacts.sql
 select count(*) >= 0 as ok from pg_prepared_xacts;
 
+-- There will surely be at least one SLRU cache
+select count(*) > 0 as ok from pg_stat_slru;
+
 -- There must be only one record
 select count(*) = 1 as ok from pg_stat_wal;