Skip to content

Commit e4155c8

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 9b0a868 commit e4155c8

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

src/bin/pg_dump/pg_dump.c

+8-7
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.584 2010/08/03 19:24:04 tgl Exp $
28+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dump.c,v 1.585 2010/08/13 14:38:03 tgl Exp $
2929
*
3030
*-------------------------------------------------------------------------
3131
*/
@@ -480,19 +480,20 @@ main(int argc, char **argv)
480480
}
481481
}
482482

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

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

src/bin/pg_dump/pg_dumpall.c

+20-19
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.137 2010/08/03 19:24:05 tgl Exp $
9+
* $PostgreSQL: pgsql/src/bin/pg_dump/pg_dumpall.c,v 1.138 2010/08/13 14:38:04 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -316,24 +316,7 @@ main(int argc, char *argv[])
316316
}
317317
}
318318

319-
/* Add long options to the pg_dump argument list */
320-
if (binary_upgrade)
321-
appendPQExpBuffer(pgdumpopts, " --binary-upgrade");
322-
if (column_inserts)
323-
appendPQExpBuffer(pgdumpopts, " --column-inserts");
324-
if (disable_dollar_quoting)
325-
appendPQExpBuffer(pgdumpopts, " --disable-dollar-quoting");
326-
if (disable_triggers)
327-
appendPQExpBuffer(pgdumpopts, " --disable-triggers");
328-
if (inserts)
329-
appendPQExpBuffer(pgdumpopts, " --inserts");
330-
if (no_tablespaces)
331-
appendPQExpBuffer(pgdumpopts, " --no-tablespaces");
332-
if (quote_all_identifiers)
333-
appendPQExpBuffer(pgdumpopts, " --quote-all-identifiers");
334-
if (use_setsessauth)
335-
appendPQExpBuffer(pgdumpopts, " --use-set-session-authorization");
336-
319+
/* Complain if any arguments remain */
337320
if (optind < argc)
338321
{
339322
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
@@ -371,6 +354,24 @@ main(int argc, char *argv[])
371354
exit(1);
372355
}
373356

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

src/bin/pg_dump/pg_restore.c

+13-2
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.103 2010/08/13 14:38:04 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)