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

Commit008cf04

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 parent2c0797d commit008cf04

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::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: 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)

[8]ページ先頭

©2009-2025 Movatter.jp