pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Fix for fragile code in ltree/crc32.c
6+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-08-24 1e9b06a8e0  pg_locale.h: define case mapping limits.
  2026-08-24 aea7bed4a7 5ce1cf485c 8fa803dd69  ltree/crc32.c: fix fragile code.
* Fix for fragile code in ltree/crc32.c
@ 2026-08-19 00:05 Jeff Davis <pgsql@j-davis.com>
  2026-08-19 07:44 ` Re: Fix for fragile code in ltree/crc32.c Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 1 reply; 6+ messages in thread

From: Jeff Davis @ 2026-08-19 00:05 UTC (permalink / raw)
  To: pgsql-hackers


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:

v1-0001-ltree-crc32.c-fix-fragile-code.patch (text/x-patch, 1.3K) download From f0d8e75f7385af2b7e9c621a2538da5e4e9e0148 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Tue, 18 Aug 2026 14:57:32 -0700 Subject: [PATCH v1] ltree/crc32.c: fix fragile code. Explicitly make space for the NUL when casefolding. No known bug in the previous code, because the buffer was more than large enough for folding any codepoint with enough room left for a NUL. But it was fragile, and would break if UNICODE_CASEMAP_BUFSZ were a tighter bound. Backpatch-through: 18 --- contrib/ltree/crc32.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/ltree/crc32.c b/contrib/ltree/crc32.c index d21bed31fdd..617cc652a7a 100644 --- a/contrib/ltree/crc32.c +++ b/contrib/ltree/crc32.c @@ -32,13 +32,13 @@ ltree_crc32_sz(const char *buf, int size) INIT_TRADITIONAL_CRC32(crc); while (size > 0) { - char foldstr[UNICODE_CASEMAP_BUFSZ]; + char foldstr[UNICODE_CASEMAP_BUFSZ + 1]; int srclen = pg_mblen_range(p, end); size_t foldlen; /* fold one codepoint at a time */ - foldlen = pg_strfold(foldstr, UNICODE_CASEMAP_BUFSZ, p, srclen, - locale); + foldlen = pg_strfold(foldstr, sizeof(foldstr), p, srclen, locale); + Assert(foldlen < sizeof(foldstr)); COMP_TRADITIONAL_CRC32(crc, foldstr, foldlen); -- 2.43.0
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: Fix for fragile code in ltree/crc32.c
  2026-08-19 00:05 Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
@ 2026-08-19 07:44 ` Heikki Linnakangas <hlinnaka@iki.fi>
  2026-08-20 20:32   ` Re: Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Heikki Linnakangas @ 2026-08-19 07:44 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; pgsql-hackers

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







^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Fix for fragile code in ltree/crc32.c
  2026-08-19 00:05 Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
  2026-08-19 07:44 ` Re: Fix for fragile code in ltree/crc32.c Heikki Linnakangas <hlinnaka@iki.fi>
@ 2026-08-20 20:32   ` Jeff Davis <pgsql@j-davis.com>
  2026-08-22 06:04     ` Re: Fix for fragile code in ltree/crc32.c Rithvika Devisetti <devisettirithvika@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Jeff Davis @ 2026-08-20 20:32 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnaka@iki.fi>; pgsql-hackers

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:

https://www.postgresql.org/message-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: download all (2 files, .tar.gz)

v2-0001-ltree-crc32.c-fix-fragile-code.patch (text/x-patch, 1.5K) download From eb7a9414f0b8a8c6c64e7249b5ed4aab1177d633 Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Tue, 18 Aug 2026 14:57:32 -0700 Subject: [PATCH v2 1/2] ltree/crc32.c: fix fragile code. Explicitly make space for the NUL when casefolding. No known bug in the previous code, because the previous buffer (size 12) was more than large enough for folding any codepoint with enough room left for a NUL. The builtin provider's limit is 7; ICU's limit seems to be 7 also; and libc always does 1:1 mappings so the real limit is MAX_MULTIBYTE_CHAR_LEN + 1 (size 5). Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Discussion: https://postgr.es/m/2cfbc37ae8deccd3825e36b7f31c391cbf8ff9b8.camel@j-davis.com Backpatch-through: 18 --- contrib/ltree/crc32.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contrib/ltree/crc32.c b/contrib/ltree/crc32.c index d21bed31fdd..617cc652a7a 100644 --- a/contrib/ltree/crc32.c +++ b/contrib/ltree/crc32.c @@ -32,13 +32,13 @@ ltree_crc32_sz(const char *buf, int size) INIT_TRADITIONAL_CRC32(crc); while (size > 0) { - char foldstr[UNICODE_CASEMAP_BUFSZ]; + char foldstr[UNICODE_CASEMAP_BUFSZ + 1]; int srclen = pg_mblen_range(p, end); size_t foldlen; /* fold one codepoint at a time */ - foldlen = pg_strfold(foldstr, UNICODE_CASEMAP_BUFSZ, p, srclen, - locale); + foldlen = pg_strfold(foldstr, sizeof(foldstr), p, srclen, locale); + Assert(foldlen < sizeof(foldstr)); COMP_TRADITIONAL_CRC32(crc, foldstr, foldlen); -- 2.43.0
v2-0002-pg_locale.h-define-casemapping-limits.patch (text/x-patch, 4.5K) download From 5013ed3d59403d943aae471c1033f43976cfc11c Mon Sep 17 00:00:00 2001 From: Jeff Davis <jeff@j-davis.com> Date: Tue, 18 Aug 2026 14:57:32 -0700 Subject: [PATCH v2 2/2] pg_locale.h: define casemapping limits. Clearly define casemapping upper bounds that cover all providers, locales, and encodings in pg_locale.h. Also redefine UTF8_CASEMAP_BUFSZ to include room for a NUL for consistency with PG_CASEMAP_BUFSZ. Use PG_CASEMAP_BUFSZ for the static buffer in ltree/crc32.c. Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Discussion: https://postgr.es/m/2cfbc37ae8deccd3825e36b7f31c391cbf8ff9b8.camel@j-davis.com --- contrib/ltree/crc32.c | 2 +- .../unicode/generate-unicode_case_table.pl | 4 +-- src/include/common/unicode_limits.h | 4 +-- src/include/utils/pg_locale.h | 35 ++++++++++++++----- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/contrib/ltree/crc32.c b/contrib/ltree/crc32.c index 617cc652a7a..2082b9ef851 100644 --- a/contrib/ltree/crc32.c +++ b/contrib/ltree/crc32.c @@ -32,7 +32,7 @@ ltree_crc32_sz(const char *buf, int size) INIT_TRADITIONAL_CRC32(crc); while (size > 0) { - char foldstr[UNICODE_CASEMAP_BUFSZ + 1]; + char foldstr[PG_CASEMAP_BUFSZ]; int srclen = pg_mblen_range(p, end); size_t foldlen; diff --git a/src/common/unicode/generate-unicode_case_table.pl b/src/common/unicode/generate-unicode_case_table.pl index d1dd3a5e8f2..059fd1231cf 100644 --- a/src/common/unicode/generate-unicode_case_table.pl +++ b/src/common/unicode/generate-unicode_case_table.pl @@ -280,9 +280,9 @@ print $LIMITS <<"EOS"; /* * The maximum number of UTF8 bytes needed to store the result of case - * mapping a single code point. + * mapping a single code point, including terminating NUL. */ -#define UTF8_CASEMAP_BUFSZ $UTF8_CASEMAP_BUFSZ +#define UTF8_CASEMAP_BUFSZ ($UTF8_CASEMAP_BUFSZ + 1) #endif EOS diff --git a/src/include/common/unicode_limits.h b/src/include/common/unicode_limits.h index 59e307857ed..2307141c823 100644 --- a/src/include/common/unicode_limits.h +++ b/src/include/common/unicode_limits.h @@ -32,8 +32,8 @@ /* * The maximum number of UTF8 bytes needed to store the result of case - * mapping a single code point. + * mapping a single code point, including terminating NUL. */ -#define UTF8_CASEMAP_BUFSZ 6 +#define UTF8_CASEMAP_BUFSZ (6 + 1) #endif diff --git a/src/include/utils/pg_locale.h b/src/include/utils/pg_locale.h index fcd508f5dd6..6f82075c61d 100644 --- a/src/include/utils/pg_locale.h +++ b/src/include/utils/pg_locale.h @@ -13,23 +13,40 @@ #define _PG_LOCALE_ #include "mb/pg_wchar.h" +#include "common/unicode_limits.h" /* use for libc locale names */ #define LOCALE_NAME_BUFLEN 128 /* - * Maximum number of bytes needed to map a single codepoint. Useful for - * mapping and processing a single input codepoint at a time with a - * statically-allocated buffer. + * 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. * - * With full case mapping, an input codepoint may be mapped to as many as - * three output codepoints. See Unicode 16.0.0, section 5.18.2, "Change in - * Length": + * NB: assumes no provider exceeds the Unicode-defined maximum. + */ +#define PG_MAX_CASEMAP_MBCHARS UNICODE_MAX_CASEMAP_CODEPOINTS + +/* + * Expansion factor of a string in bytes, not including terminating NUL. + * + * This is a conservative upper bound, assuming that each character in the + * input string is a 1-byte character that maps to PG_MAX_CASEMAP_MBCHARS + * other characters, all requiring MAX_MULTIBYTE_CHAR_LEN bytes. + */ +#define PG_MAX_CASEMAP_EXPANSION (PG_MAX_CASEMAP_MBCHARS * \ + MAX_MULTIBYTE_CHAR_LEN) + +/* + * The maximum number of bytes needed to store the result of case mapping a + * single multibyte character, including terminating NUL. * - * https://www.unicode.org/versions/Unicode16.0.0/core-spec/chapter-5/#G29675 + * This is a conservative upper bound, assuming that a single multibyte + * character can expand into PG_MAX_CASEMAP_MBCHARS other multibyte + * characters, each requiring MAX_MULTIBYTE_CHAR_LEN bytes. */ -#define UNICODE_CASEMAP_LEN 3 -#define UNICODE_CASEMAP_BUFSZ (UNICODE_CASEMAP_LEN * MAX_MULTIBYTE_CHAR_LEN) +#define PG_CASEMAP_BUFSZ ((PG_MAX_CASEMAP_MBCHARS * \ + MAX_MULTIBYTE_CHAR_LEN) + 1) /* GUC settings */ extern PGDLLIMPORT char *locale_messages; -- 2.43.0
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: Fix for fragile code in ltree/crc32.c
  2026-08-19 00:05 Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
  2026-08-19 07:44 ` Re: Fix for fragile code in ltree/crc32.c Heikki Linnakangas <hlinnaka@iki.fi>
  2026-08-20 20:32   ` Re: Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
