From 0ab0b53b7f1b8b88969dcf022fc2a18d123c50c6 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@tigerdata.com>
Date: Thu, 20 Aug 2026 15:46:37 +0300
Subject: [PATCH v2] pg_dump: use ArchiveHandle.lookaheadSize instead of a
 hardcoded 512

_discoverArchiveFormat() allocates the lookahead buffer used to identify the
archive format and records its size in AH->lookaheadSize, but then spells out
the size as a literal 512 in the two places that need it, and nothing ever
reads the field. Use the field instead, so that the buffer and its size cannot
drift apart.

This is only cosmetic, no behavior changes.

Author: Aleksander Alekseev <aleksander@tigerdata.com>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Discussion: https://postgr.es/m/CAJ7c6TPqLZOMk3hPrgLQvrOg9BCY4SV6bUC_wx54fyquRkBckw@mail.gmail.com
---
 src/bin/pg_dump/pg_backup_archiver.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index d7da3fc4325..4c3e05e10db 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -2253,7 +2253,7 @@ _discoverArchiveFormat(ArchiveHandle *AH)
 
 	AH->readHeader = 0;
 	AH->lookaheadSize = 512;
-	AH->lookahead = pg_malloc0(512);
+	AH->lookahead = pg_malloc0(AH->lookaheadSize);
 	AH->lookaheadLen = 0;
 	AH->lookaheadPos = 0;
 
@@ -2324,9 +2324,10 @@ _discoverArchiveFormat(ArchiveHandle *AH)
 	{
 		/*
 		 * *Maybe* we have a tar archive format file or a text dump ... So,
-		 * read first 512 byte header...
+		 * fill the lookahead buffer and inspect its header...
 		 */
-		cnt = fread(&AH->lookahead[AH->lookaheadLen], 1, 512 - AH->lookaheadLen, fh);
+		cnt = fread(&AH->lookahead[AH->lookaheadLen], 1,
+					AH->lookaheadSize - AH->lookaheadLen, fh);
 		/* read failure is checked below */
 		AH->lookaheadLen += cnt;
 
@@ -2341,7 +2342,7 @@ _discoverArchiveFormat(ArchiveHandle *AH)
 			pg_fatal("input file appears to be a text format dump. Please use psql.");
 		}
 
-		if (AH->lookaheadLen != 512)
+		if (AH->lookaheadLen != AH->lookaheadSize)
 		{
 			if (feof(fh))
 				pg_fatal("input file does not appear to be a valid archive (too short?)");
-- 
2.43.0

