Skip to content

Commit 7d85d87

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 0704aed commit 7d85d87

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
@@ -938,6 +938,9 @@ pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
938938

939939
pgstat_prep_snapshot();
940940

941+
/* clear padding */
942+
memset(&key, 0, sizeof(struct PgStat_HashKey));
943+
941944
key.kind = kind;
942945
key.dboid = dboid;
943946
key.objid = objid;

src/backend/utils/activity/pgstat_shmem.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,18 @@ PgStat_EntryRef *
432432
pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, uint64 objid, bool create,
433433
bool *created_entry)
434434
{
435-
PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objid = objid};
435+
PgStat_HashKey key;
436436
PgStatShared_HashEntry *shhashent;
437437
PgStatShared_Common *shheader = NULL;
438438
PgStat_EntryRef *entry_ref;
439439

440+
/* clear padding */
441+
memset(&key, 0, sizeof(struct PgStat_HashKey));
442+
443+
key.kind = kind;
444+
key.dboid = dboid;
445+
key.objid = objid;
446+
440447
/*
441448
* passing in created_entry only makes sense if we possibly could create
442449
* entry.
@@ -908,10 +915,17 @@ pgstat_drop_database_and_contents(Oid dboid)
908915
bool
909916
pgstat_drop_entry(PgStat_Kind kind, Oid dboid, uint64 objid)
910917
{
911-
PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objid = objid};
918+
PgStat_HashKey key;
912919
PgStatShared_HashEntry *shent;
913920
bool freed = true;
914921

922+
/* clear padding */
923+
memset(&key, 0, sizeof(struct PgStat_HashKey));
924+
925+
key.kind = kind;
926+
key.dboid = dboid;
927+
key.objid = objid;
928+
915929
/* delete local reference */
916930
if (pgStatEntryRefHash)
917931
{

0 commit comments

Comments
 (0)