Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1b37a8c

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 fe546f3 commit 1b37a8c

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
@@ -2760,8 +2760,11 @@ main(int argc, char *argv[])
27602760
}
27612761

27622762

2763-
/* Non-option argument specifies data directory */
2764-
if (optind < argc)
2763+
/*
2764+
* Non-option argument specifies data directory
2765+
* as long as it wasn't already specified with -D / --pgdata
2766+
*/
2767+
if (optind < argc && strlen(pg_data) == 0)
27652768
{
27662769
pg_data = xstrdup(argv[optind]);
27672770
optind++;

src/bin/scripts/clusterdb.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,22 @@ main(int argc, char *argv[])
112112
}
113113
}
114114

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

129133
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
@@ -90,14 +90,24 @@ main(int argc, char *argv[])
9090
}
9191
}
9292

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

src/bin/scripts/reindexdb.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,22 @@ main(int argc, char *argv[])
122122
}
123123
}
124124

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

138143
setup_cancel_handler();

src/bin/scripts/vacuumdb.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,23 @@ main(int argc, char *argv[])
135135
}
136136
}
137137

138-
switch (argc - optind)
138+
139+
/*
140+
* Non-option argument specifies database name
141+
* as long as it wasn't already specified with -d / --dbname
142+
*/
143+
if (optind < argc && dbname == NULL)
139144
{
140-
case 0:
141-
break;
142-
case 1:
143-
dbname = argv[optind];
144-
break;
145-
default:
146-
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
147-
progname, argv[optind + 1]);
148-
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
149-
exit(1);
145+
dbname = argv[optind];
146+
optind++;
147+
}
148+
149+
if (optind < argc)
150+
{
151+
fprintf(stderr, _("%s: too many command-line arguments (first is \"%s\")\n"),
152+
progname, argv[optind + 1]);
153+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
154+
exit(1);
150155
}
151156

152157
if (analyze_only)

0 commit comments

Comments
 (0)