Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 24a1e20

Browse files
committed
Adjust PageGetMaxOffsetNumber to ensure sane behavior on uninitialized
pages, even when the macro's result is stored into an unsigned variable.
1 parent 641c5b5 commit 24a1e20

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/include/storage/bufpage.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/storage/bufpage.h,v 1.57 2003/12/11 21:21:55 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/storage/bufpage.h,v 1.58 2004/06/05 17:42:46 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -283,13 +283,14 @@ typedef PageHeaderData *PageHeader;
283283
* Since offset numbers are 1-based, this is also the number
284284
* of items on the page.
285285
*
286-
* NOTE: to ensure sane behavior if the page is not initialized
287-
* (pd_lower == 0), cast the unsigned values to int before dividing.
288-
* That way we get -1 or so, not a huge positive number...
286+
* NOTE: if the page is not initialized (pd_lower == 0), we must
287+
* return zero to ensure sane behavior. Accept double evaluation
288+
* of the argument so that we can ensure this.
289289
*/
290290
#define PageGetMaxOffsetNumber(page) \
291-
(((int) (((PageHeader) (page))->pd_lower - SizeOfPageHeaderData)) \
292-
/ ((int) sizeof(ItemIdData)))
291+
(((PageHeader) (page))->pd_lower <= SizeOfPageHeaderData ? 0 : \
292+
((((PageHeader) (page))->pd_lower - SizeOfPageHeaderData) \
293+
/ sizeof(ItemIdData)))
293294

294295
#define PageGetLSN(page) \
295296
(((PageHeader) (page))->pd_lsn)

0 commit comments

Comments
 (0)