Re: BUG #19341: REPLACE() fails to match final character when using nondeterministic ICU collation

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Heikki Linnakangas <hlinnaka(at)iki(dot)fi>, adam(dot)warland(at)infor(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #19341: REPLACE() fails to match final character when using nondeterministic ICU collation
Date: 2025-12-03 07:51:22
Message-ID: 0bbe14779f2c37e5a2fbd514861ebedaf8f53df6.camel@cybertec.at
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Tue, 2025-12-02 at 15:53 -0500, Tom Lane wrote:
> > The attached patch v3 turns it into a while loop to avoid
> > the problem.
>
> Looking at the code overall, I wonder if the outer loop doesn't have
> the same issue.  The comments claim that we should be able to handle
> zero-length matches, but if the overall haystack is of length zero,
> we will fail to check for such a match.

If you can find zero-length matches at all, you could find a
zero-length match in a non-empty haystack. Perhaps the function is
never called with an empty haystack...

> Also, since we have haystack <= haystack_end as a starting condition,
> I think both loops could omit the initial test.  I'd be inclined
> to code them like
>
> test_ptr = start point;
> for (;;)
> {
> ...
> if (test_ptr >= haystack_end)
> break;
> test_ptr += pg_mblen(test_ptr);
> }

True. The attached v4 patch does it like that.

> On the other hand ... is that comment really right about zero-length
> match being possible?  If it is, the API for this function is in
> need of redesign, because callers that try to find "the next match"
> would go into an infinite loop re-finding the same zero-length
> match over and over.

Right. I'll see if I can trigger such a case.

Yours,
Laurenz Albe

Attachment Content-Type Size
v4-0001-Fix-greedy-substring-search-for-non-deterministic.patch text/x-patch 3.5 KB

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Heikki Linnakangas 2025-12-03 09:13:32 Re: BUG #19343: toast_internals.c:139:2: warning: missing braces around initializer [-Wmissing-braces]
Previous Message Tom Lane 2025-12-03 01:27:56 Re: BUG #19340: Wrong result from CORR() function