Skip to content

Commit e507a3e

Browse files
committed
Fix pg_restore to complain if any arguments remain after parsing the switches
and input file name, per bug #5617 from Leo Shklovskii. Rearrange the corresponding code in pg_dump and pg_dumpall so that all three programs handle this in a consistent, straightforward fashion. Back-patch to 9.0, but no further. Although this is certainly a bug, it's possible that people have scripts that will be broken by the added error check, so it seems better not to change the behavior in stable branches.
1 parent 5be77ca commit e507a3e

File tree

3 files changed

+39
-26
lines changed

3 files changed

+39
-26
lines changed

src/bin/pg_dump/pg_dump.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* http://archives.postgresql.org/pgsql-bugs/2010-02/msg00187.php
2626
*
2727
* IDENTIFICATION
28-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.581.2.1 2010/07/14 21:21:16 tgl Exp $
28+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.581.2.2 2010/08/13 14:38:12 tgl Exp $
2929
*
3030
*-------------------------------------------------------------------------
3131
*/
@@ -479,19 +479,20 @@ main(int argc, char **argv)
479479
}
480480
}
481481

482-
if (optind < (argc - 1))
482+
/* Get database name from command line */
483+
if (optind < argc)
484+
dbname = argv[optind++];
485+
486+
/* Complain if any arguments remain */
487+
if (optind < argc)
483488
{
484489
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
485-
progname, argv[optind + 1]);
490+
progname, argv[optind]);
486491
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
487492
progname);
488493
exit(1);
489494
}
490495

491-
/* Get database name from command line */
492-
if (optind < argc)
493-
dbname = argv[optind];
494-
495496
/* --column-inserts implies --inserts */
496497
if (column_inserts)
497498
dump_inserts = 1;

src/bin/pg_dump/pg_dumpall.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1994, Regents of the University of California
77
*
88
*
9-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.134 2010/02/26 02:01:17 momjian Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.134.4.1 2010/08/13 14:38:12 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -315,22 +315,7 @@ main(int argc, char *argv[])
315315
}
316316
}
317317

318-
/* Add long options to the pg_dump argument list */
319-
if (binary_upgrade)
320-
appendPQExpBuffer(pgdumpopts, " --binary-upgrade");
321-
if (column_inserts)
322-
appendPQExpBuffer(pgdumpopts, " --column-inserts");
323-
if (disable_dollar_quoting)
324-
appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
325-
if (disable_triggers)
326-
appendPQExpBuffer(pgdumpopts, " --disable-triggers");
327-
if (inserts)
328-
appendPQExpBuffer(pgdumpopts, " --inserts");
329-
if (no_tablespaces)
330-
appendPQExpBuffer(pgdumpopts, " --no-tablespaces");
331-
if (use_setsessauth)
332-
appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization");
333-
318+
/* Complain if any arguments remain */
334319
if (optind < argc)
335320
{
336321
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
@@ -368,6 +353,22 @@ main(int argc, char *argv[])
368353
exit(1);
369354
}
370355

356+
/* Add long options to the pg_dump argument list */
357+
if (binary_upgrade)
358+
appendPQExpBuffer(pgdumpopts, " --binary-upgrade");
359+
if (column_inserts)
360+
appendPQExpBuffer(pgdumpopts, " --column-inserts");
361+
if (disable_dollar_quoting)
362+
appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
363+
if (disable_triggers)
364+
appendPQExpBuffer(pgdumpopts, " --disable-triggers");
365+
if (inserts)
366+
appendPQExpBuffer(pgdumpopts, " --inserts");
367+
if (no_tablespaces)
368+
appendPQExpBuffer(pgdumpopts, " --no-tablespaces");
369+
if (use_setsessauth)
370+
appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization");
371+
371372
/*
372373
* If there was a database specified on the command line, use that,
373374
* otherwise try to connect to database "postgres", and failing that

src/bin/pg_dump/pg_restore.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
*
3636
* IDENTIFICATION
37-
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.102 2010/05/15 21:41:16 tgl Exp $
37+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_restore.c,v 1.102.2.1 2010/08/13 14:38:12 tgl Exp $
3838
*
3939
*-------------------------------------------------------------------------
4040
*/
@@ -295,11 +295,22 @@ main(int argc, char **argv)
295295
}
296296
}
297297

298+
/* Get file name from command line */
298299
if (optind < argc)
299-
inputFileSpec = argv[optind];
300+
inputFileSpec = argv[optind++];
300301
else
301302
inputFileSpec = NULL;
302303

304+
/* Complain if any arguments remain */
305+
if (optind < argc)
306+
{
307+
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
308+
progname, argv[optind]);
309+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
310+
progname);
311+
exit(1);
312+
}
313+
303314
/* Should get at most one of -d and -f, else user is confused */
304315
if (opts->dbname)
305316
{

0 commit comments

Comments
 (0)