Skip to content

Commit 11d7e27

Browse files
tglsfdcbigplaice
authored andcommitted
Adapt appendPsqlMetaConnect() to the new fmtId() encoding expectations.
We need to tell fmtId() what encoding to assume, but this function doesn't know that. Fortunately we can fix that without changing the function's API, because we can just use SQL_ASCII. That's because database names in connection requests are effectively binary not text: no encoding-aware processing will happen on them. This fixes XversionUpgrade failures seen in the buildfarm. The alternative of having pg_upgrade use setFmtEncoding() is unappetizing, given that it's connecting to multiple databases that may have different encodings. Andres Freund, Noah Misch, Tom Lane Security: CVE-2025-1094
1 parent 110ee43 commit 11d7e27

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

src/fe_utils/string_utils.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -804,29 +804,38 @@ appendPsqlMetaConnect(PQExpBuffer buf, const char *dbname)
804804
}
805805
}
806806

807-
appendPQExpBufferStr(buf, "\\connect ");
808807
if (complex)
809808
{
810809
PQExpBufferData connstr;
811810

812811
initPQExpBuffer(&connstr);
812+
813+
/*
814+
* Force the target psql's encoding to SQL_ASCII. We don't really
815+
* know the encoding of the database name, and it doesn't matter as
816+
* long as psql will forward it to the server unchanged.
817+
*/
818+
appendPQExpBufferStr(buf, "\\encoding SQL_ASCII\n");
819+
appendPQExpBufferStr(buf, "\\connect -reuse-previous=on ");
820+
813821
appendPQExpBufferStr(&connstr, "dbname=");
814822
appendConnStrVal(&connstr, dbname);
815823

816-
appendPQExpBufferStr(buf, "-reuse-previous=on ");
817-
818824
/*
819825
* As long as the name does not contain a newline, SQL identifier
820826
* quoting satisfies the psql meta-command parser. Prefer not to
821827
* involve psql-interpreted single quotes, which behaved differently
822828
* before PostgreSQL 9.2.
823829
*/
824-
appendPQExpBufferStr(buf, fmtId(connstr.data));
830+
appendPQExpBufferStr(buf, fmtIdEnc(connstr.data, PG_SQL_ASCII));
825831

826832
termPQExpBuffer(&connstr);
827833
}
828834
else
829-
appendPQExpBufferStr(buf, fmtId(dbname));
835+
{
836+
appendPQExpBufferStr(buf, "\\connect ");
837+
appendPQExpBufferStr(buf, fmtIdEnc(dbname, PG_SQL_ASCII));
838+
}
830839
appendPQExpBufferChar(buf, '\n');
831840
}
832841

0 commit comments

Comments
 (0)