Fix incorrect assertion in heapgettup_pagemode()

Started by cca550718 days ago3 messages
#1cca5507
cca5507@qq.com
1 attachment(s)

Hi,

diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 469397e7344..1229e22e78a 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -1077,7 +1077,7 @@ continue_page:
                        ItemId          lpp;
                        OffsetNumber lineoff;
-                       Assert(lineindex <= scan->rs_ntuples);
+                       Assert(lineindex < scan->rs_ntuples);
                        lineoff = scan->rs_vistuples[lineindex];
                        lpp = PageGetItemId(page, lineoff);
                        Assert(ItemIdIsNormal(lpp));

The lineindex is 0-based, so it should be '<' rather than '<=', thoughts?

--
Regards,
ChangAo Chen

Attachments:

v1-0001-Fix-incorrect-assertion-in-heapgettup_pagemode.patchapplication/octet-stream; charset=utf-8; name=v1-0001-Fix-incorrect-assertion-in-heapgettup_pagemode.patchDownload
From 49851e8a2d003873212f693fe4456a878a238e49 Mon Sep 17 00:00:00 2001
From: ChangAo Chen <cca5507@qq.com>
Date: Thu, 25 Dec 2025 19:06:26 +0800
Subject: [PATCH v1] Fix incorrect assertion in heapgettup_pagemode().

---
 src/backend/access/heap/heapam.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 469397e7344..1229e22e78a 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -1077,7 +1077,7 @@ continue_page:
 			ItemId		lpp;
 			OffsetNumber lineoff;
 
-			Assert(lineindex <= scan->rs_ntuples);
+			Assert(lineindex < scan->rs_ntuples);
 			lineoff = scan->rs_vistuples[lineindex];
 			lpp = PageGetItemId(page, lineoff);
 			Assert(ItemIdIsNormal(lpp));
-- 
2.34.1

#2Chao Li
li.evan.chao@gmail.com
In reply to: cca5507 (#1)
Re: Fix incorrect assertion in heapgettup_pagemode()

On Dec 25, 2025, at 19:13, cca5507 <cca5507@qq.com> wrote:

Hi,

diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 469397e7344..1229e22e78a 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -1077,7 +1077,7 @@ continue_page:
ItemId          lpp;
OffsetNumber lineoff;
-                       Assert(lineindex <= scan->rs_ntuples);
+                       Assert(lineindex < scan->rs_ntuples);
lineoff = scan->rs_vistuples[lineindex];
lpp = PageGetItemId(page, lineoff);
Assert(ItemIdIsNormal(lpp));

The lineindex is 0-based, so it should be '<' rather than '<=', thoughts?

--
Regards,
ChangAo Chen
<v1-0001-Fix-incorrect-assertion-in-heapgettup_pagemode.patch>

Good catch. The function comment clearly mentions that lineindex is 0 based.
```
* tuples listed in rs_vistuples[] rather than all tuples on the page. Notice
* that lineindex is 0-based, where the corresponding loop variable lineoff in
* heapgettup is 1-based.
* ----------------
*/
static void
heapgettup_pagemode(HeapScanDesc scan,
ScanDirection dir,
int nkeys,
ScanKey key)
{
HeapTuple tuple = &(scan->rs_ctup);
Page page;
uint32 lineindex;
uint32 linesleft;
```

So, the fix looks correct.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#3Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Chao Li (#2)
Re: Fix incorrect assertion in heapgettup_pagemode()

On 25/12/2025 15:34, Chao Li wrote:

On Dec 25, 2025, at 19:13, cca5507 <cca5507@qq.com> wrote:
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index 469397e7344..1229e22e78a 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -1077,7 +1077,7 @@ continue_page:
ItemId          lpp;
OffsetNumber lineoff;
-                       Assert(lineindex <= scan->rs_ntuples);
+                       Assert(lineindex < scan->rs_ntuples);
lineoff = scan->rs_vistuples[lineindex];
lpp = PageGetItemId(page, lineoff);
Assert(ItemIdIsNormal(lpp));

The lineindex is 0-based, so it should be '<' rather than '<=', thoughts?

--
Regards,
ChangAo Chen
<v1-0001-Fix-incorrect-assertion-in-heapgettup_pagemode.patch>

Good catch. The function comment clearly mentions that lineindex is 0 based.
...
So, the fix looks correct.

Committed, thanks!

- Heikki