Skip to content

Commit 008cf04

Browse files
committed
createdb: Fix quoting of --encoding, --lc-ctype and --lc-collate
The original coding failed to properly quote those arguments, leading to failures when using quotes in the values used. As the quoting can be encoding-sensitive, the connection to the backend needs to be taken before applying the correct quoting. Author: Michael Paquier Reviewed-by: Daniel Gustafsson Discussion: https://postgr.es/m/20200214041004.GB1998@paquier.xyz Backpatch-through: 9.5
1 parent 2c0797d commit 008cf04

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed

src/bin/scripts/createdb.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ main(int argc, char *argv[])
176176
dbname = get_user_name_or_exit(progname);
177177
}
178178

179+
/* No point in trying to use postgres db when creating postgres db. */
180+
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
181+
maintenance_db = "template1";
182+
183+
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
184+
prompt_password, progname, echo);
185+
179186
initPQExpBuffer(&sql);
180187

181188
appendPQExpBuffer(&sql, "CREATE DATABASE %s",
@@ -186,23 +193,25 @@ main(int argc, char *argv[])
186193
if (tablespace)
187194
appendPQExpBuffer(&sql, " TABLESPACE %s", fmtId(tablespace));
188195
if (encoding)
189-
appendPQExpBuffer(&sql, " ENCODING '%s'", encoding);
196+
{
197+
appendPQExpBufferStr(&sql, " ENCODING ");
198+
appendStringLiteralConn(&sql, encoding, conn);
199+
}
190200
if (template)
191201
appendPQExpBuffer(&sql, " TEMPLATE %s", fmtId(template));
192202
if (lc_collate)
193-
appendPQExpBuffer(&sql, " LC_COLLATE '%s'", lc_collate);
203+
{
204+
appendPQExpBufferStr(&sql, " LC_COLLATE ");
205+
appendStringLiteralConn(&sql, lc_collate, conn);
206+
}
194207
if (lc_ctype)
195-
appendPQExpBuffer(&sql, " LC_CTYPE '%s'", lc_ctype);
208+
{
209+
appendPQExpBufferStr(&sql, " LC_CTYPE ");
210+
appendStringLiteralConn(&sql, lc_ctype, conn);
211+
}
196212

197213
appendPQExpBufferChar(&sql, ';');
198214

199-
/* No point in trying to use postgres db when creating postgres db. */
200-
if (maintenance_db == NULL && strcmp(dbname, "postgres") == 0)
201-
maintenance_db = "template1";
202-
203-
conn = connectMaintenanceDatabase(maintenance_db, host, port, username,
204-
prompt_password, progname, echo);
205-
206215
if (echo)
207216
printf("%s\n", sql.data);
208217
result = PQexec(conn, sql.data);

src/bin/scripts/t/020_createdb.pl

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use PostgresNode;
55
use TestLib;
6-
use Test::More tests => 13;
6+
use Test::More tests => 22;
77

88
program_help_ok('createdb');
99
program_version_ok('createdb');
@@ -24,3 +24,27 @@
2424

2525
$node->command_fails([ 'createdb', 'foobar1' ],
2626
'fails if database already exists');
27+
28+
# Check quote handling with incorrect option values.
29+
$node->command_checks_all(
30+
[ 'createdb', '--encoding', "foo'; SELECT '1", 'foobar2' ],
31+
1,
32+
[qr/^$/],
33+
[qr/^createdb: error: "foo'; SELECT '1" is not a valid encoding name/s],
34+
'createdb with incorrect --lc-collate');
35+
$node->command_checks_all(
36+
[ 'createdb', '--lc-collate', "foo'; SELECT '1", 'foobar2' ],
37+
1,
38+
[qr/^$/],
39+
[
40+
qr/^createdb: error: database creation failed: ERROR: invalid locale name/s
41+
],
42+
'createdb with incorrect --lc-collate');
43+
$node->command_checks_all(
44+
[ 'createdb', '--lc-ctype', "foo'; SELECT '1", 'foobar2' ],
45+
1,
46+
[qr/^$/],
47+
[
48+
qr/^createdb: error: database creation failed: ERROR: invalid locale name/s
49+
],
50+
'createdb with incorrect --lc-ctype');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy