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

Commit2ae54ae

Browse files
committed
Specify the encoding of input to fmtId()
This commit adds fmtIdEnc() and fmtQualifiedIdEnc(), which allow to specifythe encoding as an explicit argument. Additionally setFmtEncoding() isprovided, which defines the encoding when no explicit encoding is provided, toavoid breaking all code using fmtId().All users of fmtId()/fmtQualifiedId() are either converted to the explicitversion or a call to setFmtEncoding() has been added.This commit does not yet utilize the now well-defined encoding, that willhappen in a subsequent commit.Reviewed-by: Noah Misch <noah@leadboat.com>Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>Backpatch-through: 13Security:CVE-2025-1094
1 parent5bc33cb commit2ae54ae

File tree

13 files changed

+112
-22
lines changed

13 files changed

+112
-22
lines changed

‎src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,6 +2716,7 @@ processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
27162716
fatal("unrecognized encoding \"%s\"",
27172717
ptr1);
27182718
AH->public.encoding=encoding;
2719+
setFmtEncoding(encoding);
27192720
}
27202721
else
27212722
fatal("invalid ENCODING item: %s",

‎src/bin/pg_dump/pg_dump.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,7 @@ setup_connection(Archive *AH, const char *dumpencoding,
11261126
* we know how to escape strings.
11271127
*/
11281128
AH->encoding = PQclientEncoding(conn);
1129+
setFmtEncoding(AH->encoding);
11291130

11301131
std_strings = PQparameterStatus(conn, "standard_conforming_strings");
11311132
AH->std_strings = (std_strings && strcmp(std_strings, "on") == 0);

‎src/bin/pg_dump/pg_dumpall.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ main(int argc, char *argv[])
512512
* we know how to escape strings.
513513
*/
514514
encoding=PQclientEncoding(conn);
515+
setFmtEncoding(encoding);
515516
std_strings=PQparameterStatus(conn,"standard_conforming_strings");
516517
if (!std_strings)
517518
std_strings="off";

‎src/bin/psql/command.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,7 @@ exec_command_encoding(PsqlScanState scan_state, bool active_branch)
12801280
/* save encoding info into psql internal data */
12811281
pset.encoding=PQclientEncoding(pset.db);
12821282
pset.popt.topt.encoding=pset.encoding;
1283+
setFmtEncoding(pset.encoding);
12831284
SetVariable(pset.vars,"ENCODING",
12841285
pg_encoding_to_char(pset.encoding));
12851286
}
@@ -3669,6 +3670,8 @@ SyncVariables(void)
36693670
pset.popt.topt.encoding=pset.encoding;
36703671
pset.sversion=PQserverVersion(pset.db);
36713672

3673+
setFmtEncoding(pset.encoding);
3674+
36723675
SetVariable(pset.vars,"DBNAME",PQdb(pset.db));
36733676
SetVariable(pset.vars,"USER",PQuser(pset.db));
36743677
SetVariable(pset.vars,"HOST",PQhost(pset.db));

‎src/bin/scripts/common.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ appendQualifiedRelation(PQExpBuffer buf, const char *spec,
112112
exit(1);
113113
}
114114
appendPQExpBufferStr(buf,
115-
fmtQualifiedId(PQgetvalue(res,0,1),
116-
PQgetvalue(res,0,0)));
115+
fmtQualifiedIdEnc(PQgetvalue(res,0,1),
116+
PQgetvalue(res,0,0),
117+
PQclientEncoding(conn)));
117118
appendPQExpBufferStr(buf,columns);
118119
PQclear(res);
119120
termPQExpBuffer(&sql);

‎src/bin/scripts/createdb.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ main(int argc, char *argv[])
191191

192192
conn=connectMaintenanceDatabase(&cparams,progname,echo);
193193

194+
setFmtEncoding(PQclientEncoding(conn));
195+
194196
initPQExpBuffer(&sql);
195197

196198
appendPQExpBuffer(&sql,"CREATE DATABASE %s",

‎src/bin/scripts/createuser.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ main(int argc, char *argv[])
263263

264264
conn=connectMaintenanceDatabase(&cparams,progname,echo);
265265

266+
setFmtEncoding(PQclientEncoding(conn));
267+
266268
initPQExpBuffer(&sql);
267269

268270
printfPQExpBuffer(&sql,"CREATE ROLE %s",fmtId(newuser));

‎src/bin/scripts/dropdb.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,6 @@ main(int argc, char *argv[])
128128
exit(0);
129129
}
130130

131-
initPQExpBuffer(&sql);
132-
133-
appendPQExpBuffer(&sql,"DROP DATABASE %s%s%s;",
134-
(if_exists ?"IF EXISTS " :""),
135-
fmtId(dbname),
136-
force ?" WITH (FORCE)" :"");
137-
138131
/* Avoid trying to drop postgres db while we are connected to it. */
139132
if (maintenance_db==NULL&&strcmp(dbname,"postgres")==0)
140133
maintenance_db="template1";
@@ -148,6 +141,12 @@ main(int argc, char *argv[])
148141

