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

Commit61ad93c

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 parent7d43ca6 commit61ad93c

File tree

13 files changed

+115
-24
lines changed

13 files changed

+115
-24
lines changed

‎src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,6 +2818,7 @@ processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
28182818
pg_fatal("unrecognized encoding \"%s\"",
28192819
ptr1);
28202820
AH->public.encoding=encoding;
2821+
setFmtEncoding(encoding);
28212822
}
28222823
else
28232824
pg_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
@@ -1211,6 +1211,7 @@ setup_connection(Archive *AH, const char *dumpencoding,
12111211
* we know how to escape strings.
12121212
*/
12131213
AH->encoding = PQclientEncoding(conn);
1214+
setFmtEncoding(AH->encoding);
12141215

12151216
std_strings = PQparameterStatus(conn, "standard_conforming_strings");
12161217
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
@@ -524,6 +524,7 @@ main(int argc, char *argv[])
524524
* we know how to escape strings.
525525
*/
526526
encoding=PQclientEncoding(conn);
527+
setFmtEncoding(encoding);
527528
std_strings=PQparameterStatus(conn,"standard_conforming_strings");
528529
if (!std_strings)
529530
std_strings="off";

‎src/bin/psql/command.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,7 @@ exec_command_encoding(PsqlScanState scan_state, bool active_branch)
13361336
/* save encoding info into psql internal data */
13371337
pset.encoding=PQclientEncoding(pset.db);
13381338
pset.popt.topt.encoding=pset.encoding;
1339+
setFmtEncoding(pset.encoding);
13391340
SetVariable(pset.vars,"ENCODING",
13401341
pg_encoding_to_char(pset.encoding));
13411342
}
@@ -3956,6 +3957,8 @@ SyncVariables(void)
39563957
pset.popt.topt.encoding=pset.encoding;
39573958
pset.sversion=PQserverVersion(pset.db);
39583959

3960+
setFmtEncoding(pset.encoding);
3961+
39593962
SetVariable(pset.vars,"DBNAME",PQdb(pset.db));
39603963
SetVariable(pset.vars,"USER",PQuser(pset.db));
39613964
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
@@ -198,6 +198,8 @@ main(int argc, char *argv[])
198198

199199
conn=connectMaintenanceDatabase(&cparams,progname,echo);
200200

201+
setFmtEncoding(PQclientEncoding(conn));
202+
201203
initPQExpBuffer(&sql);
202204

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

‎src/bin/scripts/createuser.c

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

293293
conn=connectMaintenanceDatabase(&cparams,progname,echo);
294294

295+
setFmtEncoding(PQclientEncoding(conn));
296+
295297
initPQExpBuffer(&sql);
296298

297299
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
@@ -129,13 +129,6 @@ main(int argc, char *argv[])
129129
exit(0);
130130
}
131131

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

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

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

‎src/bin/scripts/dropuser.c

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

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

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

‎src/bin/scripts/reindexdb.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,8 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
512512

513513
if (tablespace)
514514
{
515-
appendPQExpBuffer(&sql,"%sTABLESPACE %s",sep,fmtId(tablespace));
515+
appendPQExpBuffer(&sql,"%sTABLESPACE %s",sep,
516+
fmtIdEnc(tablespace,PQclientEncoding(conn)));
516517
sep=comma;
517518
}
518519

@@ -552,7 +553,8 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
552553
{
553554
caseREINDEX_DATABASE:
554555
caseREINDEX_SYSTEM:
555-
appendPQExpBufferStr(&sql,fmtId(name));
556+
appendPQExpBufferStr(&sql,
557+
fmtIdEnc(name,PQclientEncoding(conn)));
556558
break;
557559
caseREINDEX_INDEX:
558560
caseREINDEX_TABLE:
@@ -775,8 +777,9 @@ get_parallel_object_list(PGconn *conn, ReindexType type,
775777
for (i=0;i<ntups;i++)
776778
{
777779
appendPQExpBufferStr(&buf,
778-
fmtQualifiedId(PQgetvalue(res,i,1),
779-
PQgetvalue(res,i,0)));
780+
fmtQualifiedIdEnc(PQgetvalue(res,i,1),
781+
PQgetvalue(res,i,0),
782+
PQclientEncoding(conn)));
780783

781784
simple_string_list_append(tables,buf.data);
782785
resetPQExpBuffer(&buf);
@@ -788,8 +791,9 @@ get_parallel_object_list(PGconn *conn, ReindexType type,
788791
* the order of tables list.
789792
*/
790793
appendPQExpBufferStr(&buf,
791-
fmtQualifiedId(PQgetvalue(res,i,1),
792-
PQgetvalue(res,i,2)));
794+
fmtQualifiedIdEnc(PQgetvalue(res,i,1),
795+
PQgetvalue(res,i,2),
796+
PQclientEncoding(conn)));
793797

794798
simple_string_list_append(user_list,buf.data);
795799
resetPQExpBuffer(&buf);

‎src/bin/scripts/vacuumdb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,9 @@ vacuum_one_database(ConnParams *cparams,
785785
for (i=0;i<ntups;i++)
786786
{
787787
appendPQExpBufferStr(&buf,
788-
fmtQualifiedId(PQgetvalue(res,i,1),
789-
PQgetvalue(res,i,0)));
788+
fmtQualifiedIdEnc(PQgetvalue(res,i,1),
789+
PQgetvalue(res,i,0),
790+
PQclientEncoding(conn)));
790791

791792
if (objects_listed&& !PQgetisnull(res,i,2))
792793
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*rawid);
28+
externconstchar*fmtIdEnc(constchar*rawid,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