From: Bruce Momjian <bruce@momjian.us>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Cc: Nathan Bossart <nathandbossart@gmail.com>
Cc: adam@labkey.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18711: Attempting a connection with a database name longer than 63 characters now fails
Date: Sat, 23 Nov 2024 12:50:53 -0500
Message-ID: <Z0IV_c6cUUjw-sGn@momjian.us> (raw)
In-Reply-To: <659706.1732303427@sss.pgh.pa.us>
References: <Zz9B3KQGXFCGVPXy@momjian.us>
<Zz9FRrwJRlyGBFPN@ip-10-97-1-34.eu-west-3.compute.internal>
<Zz9IHPBf-z8MsLdw@momjian.us>
<Zz9OTyWAvATeeHev@nathan>
<Zz9jfOkVmlYcYHSy@momjian.us>
<Zz9pOi3pGF-DnJTp@nathan>
<Zz92cka2VlBDsat3@momjian.us>
<Zz94l3LHsML0V9eu@nathan>
<Z0A2o+dS0MqDE2Fs@ip-10-97-1-34.eu-west-3.compute.internal>
<659706.1732303427@sss.pgh.pa.us>
On Fri, Nov 22, 2024 at 02:23:47PM -0500, Tom Lane wrote:
> + if (strlen(dbname) < NAMEDATALEN)
> + {
> + /* Typical, easy case: no truncation needed */
> + tuple = GetDatabaseTupleInternal(relation, dbname);
> + }
> + else if (!IS_HIGHBIT_SET(dbname[NAMEDATALEN - 1]) ||
> + !IS_HIGHBIT_SET(dbname[NAMEDATALEN - 2]))
> + {
> + /* Also easy: truncation at NAMEDATALEN - 1 cannot break an MB char */
> + memcpy(tname, dbname, sizeof(tname));
> + tname[NAMEDATALEN - 1] = '\0';
> + tuple = GetDatabaseTupleInternal(relation, tname);
I had some time to think about this issue and I now realize the test
above is correct, but I couldn't figure out why it was correct before.
If we want to use this test, which I now think is fine, I suggest the
following comment:
If we put a NULL byte at byte offset NAMEDATALEN - 1, we don't want
to break a multi-byte character when doing this. If byte offset
NAMEDATALEN - 1 does not have its high bit set, we can be sure
we will not break a multi-byte character during the overwrite.
Also, if NAMEDATALEN - 2 does not have its high bit set, then
NAMEDATALEN - 1 is either a single-byte non-ASCII character,
or is the _start_ of a multi-byte character, so it is also safe
to overwrite it without breaking a multi-byte character.
Sorry I did not figure this out earlier.
--
Bruce Momjian <bruce@momjian.us> https://momjian.us
EDB https://enterprisedb.com
When a patient asks the doctor, "Am I going to die?", he means
"Am I going to die soon?"
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: bruce@momjian.us, tgl@sss.pgh.pa.us, bertranddrouvot.pg@gmail.com, nathandbossart@gmail.com, adam@labkey.com, pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18711: Attempting a connection with a database name longer than 63 characters now fails
In-Reply-To: <Z0IV_c6cUUjw-sGn@momjian.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox