public inbox for [email protected]  
help / color / mirror / Atom feed
From: Dmitry Nikitin <[email protected]>
To: [email protected]
Subject: [PATCH] Missing Assert in the code
Date: Mon, 25 Nov 2024 10:32:27 +0300
Message-ID: <[email protected]> (raw)

Hello ,

  The patch is pretty trivial.


-- 
Best regards,
 Dmitry                          mailto:[email protected]

Attachments:

  [text/x-patch] 0001-Get-rid-off-the-magic-number.patch (1.1K, ../[email protected]/2-0001-Get-rid-off-the-magic-number.patch)
  download | inline diff:
From 0ff6a234740d7a7d2bb9572271a1dbdfd4f45f39 Mon Sep 17 00:00:00 2001
From: Dmitry Nikitin <[email protected]>
Date: Mon, 25 Nov 2024 10:31:53 +0300
Subject: [PATCH 1/2] Get rid off the "magic number"

---
 src/include/storage/bufpage.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/include/storage/bufpage.h b/src/include/storage/bufpage.h
index 6222d46e53..390d04103e 100644
--- a/src/include/storage/bufpage.h
+++ b/src/include/storage/bufpage.h
@@ -367,7 +367,7 @@ PageGetItem(Page page, ItemId itemId)
  *		of items on the page.
  *
  *		NOTE: if the page is not initialized (pd_lower == 0), we must
- *		return zero to ensure sane behavior.
+ *		return InvalidOffsetNumber to ensure sane behavior.
  */
 static inline OffsetNumber
 PageGetMaxOffsetNumber(Page page)
@@ -375,7 +375,7 @@ PageGetMaxOffsetNumber(Page page)
 	PageHeader	pageheader = (PageHeader) page;
 
 	if (pageheader->pd_lower <= SizeOfPageHeaderData)
-		return 0;
+		return InvalidOffsetNumber;
 	else
 		return (pageheader->pd_lower - SizeOfPageHeaderData) / sizeof(ItemIdData);
 }
-- 
2.39.5



  [text/x-patch] 0002-Add-Assert-to-stop-invalid-values-to-pass-on.patch (998B, ../[email protected]/3-0002-Add-Assert-to-stop-invalid-values-to-pass-on.patch)
  download | inline diff:
From a75592838b2c7a8a164397c74ce1e21feb32ce44 Mon Sep 17 00:00:00 2001
From: Dmitry Nikitin <[email protected]>
Date: Mon, 25 Nov 2024 10:32:40 +0300
Subject: [PATCH 2/2] Add Assert to stop invalid values to pass on

PageGetMaxOffsetNumber() can legitimately return zero
(InvalidOffsetNumber) as an indication of error. However there are no
any checks against that. As a result, for exampe, subsequent
PageGetItemId() will be accessing an array at -1.
---
 src/backend/access/gin/ginentrypage.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/backend/access/gin/ginentrypage.c b/src/backend/access/gin/ginentrypage.c
index 94ef951e14..193119ac22 100644
--- a/src/backend/access/gin/ginentrypage.c
+++ b/src/backend/access/gin/ginentrypage.c
@@ -236,6 +236,8 @@ getRightMostTuple(Page page)
 {
 	OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
 
+	Assert(OffsetNumberIsValid(maxoff));
+
 	return (IndexTuple) PageGetItem(page, PageGetItemId(page, maxoff));
 }
 
-- 
2.39.5



view thread (5+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: [PATCH] Missing Assert in the code
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox