From 28d3ae6bc469f237f6ad4d31f06e8caf81111089 Mon Sep 17 00:00:00 2001
From: Aleksander Alekseev <aleksander@tigerdata.com>
Date: Tue, 18 Aug 2026 17:55:45 +0300
Subject: [PATCH v1 2/2] pg_dump: use TAR_BLOCK_SIZE instead of a hardcoded 512

_discoverArchiveFormat() reads the start of the input file into a lookahead
buffer and, unless the file turns out to be a custom-format archive or a text
dump, hands that buffer to isValidTarHeader(). The latter examines exactly
TAR_BLOCK_SIZE bytes, since tarChecksum() sums the whole tar block, so the
buffer size, the amount read and the completeness check all have to be one tar
block. Spell that out rather than repeating the literal 512 three times.

The rest of the tar code already does it this way, and so does the other
caller of isValidTarHeader() in astreamer_tar.c, which even asserts that its
buffer is TAR_BLOCK_SIZE long. pgtar.h was already included here.

This is only cosmetic, no behavior changes.

Author: Aleksander Alekseev <aleksander@tigerdata.com>
Reviewed-by: TODO FIXME
Discussion: TODO FIXME
---
 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 c2aa7e5669e..88f6990c38f 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -2252,7 +2252,7 @@ _discoverArchiveFormat(ArchiveHandle *AH)
 	pg_free(AH->lookahead);
 
 	AH->readHeader = 0;
-	AH->lookahead = pg_malloc0(512);
+	AH->lookahead = pg_malloc0(TAR_BLOCK_SIZE);
 	AH->lookaheadLen = 0;
 	AH->lookaheadPos = 0;
 
@@ -2323,9 +2323,10 @@ _discoverArchiveFormat(ArchiveHandle *AH)
 	{
 		/*
 		 * *Maybe* we have a tar archive format file or a text dump ... So,
-		 * read first 512 byte header...
+		 * read first tar block, which is what isValidTarHeader() inspects.
 		 */
-		cnt = fread(&AH->lookahead[AH->lookaheadLen], 1, 512 - AH->lookaheadLen, fh);
+		cnt = fread(&AH->lookahead[AH->lookaheadLen], 1,
+					TAR_BLOCK_SIZE - AH->lookaheadLen, fh);
 		/* read failure is checked below */
 		AH->lookaheadLen += cnt;
 
@@ -2340,7 +2341,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 != TAR_BLOCK_SIZE)
 		{
 			if (feof(fh))
 				pg_fatal("input file does not appear to be a valid archive (too short?)");
-- 
2.43.0

