Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit83bd732

Browse files
committed
createdb: Fix quoting of --encoding, --lc-ctype and --lc-collate
The original coding failed to properly quote those arguments, leading tofailures when using quotes in the values used. As the quoting can beencoding-sensitive, the connection to the backend needs to be takenbefore applying the correct quoting.Author: Michael PaquierReviewed-by: Daniel GustafssonDiscussion:https://postgr.es/m/20200214041004.GB1998@paquier.xyzBackpatch-through: 9.5
1 parent1431524 commit83bd732

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
@@ -177,6 +177,13 @@ main(int argc, char *argv[])
177177
dbname=get_user_name_or_exit(progname);
178178
}
179179

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

182189
appendPQExpBuffer(&sql,"CREATE DATABASE %s",
@@ -187,23 +194,25 @@ main(int argc, char *argv[])
187194
if (tablespace)
188195
appendPQExpBuffer(&sql," TABLESPACE %s",fmtId(tablespace));
189196
if (encoding)
190-
appendPQExpBuffer(&sql," ENCODING '%s'",encoding);
197+
{
198+
appendPQExpBufferStr(&sql," ENCODING ");
199+
appendStringLiteralConn(&sql,encoding,conn);
200+
}
191201
if (template)
192202
appendPQExpBuffer(&sql," TEMPLATE %s",fmtId(template));
193203
if (lc_collate)
194-
appendPQExpBuffer(&sql," LC_COLLATE '%s'",lc_collate);
204+
{
205+
appendPQExpBufferStr(&sql," LC_COLLATE ");
206+
appendStringLiteralConn(&sql,lc_collate,conn);
207+
}
195208
if (lc_ctype)
196-
appendPQExpBuffer(&sql," LC_CTYPE '%s'",lc_ctype);
209+
{
210+
appendPQExpBufferStr(&sql," LC_CTYPE ");
211+
appendStringLiteralConn(&sql,lc_ctype,conn);
212+
}
197213

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

200-
/* No point in trying to use postgres db when creating postgres db. */
201-
if (maintenance_db==NULL&&strcmp(dbname,"postgres")==0)
202-
maintenance_db="template1";
203-
204-
conn=connectMaintenanceDatabase(maintenance_db,host,port,username,
205-
prompt_password,progname,echo);
206-
207216
if (echo)
208217
printf("%s\n",sql.data);
209218
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::Moretests=>13;
6+
use Test::Moretests=>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: "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: 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: database creation failed: ERROR: invalid locale name/s
49+
],
50+
'createdb with incorrect --lc-ctype');

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp