]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Assert no duplicate keys in shm_toc_insert()
authorMelanie Plageman <melanieplageman@gmail.com>
Mon, 6 Apr 2026 22:41:17 +0000 (18:41 -0400)
committerMelanie Plageman <melanieplageman@gmail.com>
Mon, 6 Apr 2026 22:41:47 +0000 (18:41 -0400)
shm_toc_insert() silently accepts duplicate keys. Since shm_toc_lookup()
returns the first matching entry, any later entry with the same key
would be unreachable. Add an assertion to catch this.

Author: Melanie Plageman <melanieplageman@gmail.com>
Discussion: https://postgr.es/m/flat/a177a6dd-240b-455a-8f25-aca0b1c08c6e%40vondra.me

src/backend/storage/ipc/shm_toc.c

index e74a0b97ac0def830c42c40d5c7a93ef35d011fc..2f9fbb0a519dd7e81b9f3fa234145e50e16ed248 100644 (file)
@@ -186,6 +186,13 @@ shm_toc_insert(shm_toc *toc, uint64 key, void *address)
        total_bytes = vtoc->toc_total_bytes;
        allocated_bytes = vtoc->toc_allocated_bytes;
        nentry = vtoc->toc_nentry;
+
+#ifdef USE_ASSERT_CHECKING
+       /* Verify no duplicate keys */
+       for (Size i = 0; i < nentry; i++)
+               Assert(vtoc->toc_entry[i].key != key);
+#endif
+
        toc_bytes = offsetof(shm_toc, toc_entry) + nentry * sizeof(shm_toc_entry)
                + allocated_bytes;