1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-25 13:17:41 +03:00

Fix off-by-one Asserts in FreePageBtreeInsertInternal/Leaf.

These two functions expect there to be room to insert another item
in the FreePageBtree's array, but their assertions were too weak
to guarantee that.  This has little practical effect granting that
the callers are not buggy, but it seems to be misleading late-model
Coverity into complaining about possible array overrun.

Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/799984.1761150474@sss.pgh.pa.us
Backpatch-through: 13
This commit is contained in:
Tom Lane
2025-10-23 12:32:06 -04:00
parent 7fb8c92404
commit 7b542cdbcd

View File

@@ -894,14 +894,14 @@ FreePageBtreeGetRecycled(FreePageManager *fpm)
} }
/* /*
* Insert an item into an internal page. * Insert an item into an internal page (there must be room).
*/ */
static void static void
FreePageBtreeInsertInternal(char *base, FreePageBtree *btp, Size index, FreePageBtreeInsertInternal(char *base, FreePageBtree *btp, Size index,
Size first_page, FreePageBtree *child) Size first_page, FreePageBtree *child)
{ {
Assert(btp->hdr.magic == FREE_PAGE_INTERNAL_MAGIC); Assert(btp->hdr.magic == FREE_PAGE_INTERNAL_MAGIC);
Assert(btp->hdr.nused <= FPM_ITEMS_PER_INTERNAL_PAGE); Assert(btp->hdr.nused < FPM_ITEMS_PER_INTERNAL_PAGE);
Assert(index <= btp->hdr.nused); Assert(index <= btp->hdr.nused);
memmove(&btp->u.internal_key[index + 1], &btp->u.internal_key[index], memmove(&btp->u.internal_key[index + 1], &btp->u.internal_key[index],
sizeof(FreePageBtreeInternalKey) * (btp->hdr.nused - index)); sizeof(FreePageBtreeInternalKey) * (btp->hdr.nused - index));
@@ -911,14 +911,14 @@ FreePageBtreeInsertInternal(char *base, FreePageBtree *btp, Size index,
} }
/* /*
* Insert an item into a leaf page. * Insert an item into a leaf page (there must be room).
*/ */
static void static void
FreePageBtreeInsertLeaf(FreePageBtree *btp, Size index, Size first_page, FreePageBtreeInsertLeaf(FreePageBtree *btp, Size index, Size first_page,
Size npages) Size npages)
{ {
Assert(btp->hdr.magic == FREE_PAGE_LEAF_MAGIC); Assert(btp->hdr.magic == FREE_PAGE_LEAF_MAGIC);
Assert(btp->hdr.nused <= FPM_ITEMS_PER_LEAF_PAGE); Assert(btp->hdr.nused < FPM_ITEMS_PER_LEAF_PAGE);
Assert(index <= btp->hdr.nused); Assert(index <= btp->hdr.nused);
memmove(&btp->u.leaf_key[index + 1], &btp->u.leaf_key[index], memmove(&btp->u.leaf_key[index + 1], &btp->u.leaf_key[index],
sizeof(FreePageBtreeLeafKey) * (btp->hdr.nused - index)); sizeof(FreePageBtreeLeafKey) * (btp->hdr.nused - index));