In pg_rewind findLastCheckpoint() there is a buffer size mismatch when
composing a WAL file name. You can't actually overflow the buffer, so
it's harmless, but when playing with compiler annotations, I see this
getting flagged, because it's rather obviously incorrect on some level,
so I think it's worth fixing. See attached patch for details.
From dcab526b02c9b2f62d24e456a6f35461ce990258 Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <[email protected]>
Date: Wed, 25 Nov 2026 17:52:07 +0100
Subject: [PATCH] pg_rewind: Fix buffer size passed when building WAL file name
findLastCheckpoint() wrote XLOGDIR "/" into a buffer of size
MAXFNAMELEN and then called XLogFileName() on the buffer offset by
sizeof(XLOGDIR). XLogFileName() itself passes MAXFNAMELEN to
snprintf() as the buffer size, which overstates the remaining space by
sizeof(XLOGDIR) bytes. This could not actually overflow, since the
produced file name is much shorter than the buffer, but it is
incorrect and could get flagged by various higher-level compiler
warnings or annotations. To fix, use XLogFilePath(), which produces
the whole path in one call and with the correct buffer size.
---
src/bin/pg_rewind/parsexlog.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/src/bin/pg_rewind/parsexlog.c b/src/bin/pg_rewind/parsexlog.c
index 6e87b00f8c2..a8a5b31dc1e 100644
--- a/src/bin/pg_rewind/parsexlog.c
+++ b/src/bin/pg_rewind/parsexlog.c
@@ -224,16 +224,13 @@ findLastCheckpoint(const char *datadir, XLogRecPtr
forkptr, int tliIndex,
if (xlogreader->seg.ws_tli != current_tli ||
xlogreader->seg.ws_segno != current_segno)
{
- char xlogfname[MAXFNAMELEN];
-
- snprintf(xlogfname, MAXFNAMELEN, XLOGDIR "/");
+ char xlogfname[MAXPGPATH];
/* update current values */
current_tli = xlogreader->seg.ws_tli;
current_segno = xlogreader->seg.ws_segno;
- XLogFileName(xlogfname + sizeof(XLOGDIR),
- current_tli, current_segno,
WalSegSz);
+ XLogFilePath(xlogfname, current_tli, current_segno,
WalSegSz);
/* Track this filename as one to not remove */
keepwal_add_entry(xlogfname);
--
2.56.0