Improve assertion in mdwritev()
authorMichael Paquier <[email protected]>
Mon, 3 Jun 2024 22:15:10 +0000 (07:15 +0900)
committerMichael Paquier <[email protected]>
Mon, 3 Jun 2024 22:15:10 +0000 (07:15 +0900)
The assertion used at the beginning of mdwritev(), that is not enabled
except by defining -DCHECK_WRITE_VS_EXTEND as mdnblocks() is costly,
forgot about the total number of blocks to write at location specified
by the caller.  The calculation is fixed to count for that, and uses
casts to uint64 to ensure a proper check should the number of blocks
overflow.

Using a cast is a suggestion from Tom Lane.

Oversight in 4908c5872059.

Author: Xing Guo
Discussion: https://postgr.es/m/CACpMh+BM-VgKeO7suPG-VHTtpzJ+zsbDPwVHu42PLp-iTk0z+A@mail.gmail.com

src/backend/storage/smgr/md.c

index bf0f3ca76d1b321eb9f92c086771361abbaf0122..6796756358f348685feb3e9475d11fcd207baf05 100644 (file)
@@ -930,7 +930,7 @@ mdwritev(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
 {
    /* This assert is too expensive to have on normally ... */
 #ifdef CHECK_WRITE_VS_EXTEND
-   Assert(blocknum < mdnblocks(reln, forknum));
+   Assert((uint64) blocknum + (uint64) nblocks <= (uint64) mdnblocks(reln, forknum));
 #endif
 
    while (nblocks > 0)