Skip to content

Commit 68d9662

Browse files
Make rs_cindex and rs_ntuples unsigned
HeapScanDescData.rs_cindex and rs_ntuples can't be less than 0. All scan types using the heap scan descriptor expect these values to be >= 0. Make that expectation clear by making rs_cindex and rs_ntuples unsigned. Also remove the test in heapam_scan_bitmap_next_tuple() that checks if rs_cindex < 0. This was never true, but now that rs_cindex is unsigned, it makes even less sense. While we are at it, initialize both rs_cindex and rs_ntuples to 0 in initscan(). Author: Melanie Plageman Reviewed-by: Dilip Kumar Discussion: https://postgr.es/m/CAAKRu_ZxF8cDCM_BFi_L-t%3DRjdCZYP1usd1Gd45mjHfZxm0nZw%40mail.gmail.com
1 parent 1f0de66 commit 68d9662

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/backend/access/heap/heapam.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ initscan(HeapScanDesc scan, ScanKey key, bool keep_startblock)
378378
ItemPointerSetInvalid(&scan->rs_ctup.t_self);
379379
scan->rs_cbuf = InvalidBuffer;
380380
scan->rs_cblock = InvalidBlockNumber;
381+
scan->rs_ntuples = 0;
382+
scan->rs_cindex = 0;
381383

382384
/*
383385
* Initialize to ForwardScanDirection because it is most common and
@@ -943,8 +945,8 @@ heapgettup_pagemode(HeapScanDesc scan,
943945
{
944946
HeapTuple tuple = &(scan->rs_ctup);
945947
Page page;
946-
int lineindex;
947-
int linesleft;
948+
uint32 lineindex;
949+
uint32 linesleft;
948950

949951
if (likely(scan->rs_inited))
950952
{
@@ -989,6 +991,7 @@ heapgettup_pagemode(HeapScanDesc scan,
989991
ItemId lpp;
990992
OffsetNumber lineoff;
991993

994+
Assert(lineindex <= scan->rs_ntuples);
992995
lineoff = scan->rs_vistuples[lineindex];
993996
lpp = PageGetItemId(page, lineoff);
994997
Assert(ItemIdIsNormal(lpp));

src/backend/access/heap/heapam_handler.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2303,7 +2303,7 @@ heapam_scan_bitmap_next_tuple(TableScanDesc scan,
23032303
/*
23042304
* Out of range? If so, nothing more to look at on this page
23052305
*/
2306-
if (hscan->rs_cindex < 0 || hscan->rs_cindex >= hscan->rs_ntuples)
2306+
if (hscan->rs_cindex >= hscan->rs_ntuples)
23072307
return false;
23082308

23092309
targoffset = hscan->rs_vistuples[hscan->rs_cindex];

src/include/access/heapam.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ typedef struct HeapScanDescData
103103
int rs_empty_tuples_pending;
104104

105105
/* these fields only used in page-at-a-time mode and for bitmap scans */
106-
int rs_cindex; /* current tuple's index in vistuples */
107-
int rs_ntuples; /* number of visible tuples on page */
106+
uint32 rs_cindex; /* current tuple's index in vistuples */
107+
uint32 rs_ntuples; /* number of visible tuples on page */
108108
OffsetNumber rs_vistuples[MaxHeapTuplesPerPage]; /* their offsets */
109109
} HeapScanDescData;
110110
typedef struct HeapScanDescData *HeapScanDesc;

0 commit comments

Comments
 (0)