149142
conn=connectMaintenanceDatabase(&cparams,progname,echo);
150143

144+
initPQExpBuffer(&sql);
145+
appendPQExpBuffer(&sql,"DROP DATABASE %s%s%s;",
146+
(if_exists ?"IF EXISTS " :""),
147+
fmtIdEnc(dbname,PQclientEncoding(conn)),
148+
force ?" WITH (FORCE)" :"");
149+
151150
if (echo)
152151
printf("%s\n",sql.data);
153152
result=PQexec(conn,sql.data);

‎src/bin/scripts/dropuser.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ main(int argc, char *argv[])
142142

143143
initPQExpBuffer(&sql);
144144
appendPQExpBuffer(&sql,"DROP ROLE %s%s;",
145-
(if_exists ?"IF EXISTS " :""),fmtId(dropuser));
145+
(if_exists ?"IF EXISTS " :""),
146+
fmtIdEnc(dropuser,PQclientEncoding(conn)));
146147

147148
if (echo)
148149
printf("%s\n",sql.data);

‎src/bin/scripts/reindexdb.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
533533

534534
if (tablespace)
535535
{
536-
appendPQExpBuffer(&sql,"%sTABLESPACE %s",sep,fmtId(tablespace));
536+
appendPQExpBuffer(&sql,"%sTABLESPACE %s",sep,
537+
fmtIdEnc(tablespace,PQclientEncoding(conn)));
537538
sep=comma;
538539
}
539540

