Skip to content

Commit 095527a

Browse files
committed
Don't override arguments set via options with positional arguments.
A number of utility programs were rather careless about paremeters that can be set via both an option argument and a positional argument. This leads to results which can violate the Principal Of Least Astonishment. These changes refuse to use positional arguments to override settings that have been made via positional arguments. The changes are backpatched to all live branches.
1 parent 8d444c9 commit 095527a

File tree

6 files changed

+75
-38
lines changed

6 files changed

+75
-38
lines changed

src/bin/initdb/initdb.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,8 +2612,11 @@ main(int argc, char *argv[])
26122612
}
26132613

26142614

2615-
/* Non-option argument specifies data directory */
2616-
if (optind < argc)
2615+
/*
2616+
* Non-option argument specifies data directory
2617+
* as long as it wasn't already specified with -D / --pgdata
2618+
*/
2619+
if (optind < argc && strlen(pg_data) == 0)
26172620
{
26182621
pg_data = xstrdup(argv[optind]);
26192622
optind++;

src/bin/scripts/clusterdb.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,22 @@ main(int argc, char *argv[])
106106
}
107107
}
108108

109-
switch (argc - optind)
109+
/*
110+
* Non-option argument specifies database name
111+
* as long as it wasn't already specified with -d / --dbname
112+
*/
113+
if (optind < argc && dbname == NULL)
110114
{
111-
case 0:
112-
break;
113-
case 1:
114-
dbname = argv[optind];
115-
break;
116-
default:
117-
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
118-
progname, argv[optind + 1]);
119-
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
120-
exit(1);
115+
dbname = argv[optind];
116+
optind++;
117+
}
118+
119+
if (optind < argc)
120+
{
121+
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
122+
progname, argv[optind + 1]);
123+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
124+
exit(1);
121125
}
122126

123127
setup_cancel_handler();

src/bin/scripts/createlang.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,24 @@ main(int argc, char *argv[])
9191
}
9292
}
9393

94+
/*
95+
* We set dbname from positional arguments if it is not
96+
* already set by option arguments -d. If not doing
97+
* listlangs, positional dbname must follow positional
98+
* langname.
99+
*/
100+
94101
if (argc - optind > 0)
95102
{
96103
if (listlangs)
97-
dbname = argv[optind++];
104+
{
105+
if (dbname == NULL)
106+
dbname = argv[optind++];
107+
}
98108
else
99109
{
100110
langname = argv[optind++];
101-
if (argc - optind > 0)
111+
if (argc - optind > 0 && dbname == NULL)
102112
dbname = argv[optind++];
103113
}
104114
}

src/bin/scripts/droplang.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,24 @@ main(int argc, char *argv[])
102102
}
103103
}
104104

105+
/*
106+
* We set dbname from positional arguments if it is not
107+
* already set by option arguments -d. If not doing
108+
* listlangs, positional dbname must follow positional
109+
* langname.
110+
*/
111+
105112
if (argc - optind > 0)
106113
{
107114
if (listlangs)
108-
dbname = argv[optind++];
115+
{
116+
if (dbname == NULL)
117+
dbname = argv[optind++];
118+
}
109119
else
110120
{
111121
langname = argv[optind++];
112-
if (argc - optind > 0)
122+
if (argc - optind > 0 && dbname == NULL)
113123
dbname = argv[optind++];
114124
}
115125
}

src/bin/scripts/reindexdb.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,22 @@ main(int argc, char *argv[])
116116
}
117117
}
118118

119-
switch (argc - optind)
119+
/*
120+
* Non-option argument specifies database name
121+
* as long as it wasn't already specified with -d / --dbname
122+
*/
123+
if (optind < argc && dbname == NULL)
120124
{
121-
case 0:
122-
break;
123-
case 1:
124-
dbname = argv[optind];
125-
break;
126-
default:
127-
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"), progname, argv[optind + 1]);
128-
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
129-
exit(1);
125+
dbname = argv[optind];
126+
optind++;
127+
}
128+
129+
if (optind < argc)
130+
{
131+
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
132+
progname, argv[optind + 1]);
133+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
134+
exit(1);
130135
}
131136

132137
setup_cancel_handler();

src/bin/scripts/vacuumdb.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,23 @@ main(int argc, char *argv[])
123123
}
124124
}
125125

126-
switch (argc - optind)
126+
127+
/*
128+
* Non-option argument specifies database name
129+
* as long as it wasn't already specified with -d / --dbname
130+
*/
131+
if (optind < argc && dbname == NULL)
127132
{
128-
case 0:
129-
break;
130-
case 1:
131-
dbname = argv[optind];
132-
break;
133-
default:
134-
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
135-
progname, argv[optind + 1]);
136-
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
137-
exit(1);
133+
dbname = argv[optind];
134+
optind++;
135+
}
136+
137+
if (optind < argc)
138+
{
139+
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
140+
progname, argv[optind + 1]);
141+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
142+
exit(1);
138143
}
139144

140145
setup_cancel_handler();

0 commit comments

Comments
 (0)