Clear padding of PgStat_HashKey when handling pgstats entries
authorMichael Paquier <michael@paquier.xyz>
Tue, 5 Nov 2024 00:40:58 +0000 (09:40 +0900)
committerMichael Paquier <michael@paquier.xyz>
Tue, 5 Nov 2024 00:40:58 +0000 (09:40 +0900)
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

src/backend/utils/activity/pgstat.c
src/backend/utils/activity/pgstat_shmem.c

index a220cb876d82130f405a57ffb906bdbb69a1262c..1c0b1625e30946d44e4d22952611297b7606c163 100644 (file)
@@ -825,6 +825,9 @@ pgstat_fetch_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
 
    pgstat_prep_snapshot();
 
+   /* clear padding */
+   memset(&key, 0, sizeof(struct PgStat_HashKey));
+
    key.kind = kind;
    key.dboid = dboid;
    key.objoid = objoid;
index 64121cb76cd4fb2188aa926493694cef8541d2e2..7cf88c8ae55d133a0bf5bca27cea97852f567a6f 100644 (file)
@@ -405,11 +405,18 @@ PgStat_EntryRef *
 pgstat_get_entry_ref(PgStat_Kind kind, Oid dboid, Oid objoid, bool create,
                     bool *created_entry)
 {
-   PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
+   PgStat_HashKey key;
    PgStatShared_HashEntry *shhashent;
    PgStatShared_Common *shheader = NULL;
    PgStat_EntryRef *entry_ref;
 
+   /* clear padding */
+   memset(&key, 0, sizeof(struct PgStat_HashKey));
+
+   key.kind = kind;
+   key.dboid = dboid;
+   key.objoid = objoid;
+
    /*
     * passing in created_entry only makes sense if we possibly could create
     * entry.
@@ -880,10 +887,17 @@ pgstat_drop_database_and_contents(Oid dboid)
 bool
 pgstat_drop_entry(PgStat_Kind kind, Oid dboid, Oid objoid)
 {
-   PgStat_HashKey key = {.kind = kind,.dboid = dboid,.objoid = objoid};
+   PgStat_HashKey key;
    PgStatShared_HashEntry *shent;
    bool        freed = true;
 
+   /* clear padding */
+   memset(&key, 0, sizeof(struct PgStat_HashKey));
+
+   key.kind = kind;
+   key.dboid = dboid;
+   key.objoid = objoid;
+
    /* delete local reference */
    if (pgStatEntryRefHash)
    {