Skip to content

Commit 7f3b41c

Browse files
committed
Clear padding of PgStat_HashKey when handling pgstats entries
PgStat_HashKey is currently initialized in a way that could result in random data if the structure has any padding bytes. The structure has no padding bytes currently, fortunately, but it could become a problem should the structure change at some point in the future. The code is changed to use some memset(0) so as any padding would be handled properly, as it would be surprising to see random failures in the pgstats entry lookups. PgStat_HashKey is a structure internal to pgstats, and an ABI change could be possible in the scope of a bug fix, so backpatch down to 15 where this has been introduced. Author: Bertrand Drouvot Reviewed-by: Jelte Fennema-Nio, Michael Paquier Discussion: https://postgr.es/m/Zyb7RW1y9dVfO0UH@ip-10-97-1-34.eu-west-3.compute.internal Backpatch-through: 15
1 parent 811f8d3 commit 7f3b41c

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/backend/utils/activity/pgstat.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,9 @@ pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
822822

823823
pgstat_prep_snapshot();
824824

825+
/* clear padding */
826+
memset(&key, 0, sizeof(struct PgStat_HashKey));
827+
825828
key.kind = kind;
826829
key.dboid = dboid;
827830
key.objoid = objoid;

src/backend/utils/activity/pgstat_shmem.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,18 @@ PgStat_EntryRef *
406406
pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create,
407407
bool *created_entry)
408408
{
409-
PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
409+
PgStat_HashKey key;
410410
PgStatShared_HashEntry *shhashent;
411411
PgStatShared_Common *shheader = NULL;
412412
PgStat_EntryRef *entry_ref;
413413

414+
/* clear padding */
415+
memset(&key, 0, sizeof(struct PgStat_HashKey));
416+
417+
key.kind = kind;
418+
key.dboid = dboid;
419+
key.objoid = objoid;
420+
414421
/*
415422
* passing in created_entry only makes sense if we possibly could create
416423
* entry.
@@ -881,10 +888,17 @@ pgstat_drop_database_and_contents(Oid dboid)
881888
bool
882889
pgstat_drop_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
883890
{
884-
PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
891+
PgStat_HashKey key;
885892
PgStatShared_HashEntry *shent;
886893
bool freed = true;
887894

895+
/* clear padding */
896+
memset(&key, 0, sizeof(struct PgStat_HashKey));
897+
898+
key.kind = kind;
899+
key.dboid = dboid;
900+
key.objoid = objoid;
901+
888902
/* delete local reference */
889903
if (pgStatEntryRefHash)
890904
{

0 commit comments

Comments
 (0)