From 9ad73adf67f29388a8f001fd4c11125fa3fa0d6a Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Fri, 20 Nov 2026 15:33:32 +0000
Subject: [PATCH v1] pg_restore: reject an empty -F/--format argument

pg_restore stored the -F value only when it was not empty, so -F ''
was treated as if -F had not been given and pg_restore went on to
detect the archive format.  Any other unknown value is rejected, and
pg_dump rejects an empty -F too.  Store the value always, so an empty
one is reported as an unrecognized archive format.

Discussion: https://postgr.es/m/19891-0b151c9cd5969ede@postgresql.org
---
 src/bin/pg_dump/pg_restore.c   | 3 +--
 src/bin/pg_dump/t/001_basic.pl | 5 +++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 84b8d410c9..425f1fcc9f 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -194,8 +194,7 @@ main(int argc, char **argv)
 				opts->filename = pg_strdup(optarg);
 				break;
 			case 'F':
-				if (strlen(optarg) != 0)
-					opts->formatName = pg_strdup(optarg);
+				opts->formatName = pg_strdup(optarg);
 				break;
 			case 'h':
 				if (strlen(optarg) != 0)
diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl
index b255804622..3a98727489 100644
--- a/src/bin/pg_dump/t/001_basic.pl
+++ b/src/bin/pg_dump/t/001_basic.pl
@@ -207,6 +207,11 @@ command_fails_like(
 	qr/\Qpg_restore: error: unrecognized archive format "garbage";\E/,
 	'pg_dump: unrecognized archive format');
 
+command_fails_like(
+	[ 'pg_restore', '-f -', '-F', '' ],
+	qr/\Qpg_restore: error: unrecognized archive format "";\E/,
+	'pg_restore: empty archive format');
+
 command_fails_like(
 	[ 'pg_dump', '--on-conflict-do-nothing' ],
 	qr/pg_dump: error: option --on-conflict-do-nothing requires option --inserts, --rows-per-insert, or --column-inserts/,
-- 
2.50.1 (Apple Git-155)

