Skip to content

Commit 5962519

Browse files
committed
TYPEALIGN doesn't work on int64 on 32-bit platforms.
The TYPEALIGN macro, and the related ones like MAXALIGN, don't work with values larger than intptr_t, because TYPEALIGN casts the argument to intptr_t to do the arithmetic. That's not a problem when dealing with pointers or lengths or offsets related to pointers, but the XLogInsert scaling patch added a call to MAXALIGN with an XLogRecPtr argument. To fix, add wider variants of the macros, called TYPEALIGN64 and MAXALIGN64, which are just like the existing variants but work with uint64 instead of intptr_t. Report and patch by David Rowley, analysis by Andres Freund.
1 parent 81fbbfe commit 5962519

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/backend/access/transam/xlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,7 @@ CopyXLogRecordToWAL(int write_len, bool isLogSwitch, XLogRecData *rdata,
14821482
Assert(written == write_len);
14831483

14841484
/* Align the end position, so that the next record starts aligned */
1485-
CurrPos = MAXALIGN(CurrPos);
1485+
CurrPos = MAXALIGN64(CurrPos);
14861486

14871487
/*
14881488
* If this was an xlog-switch, it's not enough to write the switch record,

src/include/c.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,18 @@ typedef NameData *Name;
551551
#define DOUBLEALIGN_DOWN(LEN) TYPEALIGN_DOWN(ALIGNOF_DOUBLE, (LEN))
552552
#define MAXALIGN_DOWN(LEN) TYPEALIGN_DOWN(MAXIMUM_ALIGNOF, (LEN))
553553

554+
/*
555+
* The above macros will not work with types wider than intptr_t, like with
556+
* uint64 on 32-bit platforms. That's not problem for the usual use where a
557+
* pointer or a length is aligned, but for the odd case that you need to
558+
* align something (potentially) wider, use TYPEALIGN64.
559+
*/
560+
#define TYPEALIGN64(ALIGNVAL,LEN) \
561+
(((uint64) (LEN) + ((ALIGNVAL) - 1)) & ~((uint64) ((ALIGNVAL) - 1)))
562+
563+
/* we don't currently need wider versions of the other ALIGN macros */
564+
#define MAXALIGN64(LEN) TYPEALIGN64(MAXIMUM_ALIGNOF, (LEN))
565+
554566
/* ----------------------------------------------------------------
555567
* Section 6: assertions
556568
* ----------------------------------------------------------------

0 commit comments

Comments
 (0)