pg_rewind: Fix buffer size passed when building WAL file name

Started by Peter Eisentraut3 days ago4 messageshackers

Thread notes

1 note#refactor
hackorum-bot#refactor
hackorum-bot
2 days ago

@global #refactor

#refactor
Jump to latest
#1Peter Eisentraut
peter@eisentraut.org

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.

Attachments:

t259804_1
0001-pg_rewind-Fix-buffer-size-passed-when-building-WAL-f.patchtext/plain; charset=UTF-8; name=0001-pg_rewind-Fix-buffer-size-passed-when-building-WAL-f.patchDownload+2-6
#2Ayush Tiwari
ayushtiwari.slg01@gmail.com
In reply to: Peter Eisentraut (#1)
Re: pg_rewind: Fix buffer size passed when building WAL file name

Hi,

On Wed, 25 Nov 2026 at 22:34, Peter Eisentraut <peter@eisentraut.org> wrote:

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.

+1, patch Lgtm.

(Could not find any other place doing the same offset trick)

Regards,
Ayush

#3Chao Li
li.evan.chao@gmail.com
In reply to: Peter Eisentraut (#1)
Re: pg_rewind: Fix buffer size passed when building WAL file name

On Nov 26, 2026, at 01:04, Peter Eisentraut <peter@eisentraut.org> wrote:

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.
<0001-pg_rewind-Fix-buffer-size-passed-when-building-WAL-f.patch>

I see this patch actually two things:

1. Replacing MAXFNAMELEN with MAXPGPATH. As xlogfname is going to store a path rather than just a file name, the fix is reasonable, though we cannot overflow it today.

2. The current code uses snprintf() + XLogFileName(), while this patch changes it to use XLogFilePath(), which makes the code easier to read and looks like a good cleanup.

So, the patch looks good to me.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/

#4Xuneng Zhou
xunengzhou@gmail.com
In reply to: Chao Li (#3)
Re: pg_rewind: Fix buffer size passed when building WAL file name

Hi,

On Thu, Nov 26, 2026 at 8:38 AM Chao Li <li.evan.chao@gmail.com> wrote:

On Nov 26, 2026, at 01:04, Peter Eisentraut <peter@eisentraut.org> wrote:

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.
<0001-pg_rewind-Fix-buffer-size-passed-when-building-WAL-f.patch>

I see this patch actually two things:

1. Replacing MAXFNAMELEN with MAXPGPATH. As xlogfname is going to store a path rather than just a file name, the fix is reasonable, though we cannot overflow it today.

2. The current code uses snprintf() + XLogFileName(), while this patch changes it to use XLogFilePath(), which makes the code easier to read and looks like a good cleanup.

So, the patch looks good to me.

+1. Additionally, AI checked 103 calls to the eight WAL, timeline,
archive-status, and backup filename/path helpers under src and
contrib, and it found no similar buffer-size mismatches.

--
Regards,
Xuneng Zhou
HighGo Software Co., Ltd.