From f4d7ec11ff9e9cb9e1f7d0df1cb74ae4329a86ff Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 16 Sep 2026 09:49:28 +0200 Subject: [PATCH v4 4/5] Add an assertion and comments for HEADER_SCRATCH_SIZE HEADER_SCRATCH_SIZE made a complicated calculation without any comments, and there was no check that the calculation was correct. Add some comments and assertion. Suggested-by: Heikki Linnakangas Discussion: https://www.postgresql.org/message-id/flat/94a128da-bf3e-46bd-9e2d-609573c484da%40eisentraut.org --- src/backend/access/transam/xloginsert.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/backend/access/transam/xloginsert.c b/src/backend/access/transam/xloginsert.c index 1a3a89665bb..8e6fe04b368 100644 --- a/src/backend/access/transam/xloginsert.c +++ b/src/backend/access/transam/xloginsert.c @@ -113,6 +113,18 @@ static ZSTD_CCtx *zstd_cctx = NULL; #define SizeOfXlogOrigin (sizeof(ReplOriginId) + sizeof(char)) #define SizeOfXLogTransactionId (sizeof(TransactionId) + sizeof(char)) +/* + * Size of the workspace used to hold the record header while constructing a + * record. It is large enough for the header of any WAL record: the + * fixed-size XLogRecord, a block header for every possible block ID, the main + * data header, and one term for each of the "special" block IDs. + * + * Every "special" block ID needs its own term here. If you add one alongside + * XLR_BLOCK_ID_ORIGIN (SizeOfXlogOrigin) and XLR_BLOCK_ID_TOPLEVEL_XID + * (SizeOfXLogTransactionId), you must add a corresponding term below as well. + * + * XLogRecordAssemble() asserts that the assembled header actually fits. + */ #define HEADER_SCRATCH_SIZE \ (SizeOfXLogRecord + \ MaxSizeOfXLogRecordBlockHeader * (XLR_MAX_BLOCK_ID + 1) + \ @@ -970,6 +982,7 @@ XLogRecordAssemble(RmgrId rmid, uint8 info, rdt_datas_last->next = NULL; hdr_rdt.len = (scratch - hdr_scratch); + Assert(hdr_rdt.len <= HEADER_SCRATCH_SIZE); total_len += hdr_rdt.len; /* -- 2.56.0