Skip to content

Commit a7a73ce

Browse files
committed
Check for relation length overrun soon enough.
We don't allow relations to exceed 2^32-1 blocks, because block numbers are 32 bits and the last possible block number is reserved to mean InvalidBlockNumber. There is a check for this in mdextend, but that's really way too late, because the smgr API requires us to create a buffer for the block-to-be-added, and we do not want to have any buffer with blocknum InvalidBlockNumber. (Such a case can trigger assertions in bufmgr.c, plus I think it might confuse ReadBuffer's logic for data-past-EOF later on.) So put the check into ReadBuffer. Per report from Christoph Berg. It's been like this forever, so back-patch to all supported branches. Discussion: https://postgr.es/m/YTn1iTkUYBZfcODk@msg.credativ.de
1 parent 4665352 commit a7a73ce

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,16 @@ ReadBuffer_common(SMgrRelation smgr, char relpersistence, ForkNumber forkNum,
728728

729729
/* Substitute proper block number if caller asked for P_NEW */
730730
if (isExtend)
731+
{
731732
blockNum = smgrnblocks(smgr, forkNum);
733+
/* Fail if relation is already at maximum possible length */
734+
if (blockNum == P_NEW)
735+
ereport(ERROR,
736+
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
737+
errmsg("cannot extend relation %s beyond %u blocks",
738+
relpath(smgr->smgr_rnode, forkNum),
739+
P_NEW)));
740+
}
732741

733742
if (isLocalBuf)
734743
{

src/backend/storage/smgr/md.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,8 @@ mdextend(SMgrRelation reln, ForkNumber forknum, BlockNumber blocknum,
424424
/*
425425
* If a relation manages to grow to 2^32-1 blocks, refuse to extend it any
426426
* more --- we mustn't create a block whose number actually is
427-
* InvalidBlockNumber.
427+
* InvalidBlockNumber. (Note that this failure should be unreachable
428+
* because of upstream checks in bufmgr.c.)
428429
*/
429430
if (blocknum == InvalidBlockNumber)
430431
ereport(ERROR,

0 commit comments

Comments
 (0)