PATCH for BUG #18785: Pointer bmr.rel, dereferenced by passing as 1st parameter to function is checked for NULL later

From: Алена Васильева <gorcom2012(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org, pgsql-bugs(at)postgresql(dot)org
Subject: PATCH for BUG #18785: Pointer bmr.rel, dereferenced by passing as 1st parameter to function is checked for NULL later
Date: 2025-09-18 08:44:57
Message-ID: CABg3sZqO_TVXUcH4NvOqeRFYFb+QzEdQDpKiO_zp=AsJi2QJPw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

Hello!
I propose adding an Assert at this point as a way to document the contract
between this code and its environment.
This was previously discussed in BUG #18785 in pqsql-bugs
https://www.postgresql.org/message-id/flat/a6oxxee6blexicuark46yydtaqulsjvkrwkri6aic4vbofjxse%404a6j4kuwda7u#c52de413b182d9c42fe1eb34a82871b5

Best regards,
Eugeny Goryachev

Patch for PostgreSQL 16.6:
---
src/backend/storage/buffer/bufmgr.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/src/backend/storage/buffer/bufmgr.c
b/src/backend/storage/buffer/bufmgr.c
index e066a3f888f..1d3b31f5070 100644
--- a/src/backend/storage/buffer/bufmgr.c
+++ b/src/backend/storage/buffer/bufmgr.c
@@ -895,6 +895,17 @@ ExtendBufferedRelTo(BufferManagerRelation bmr,
bmr.relpersistence = bmr.rel->rd_rel->relpersistence;
}

+ /*
+ * Validate the contract between flags and bmr.rel.
+ *
+ * EB_CREATE_FORK_IF_NEEDED implies we need to take extension lock,
+ * which requires a valid relation. Conversely, if we're skipping
+ * the extension lock (e.g., in recovery), then we must not be creating
+ * a fork here.
+ */
+ Assert(!(flags & EB_CREATE_FORK_IF_NEEDED) || !(flags &
EB_SKIP_EXTENSION_LOCK));
+ Assert(bmr.rel != NULL || (flags & EB_SKIP_EXTENSION_LOCK));
+
/*
* If desired, create the file if it doesn't exist. If
* smgr_cached_nblocks[fork] is positive then it must exist, no need for
--
2.42.4

Browse pgsql-bugs by date

  From Date Subject
Next Message Ashutosh Bapat 2025-09-18 12:07:10 Re: "unexpected duplicate for tablespace" problem in logical replication
Previous Message Amit Langote 2025-09-18 08:25:28 Re: BUG #19056: ExecInitPartitionExecPruning segfault due to NULL es_part_prune_infos

Browse pgsql-hackers by date

  From Date Subject
Next Message John Naylor 2025-09-18 08:53:08 Re: GB18030-2022 Support in PostgreSQL
Previous Message Japin Li 2025-09-18 08:38:50 Re: [WIP]Vertical Clustered Index (columnar store extension) - take2