From 93624bcda0dbd1befaba81299831b5f0e64ecdd6 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sun, 15 Jul 2007 23:46:20 +0000
Subject: [PATCH] Fix CHECK_RELATION_BLOCK_RANGE macro, which was not merely
 producing a warning but was outright wrong.

---
 contrib/pageinspect/btreefuncs.c  | 7 ++++---
 contrib/pgstattuple/pgstatindex.c | 7 ++++---
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/contrib/pageinspect/btreefuncs.c b/contrib/pageinspect/btreefuncs.c
index 97c5ae409a4..f02c3821834 100644
--- a/contrib/pageinspect/btreefuncs.c
+++ b/contrib/pageinspect/btreefuncs.c
@@ -57,12 +57,13 @@ extern Datum bt_page_stats(PG_FUNCTION_ARGS);
 #define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
 
 #define CHECK_PAGE_OFFSET_RANGE(pg, offnum) { \
-		if ( !(FirstOffsetNumber<=(offnum) && \
-						(offnum)<=PageGetMaxOffsetNumber(pg)) ) \
+		if ( !(FirstOffsetNumber <= (offnum) && \
+						(offnum) <= PageGetMaxOffsetNumber(pg)) ) \
 			 elog(ERROR, "page offset number out of range"); }
 
+/* note: BlockNumber is unsigned, hence can't be negative */
 #define CHECK_RELATION_BLOCK_RANGE(rel, blkno) { \
-		if ( (blkno)<0 && RelationGetNumberOfBlocks((rel))<=(blkno) ) \
+		if ( RelationGetNumberOfBlocks(rel) <= (BlockNumber) (blkno) ) \
 			 elog(ERROR, "block number out of range"); }
 
 /* ------------------------------------------------
diff --git a/contrib/pgstattuple/pgstatindex.c b/contrib/pgstattuple/pgstatindex.c
index 6a64d3992a9..0c2c9a14484 100644
--- a/contrib/pgstattuple/pgstatindex.c
+++ b/contrib/pgstattuple/pgstatindex.c
@@ -48,12 +48,13 @@ extern Datum pg_relpages(PG_FUNCTION_ARGS);
 #define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
 
 #define CHECK_PAGE_OFFSET_RANGE(pg, offnum) { \
-		if ( !(FirstOffsetNumber<=(offnum) && \
-						(offnum)<=PageGetMaxOffsetNumber(pg)) ) \
+		if ( !(FirstOffsetNumber <= (offnum) && \
+						(offnum) <= PageGetMaxOffsetNumber(pg)) ) \
 			 elog(ERROR, "page offset number out of range"); }
 
+/* note: BlockNumber is unsigned, hence can't be negative */
 #define CHECK_RELATION_BLOCK_RANGE(rel, blkno) { \
-		if ( (blkno)<0 && RelationGetNumberOfBlocks((rel))<=(blkno) ) \
+		if ( RelationGetNumberOfBlocks(rel) <= (BlockNumber) (blkno) ) \
 			 elog(ERROR, "block number out of range"); }
 
 /* ------------------------------------------------
-- 
GitLab