@@ -573,7 +574,8 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
573574
{
574575
caseREINDEX_DATABASE:
575576
caseREINDEX_SYSTEM:
576-
appendPQExpBufferStr(&sql,fmtId(name));
577+
appendPQExpBufferStr(&sql,
578+
fmtIdEnc(name,PQclientEncoding(conn)));
577579
break;
578580
caseREINDEX_INDEX:
579581
caseREINDEX_TABLE:
@@ -743,8 +745,9 @@ get_parallel_object_list(PGconn *conn, ReindexType type,
743745
for (i=0;i<ntups;i++)
744746
{
745747
appendPQExpBufferStr(&buf,
746-
fmtQualifiedId(PQgetvalue(res,i,1),
747-
PQgetvalue(res,i,0)));
748+
fmtQualifiedIdEnc(PQgetvalue(res,i,1),
749+
PQgetvalue(res,i,0),
750+
PQclientEncoding(conn)));
748751

749752
simple_string_list_append(tables,buf.data);
750753
resetPQExpBuffer(&buf);

‎src/bin/scripts/vacuumdb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,9 @@ vacuum_one_database(ConnParams *cparams,
701701
for (i=0;i<ntups;i++)
702702
{
703703
appendPQExpBufferStr(&buf,
704-
fmtQualifiedId(PQgetvalue(res,i,1),
705-
PQgetvalue(res,i,0)));
704+
fmtQualifiedIdEnc(PQgetvalue(res,i,1),
705+
PQgetvalue(res,i,0),
706+
PQclientEncoding(conn)));
706707

707708
if (tables_listed&& !PQgetisnull(res,i,2))
708709
appendPQExpBufferStr(&buf,PQgetvalue(res,i,2));

‎src/fe_utils/string_utils.c

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@
1919

2020
#include"common/keywords.h"
2121
#include"fe_utils/string_utils.h"
22+
#include"mb/pg_wchar.h"
2223

2324
staticPQExpBufferdefaultGetLocalPQExpBuffer(void);
2425

2526
/* Globals exported by this file */
2627
intquote_all_identifiers=0;
2728
PQExpBuffer (*getLocalPQExpBuffer) (void)=defaultGetLocalPQExpBuffer;
2829

30+
staticintfmtIdEncoding=-1;
31+
2932

3033
/*
3134
* Returns a temporary PQExpBuffer, valid until the next call to the function.
@@ -54,14 +57,48 @@ defaultGetLocalPQExpBuffer(void)
5457
returnid_return;
5558
}
5659

60+
/*
61+
* Set the encoding that fmtId() and fmtQualifiedId() use.
62+
*
63+
* This is not safe against multiple connections having different encodings,
64+
* but there is no real other way to address the need to know the encoding for
65+
* fmtId()/fmtQualifiedId() input for safe escaping. Eventually we should get
66+
* rid of fmtId().
67+
*/
68+
void
69+
setFmtEncoding(intencoding)
70+
{
71+
fmtIdEncoding=encoding;
72+
}
73+
74+
/*
75+
* Return the currently configured encoding for fmtId() and fmtQualifiedId().
76+
*/
77+
staticint
78+
getFmtEncoding(void)
79+
{
80+
if (fmtIdEncoding!=-1)
81+
returnfmtIdEncoding;
82+
83+
/*
84+
* In assertion builds it seems best to fail hard if the encoding was not
85+
* set, to make it easier to find places with missing calls. But in
86+
* production builds that seems like a bad idea, thus we instead just
87+
* default to UTF-8.
88+
*/
89+
Assert(fmtIdEncoding!=-1);
90+
91+
returnPG_UTF8;
92+
}
93+
5794
/*
5895
*Quotes input string if it's not a legitimate SQL identifier as-is.
5996
*
60-
*Note that the returned string must be used before callingfmtId again,
97+
*Note that the returned string must be used before callingfmtIdEnc again,
6198
*since we re-use the same return buffer each time.
6299
*/
63100
constchar*
64-
fmtId(constchar*rawid)
101+
fmtIdEnc(constchar*rawid,intencoding)
65102
{
66103
PQExpBufferid_return=getLocalPQExpBuffer();
67104

@@ -134,25 +171,42 @@ fmtId(const char *rawid)
134171
}
135172

136173
/*
137-
* fmtQualifiedId - construct a schema-qualified name, with quoting as needed.
174+
*Quotes input string if it's not a legitimate SQL identifier as-is.
175+
*
176+
*Note that the returned string must be used before calling fmtId again,
177+
*since we re-use the same return buffer each time.
178+
*
179+
* NB: This assumes setFmtEncoding() previously has been called to configure
180+
* the encoding of rawid. It is preferable to use fmtIdEnc() with an
181+
* explicit encoding.
182+
*/
183+
constchar*
184+
fmtId(constchar*rawid)
185+
{
186+
returnfmtIdEnc(rawid,getFmtEncoding());
187+
}
188+
189+
/*
190+
* fmtQualifiedIdEnc - construct a schema-qualified name, with quoting as
191+
* needed.
138192
*
139193
* Like fmtId, use the result before calling again.
140194
*
141195
* Since we call fmtId and it also uses getLocalPQExpBuffer() we cannot
142196
* use that buffer until we're finished with calling fmtId().
143197
*/
144198
constchar*
145-
fmtQualifiedId(constchar*schema,constchar*id)
199+
fmtQualifiedIdEnc(constchar*schema,constchar*id,intencoding)
146200
{
147201
PQExpBufferid_return;
148202
PQExpBufferlcl_pqexp=createPQExpBuffer();
149203

150204
/* Some callers might fail to provide a schema name */
151205
if (schema&&*schema)
152206
{
153-
appendPQExpBuffer(lcl_pqexp,"%s.",fmtId(schema));
207+
appendPQExpBuffer(lcl_pqexp,"%s.",fmtIdEnc(schema,encoding));
154208
}
155-
appendPQExpBufferStr(lcl_pqexp,fmtId(id));
209+
appendPQExpBufferStr(lcl_pqexp,fmtIdEnc(id,encoding));
156210

157211
id_return=getLocalPQExpBuffer();
158212

@@ -162,6 +216,24 @@ fmtQualifiedId(const char *schema, const char *id)
162216
returnid_return->data;
163217
}
164218

219+
/*
220+
* fmtQualifiedId - construct a schema-qualified name, with quoting as needed.
221+
*
222+
* Like fmtId, use the result before calling again.
223+
*
224+
* Since we call fmtId and it also uses getLocalPQExpBuffer() we cannot
225+
* use that buffer until we're finished with calling fmtId().
226+
*
227+
* NB: This assumes setFmtEncoding() previously has been called to configure
228+
* the encoding of schema/id. It is preferable to use fmtQualifiedIdEnc()
229+
* with an explicit encoding.
230+
*/
231+
constchar*
232+
fmtQualifiedId(constchar*schema,constchar*id)
233+
{
234+
returnfmtQualifiedIdEnc(schema,id,getFmtEncoding());
235+
}
236+
165237

166238
/*
167239
* Format a Postgres version number (in the PG_VERSION_NUM integer format

‎src/include/fe_utils/string_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ extern PQExpBuffer (*getLocalPQExpBuffer) (void);
2525

2626
/* Functions */
2727
externconstchar*fmtId(constchar*identifier);
28+
externconstchar*fmtIdEnc(constchar*identifier,intencoding);
2829
externconstchar*fmtQualifiedId(constchar*schema,constchar*id);
30+
externconstchar*fmtQualifiedIdEnc(constchar*schema,constchar*id,intencoding);
31+
externvoidsetFmtEncoding(intencoding);
2932

3033
externchar*formatPGVersionNumber(intversion_number,boolinclude_minor,
3134
char*buf,size_tbuflen);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp