pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Chao Li <li.evan.chao@gmail.com>
To: Ewan Young <kdbase.hack@gmail.com>
Cc: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Cc: David Rowley <dgrowleyml@gmail.com>
Cc: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Daniel Gustafsson <daniel@yesql.se>
Subject: Re: right() returns the whole string for the most negative n
Date: Wed, 26 Aug 2026 15:37:36 +0800
Message-ID: <77EDC0CB-4BA9-4E14-AC90-D92D0EAD58F6@gmail.com> (raw)
In-Reply-To: <CAON2xHPctN75jjiAau-YGWN_baeq5f_rzOZMkfjqTJOdUeHZcA@mail.gmail.com>
References: <CAON2xHNnBz-AcPJgDmd5_39+8qR5AUKEZk4X3ZM-0zdsATn8kQ@mail.gmail.com>
	<87bjaqz946.fsf@wibble.ilmari.org>
	<CAON2xHOyMfo0ukXCx6s7_PZrR5_KWrUdQAmutEFpxBMDUhgChg@mail.gmail.com>
	<45315BCF-2D6E-4AE9-A62E-668E5A12C66B@gmail.com>
	<CAON2xHPctN75jjiAau-YGWN_baeq5f_rzOZMkfjqTJOdUeHZcA@mail.gmail.com>



> On Aug 26, 2026, at 14:29, Ewan Young <kdbase.hack@gmail.com> wrote:
> 
> Hi
> 
> On Wed, Aug 26, 2026 at 1:58 PM Chao Li <li.evan.chao@gmail.com> wrote:
>> 
>> 
>> 
>>> On Aug 26, 2026, at 09:52, Ewan Young <kdbase.hack@gmail.com> wrote:
>>> 
>>> On Tue, Aug 25, 2026 at 8:18 PM Dagfinn Ilmari Mannsåker
>>> <ilmari@ilmari.org> wrote:
>>>> 
>>>> Ewan Young <kdbase.hack@gmail.com> writes:
>>>> 
>>>>> diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
>>>>> index a09a9e5d5bb..3117069cf1a 100644
>>>>> --- a/src/backend/utils/adt/varlena.c
>>>>> +++ b/src/backend/utils/adt/varlena.c
>>>>> @@ -4714,7 +4714,17 @@ text_right(PG_FUNCTION_ARGS)
>>>>>     int                     off;
>>>>> 
>>>>>     if (n < 0)
>>>>> -             n = -n;
>>>>> +     {
>>>>> +             /*
>>>>> +              * Negating PG_INT32_MIN would overflow, so clamp instead.  Any n whose
>>>>> +              * absolute value is at least the string's length skips the whole
>>>>> +              * string, and len can't exceed PG_INT32_MAX, so this is equivalent.
>>>>> +              */
>>>>> +             if (unlikely(n == PG_INT32_MIN))
>>>>> +                     n = PG_INT32_MAX;
>>>>> +             else
>>>>> +                     n = -n;
>>>>> +     }
>>>> 
>>>> Instead of open-coding this, how about about using pg_neg_s32_overflow?
>>>> 
>>>>       if (pg_neg_s32_overflow(n, &n))
>>>>               n = PG_INT32_MAX;
>>>> 
>>> 
>>> Much nicer, thanks - done in v2.  varlena.c already includes common/int.h,
>>> so no new header was needed.
>>> 
>>>> This made me think we might want saturating versions of the
>>>> pg_*_overflow functions, but some quick grepping doesn't reveal any
>>>> other places using pg_*_overflow do it manually, so that feels like
>>>> premature generalisation.
>>> 
>>> Agreed, I left it as the two-liner.
>>> 
>>> Behaviour and tests are unchanged from v1: right('abcdef', INT32_MIN) now
>>> returns '', the adjacent values and left() are untouched, and make check
>>> passes.
>>> 
>>>> 
>>>> - ilmari
>>> 
>>> 
>>> 
>>> --
>>> Regards,
>>> Ewan Young
>>> <v2-0001-Fix-right-with-the-most-negative-integer.patch>
>> 
>> ```
>> +               /*
>> +                * Negating PG_INT32_MIN would overflow, so clamp instead.  Any n whose
>> +                * absolute value is at least the string's length skips the whole
>> +                * string, and len can't exceed PG_INT32_MAX, so this is equivalent.
>> +                */
>> +               if (pg_neg_s32_overflow(n, &n))
>> +                       n = PG_INT32_MAX;
>> ```
>> 
>> I think using pg_neg_s32_overflow() is clearer. Shall we also update the comment, since PG_INT32_MIN is no longer explicitly referenced in this code?
> 
> Good point — done. Reworded the comment in v3 to describe the overflow
> case generically; no other changes from v2. Patch attached.
> 
> Thanks for the review.
> 
> -- 
> Regards,
> Ewan Young
> <v3-0001-Fix-right-with-the-most-negative-integer.patch>

Thanks for updating the patch. V3 LGTM.

Best regards,
--
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/










view thread (14+ messages)  latest in thread

Message-ID: <77EDC0CB-4BA9-4E14-AC90-D92D0EAD58F6@gmail.com>
Permalink:  ../77EDC0CB-4BA9-4E14-AC90-D92D0EAD58F6@gmail.com/
Also on:    postgresql.org/message-id/77EDC0CB-4BA9-4E14-AC90-D92D0EAD58F6@gmail.com

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: li.evan.chao@gmail.com, kdbase.hack@gmail.com, ilmari@ilmari.org, dgrowleyml@gmail.com, pl0h0yp1@gmail.com, tgl@sss.pgh.pa.us, daniel@yesql.se
  Subject: Re: right() returns the whole string for the most negative n
  In-Reply-To: <77EDC0CB-4BA9-4E14-AC90-D92D0EAD58F6@gmail.com>
  References: <CAON2xHNnBz-AcPJgDmd5_39+8qR5AUKEZk4X3ZM-0zdsATn8kQ@mail.gmail.com> <87bjaqz946.fsf@wibble.ilmari.org> <CAON2xHOyMfo0ukXCx6s7_PZrR5_KWrUdQAmutEFpxBMDUhgChg@mail.gmail.com> <45315BCF-2D6E-4AE9-A62E-668E5A12C66B@gmail.com> <CAON2xHPctN75jjiAau-YGWN_baeq5f_rzOZMkfjqTJOdUeHZcA@mail.gmail.com> <77EDC0CB-4BA9-4E14-AC90-D92D0EAD58F6@gmail.com>

* 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