@ 2026-08-22 06:04     ` Rithvika Devisetti <devisettirithvika@gmail.com>
  2026-08-25 08:13       ` Re: Fix for fragile code in ltree/crc32.c Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 1 reply; 6+ messages in thread

From: Rithvika Devisetti @ 2026-08-22 06:04 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; +Cc: Heikki Linnakangas <hlinnaka@iki.fi>; pgsql-hackers

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:

> 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:
>
>
> https://www.postgresql.org/message-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
>
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Fix for fragile code in ltree/crc32.c
  2026-08-19 00:05 Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
  2026-08-19 07:44 ` Re: Fix for fragile code in ltree/crc32.c Heikki Linnakangas <hlinnaka@iki.fi>
  2026-08-20 20:32   ` Re: Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
  2026-08-22 06:04     ` Re: Fix for fragile code in ltree/crc32.c Rithvika Devisetti <devisettirithvika@gmail.com>
@ 2026-08-25 08:13       ` Heikki Linnakangas <hlinnaka@iki.fi>
  2026-08-25 08:15         ` Re: Fix for fragile code in ltree/crc32.c Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 1 reply; 6+ messages in thread

From: Heikki Linnakangas @ 2026-08-25 08:13 UTC (permalink / raw)
  To: Rithvika Devisetti <devisettirithvika@gmail.com>; Jeff Davis <pgsql@j-davis.com>; +Cc: pgsql-hackers

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






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Fix for fragile code in ltree/crc32.c
  2026-08-19 00:05 Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
  2026-08-19 07:44 ` Re: Fix for fragile code in ltree/crc32.c Heikki Linnakangas <hlinnaka@iki.fi>
  2026-08-20 20:32   ` Re: Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
  2026-08-22 06:04     ` Re: Fix for fragile code in ltree/crc32.c Rithvika Devisetti <devisettirithvika@gmail.com>
  2026-08-25 08:13       ` Re: Fix for fragile code in ltree/crc32.c Heikki Linnakangas <hlinnaka@iki.fi>
@ 2026-08-25 08:15         ` Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 0 replies; 6+ messages in thread

From: Heikki Linnakangas @ 2026-08-25 08:15 UTC (permalink / raw)
  To: Rithvika Devisetti <devisettirithvika@gmail.com>; Jeff Davis <pgsql@j-davis.com>; +Cc: pgsql-hackers

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







^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-08-25 08:15 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-19 00:05 Fix for fragile code in ltree/crc32.c Jeff Davis <pgsql@j-davis.com>
2026-08-19 07:44 ` Heikki Linnakangas <hlinnaka@iki.fi>
2026-08-20 20:32   ` Jeff Davis <pgsql@j-davis.com>
2026-08-22 06:04     ` Rithvika Devisetti <devisettirithvika@gmail.com>
2026-08-25 08:13       ` Heikki Linnakangas <hlinnaka@iki.fi>
2026-08-25 08:15         ` Heikki Linnakangas <hlinnaka@iki.fi>

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