Fix for fragile code in ltree/crc32.c

Started by Jeff Davis3 months ago6 messageshackers
Jump to latest
#1Jeff Davis
pgsql@j-davis.com

The buffer in ltree_crc32_sz() is more than large enough in practice,
but it didn't explicitly leave room for the NUL byte. Patch attached.

Regards,
Jeff Davis

Attachments:

t253477_1
v1-0001-ltree-crc32.c-fix-fragile-code.patchtext/x-patch; charset=UTF-8; name=v1-0001-ltree-crc32.c-fix-fragile-code.patchDownload+3-4
#2Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Jeff Davis (#1)
Re: Fix for fragile code in ltree/crc32.c

On 19/08/2026 03:05, Jeff Davis wrote:

The buffer in ltree_crc32_sz() is more than large enough in practice,
but it didn't explicitly leave room for the NUL byte. Patch attached.

Makes sense.

Looking at pg_strfold() (and other similar functions), there's no
indication that there's an upper bound on the destination size like
this. Does UNICODE_CASEMAP_BUFSZ work for all collation providers and
locales? How about adding a comment in pg_strfold() and friends about that?
- Heikki

#3Jeff Davis
pgsql@j-davis.com
In reply to: Heikki Linnakangas (#2)
Re: Fix for fragile code in ltree/crc32.c

On Wed, 2026-08-19 at 10:44 +0300, Heikki Linnakangas wrote:

Looking at pg_strfold() (and other similar functions), there's no
indication that there's an upper bound on the destination size like
this. Does UNICODE_CASEMAP_BUFSZ work for all collation providers and
locales? How about adding a comment in pg_strfold() and friends about
that?

Attached two patches. The first one is the same and backpatches through
18. The second defines some new macros and uses those, which is a
better place to document the limits, and I'll only commit that one to
master.

Along with the Case Mapping Complexities comment I'm adding here:

/messages/by-id/fe35594a54923c94142390dabb365b737c581f2a.camel@j-davis.com

(which will be backpatched to 18), I think that should be sufficient,
but I could add another note there about the byte limits if you think
it would be worthwhile.

Regards,
Jeff Davis

Attachments:

t253477_3
v2-0001-ltree-crc32.c-fix-fragile-code.patchtext/x-patch; charset=UTF-8; name=v2-0001-ltree-crc32.c-fix-fragile-code.patchDownload+3-4
v2-0002-pg_locale.h-define-casemapping-limits.patchtext/x-patch; charset=UTF-8; name=v2-0002-pg_locale.h-define-casemapping-limits.patchDownload+31-15
#4Rithvika Devisetti
devisettirithvika@gmail.com
In reply to: Jeff Davis (#3)
Re: Fix for fragile code in ltree/crc32.c

On Thu, 2026-08-20 at 13:32 -0700, Jeff Davis wrote:

Attached two patches. The first one is the same and backpatches through
18. The second defines some new macros and uses those, which is a
better place to document the limits, and I'll only commit that one to
master.

I applied both v2 patches and tested them on macOS 15 (aarch64,
clang 21), built with meson and --enable-cassert. Reporting results in
case an independent check on a non-Linux platform is useful.

Full test suite passes: 360 tests OK, 0 failures. The 40 skips are all
PG_TEST_EXTRA opt-ins (ssl, ldap, oauth, load_balance,
libpq_encryption, xid_wraparound). I ran saslprep separately with
PG_TEST_EXTRA=saslprep and it passes as well.

To exercise the changed path specifically, I used a database with
LOCALE_PROVIDER builtin / BUILTIN_LOCALE 'PG_UNICODE_FAST' and checked
the expanding case mappings:

upper('ß') -> 'SS' (2 codepoints)
upper('ffl') -> 'FFL' (3 codepoints)
upper('ΐ') -> 3 codepoints
upper('ῷ') -> 3 codepoints
casefold('ß') -> 'ss'

and drove ltree_crc32_sz() via ltree tables with GiST and btree indexes
over 1000 rows. No assertion failures from the new
Assert(foldlen < sizeof(foldstr)).

Regards,
Rithvika Devisetti

On Thu, Aug 20, 2026 at 1:32 PM Jeff Davis <pgsql@j-davis.com> wrote:

Show quoted text

On Wed, 2026-08-19 at 10:44 +0300, Heikki Linnakangas wrote:

Looking at pg_strfold() (and other similar functions), there's no
indication that there's an upper bound on the destination size like
this. Does UNICODE_CASEMAP_BUFSZ work for all collation providers and
locales? How about adding a comment in pg_strfold() and friends about
that?

Attached two patches. The first one is the same and backpatches through
18. The second defines some new macros and uses those, which is a
better place to document the limits, and I'll only commit that one to
master.

Along with the Case Mapping Complexities comment I'm adding here:

/messages/by-id/fe35594a54923c94142390dabb365b737c581f2a.camel@j-davis.com

(which will be backpatched to 18), I think that should be sufficient,
but I could add another note there about the byte limits if you think
it would be worthwhile.

Regards,
Jeff Davis

#5Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Rithvika Devisetti (#4)
Re: Fix for fragile code in ltree/crc32.c

On 22/08/2026 09:04, Rithvika Devisetti wrote:

On Thu, 2026-08-20 at 13:32 -0700, Jeff Davis wrote:

Attached two patches. The first one is the same and backpatches through
18. The second defines some new macros and uses those, which is a
better place to document the limits, and I'll only commit that one to
master.

I applied both v2 patches and tested them on macOS 15 (aarch64,
clang 21), built with meson and --enable-cassert. Reporting results in
case an independent check on a non-Linux platform is useful.

Full test suite passes: 360 tests OK, 0 failures. The 40 skips are all
PG_TEST_EXTRA opt-ins (ssl, ldap, oauth, load_balance,
libpq_encryption, xid_wraparound). I ran saslprep separately with
PG_TEST_EXTRA=saslprep and it passes as well.

To exercise the changed path specifically, I used a database with
LOCALE_PROVIDER builtin / BUILTIN_LOCALE 'PG_UNICODE_FAST' and checked
the expanding case mappings:

upper('ß') -> 'SS' (2 codepoints)
upper('ffl') -> 'FFL' (3 codepoints)
upper('ΐ') -> 3 codepoints
upper('ῷ') -> 3 codepoints
casefold('ß') -> 'ss'

and drove ltree_crc32_sz() via ltree tables with GiST and btree indexes
over 1000 rows. No assertion failures from the new
Assert(foldlen < sizeof(foldstr)).

Hmm, so the code has this:

/*
* Expansion factor of string length, not including terminating NUL. That is,
* the upper bound of the number of multibyte characters in the result string
* per multibyte character in the input string.
*
* NB: assumes no provider exceeds the Unicode-defined maximum.
*/
#define PG_MAX_CASEMAP_MBCHARS UNICODE_MAX_CASEMAP_CODEPOINTS

Is it a number of codepoints or bytes? The comment says "multibyte
characters" which I think means codepoints in unicode, but the ltree
code does this:

/* max space required to map single codepoint, including NUL */
char foldstr[PG_CASEMAP_BUFSZ];

Which sure looks like # of bytes.

- Heikki

#6Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Heikki Linnakangas (#5)
Re: Fix for fragile code in ltree/crc32.c

On 25/08/2026 11:13, Heikki Linnakangas wrote:

/*
 * Expansion factor of string length, not including terminating NUL.
That is,
 * the upper bound of the number of multibyte characters in the result
string
 * per multibyte character in the input string.
 *
 * NB: assumes no provider exceeds the Unicode-defined maximum.
 */
#define PG_MAX_CASEMAP_MBCHARS        UNICODE_MAX_CASEMAP_CODEPOINTS

Is it a number of codepoints or bytes? The comment says "multibyte
characters" which I think means codepoints in unicode, but the ltree
code does this:

        /* max space required to map single codepoint, including NUL */
        char        foldstr[PG_CASEMAP_BUFSZ];

Which sure looks like # of bytes.

Never mind. Just after sending this I saw that PG_CASEMAP_BUFSZ is
different from PG_MAX_CASEMAP_MBCHARS. Sorry for the noise.

- Heikki