From ce5e45b744556b77028f9dfc398b330e92f0f4ae Mon Sep 17 00:00:00 2001
From: Mahendra Singh Thalor <mahi6run@gmail.com>
Date: Sun, 15 Mar 2026 22:21:57 +0530
Subject: [PATCH] pg_restore -F/--format, -h/--host, -p/--port  options should
 validate values in all cases

With "pg_restore --format=", we are not giving any error because in code, we are checking
length of arg but pg_dump is troughing an error for the same option.

Ex: before this patch, below command was passing.
/pg_restore  x1 -d postgres -j 10 -C --verbose --format=

after patch:
/pg_restore  x1 -d postgres -j 10 -C --verbose --format=
pg_restore: error: unrecognized archive format ""; please specify "c", "d", or "t"

Note: pg_dump and pg_dumpall are already reporting an error in above case.

same fix is needed for -h/--host, -p/--port options also.
---
 src/bin/pg_dump/pg_restore.c   |  9 +++------
 src/bin/pg_dump/t/001_basic.pl | 10 ++++++++++
 2 files changed, 13 insertions(+), 6 deletions(-)
 mode change 100644 => 100755 src/bin/pg_dump/t/001_basic.pl

diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 9b4b151b318..fa5e1355e71 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -225,16 +225,14 @@ 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 'g':
 				/* restore only global sql commands. */
 				globals_only = true;
 				break;
 			case 'h':
-				if (strlen(optarg) != 0)
-					opts->cparams.pghost = pg_strdup(optarg);
+				opts->cparams.pghost = pg_strdup(optarg);
 				break;
 			case 'j':			/* number of restore jobs */
 				if (!option_parse_int(optarg, "-j/--jobs", 1,
@@ -264,8 +262,7 @@ main(int argc, char **argv)
 				break;
 
 			case 'p':
-				if (strlen(optarg) != 0)
-					opts->cparams.pgport = pg_strdup(optarg);
+				opts->cparams.pgport = pg_strdup(optarg);
 				break;
 			case 'R':
 				/* no-op, still accepted for backwards compatibility */
diff --git a/src/bin/pg_dump/t/001_basic.pl b/src/bin/pg_dump/t/001_basic.pl
old mode 100644
new mode 100755
index 2f5eb48e7b8..3914fb158c2
--- a/src/bin/pg_dump/t/001_basic.pl
+++ b/src/bin/pg_dump/t/001_basic.pl
@@ -201,6 +201,16 @@ command_fails_like(
 	qr/\Qpg_restore: error: unrecognized archive format "garbage";\E/,
 	'pg_dump: unrecognized archive format');
 
+command_fails_like(
+	[ 'pg_restore', '-f -', '--format='],
+	qr/\Qpg_restore: error: unrecognized archive format "";\E/,
+	'pg_restore: unrecognized archive format empty string');
+
+command_fails_like(
+	[ 'pg_restore', '-f -', '-F', 'p' ],
+	qr/\Qpg_restore: error: archive format "p" is not supported; please use psql\E/,
+	'pg_restore: unrecognized archive format p|plain');
+
 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.52.0

