pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
right() returns the whole string for the most negative n
14+ messages / 5 participants
[nested] [flat]

Commits from this thread:
  2026-09-01 729bac9eb1  Fix right() with the most negative integer
  2026-09-01 bfb08d0946 a63df26642 858fb27b69 2291b6ffbe 48af47dcdd 3103bdafde  Handle PG_INT32_MIN negation overflow in right()
* right() returns the whole string for the most negative n
@ 2026-08-25 06:57 Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 07:29 ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  0 siblings, 2 replies; 14+ messages in thread

From: Ewan Young @ 2026-08-25 06:57 UTC (permalink / raw)
  To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; +Cc: David Rowley <dgrowleyml@gmail.com>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

Hi Hackers,

right(text, int) gets one input wrong:

    SELECT right('abcdef', (-2147483648)::int4);   -- 'abcdef'
    SELECT right('abcdef', -2147483647);           -- '' (correct)
    SELECT right('abcdef', -3);                    -- 'def' (correct)

A negative n means "return all but the first |n| characters", so |n| that
big has to give an empty string.  Only this one value is affected, and
left() is fine.

text_right() negates n to get the number of characters to skip:

    if (n < 0)
        n = -n;
    else
        n = pg_mbstrlen_with_len(p, len) - n;
    off = pg_mbcharcliplen(p, len, n);

Negating PG_INT32_MIN overflows; under -fwrapv it comes back as
PG_INT32_MIN, still negative, and pg_mbcharcliplen() returns 0 for any
negative limit, so off is 0 and the whole string is returned.

The attached patch clamps to PG_INT32_MAX rather than negating.  Any n
whose absolute value reaches the string's length skips all of it, and a
text value can't be longer than PG_INT32_MAX, so the answer is unchanged
for every other input.  I did not make it an error, unlike
text_format_string_conversion() a few hundred lines down, which rejects a
width of INT_MIN (73e7025bd8e, complete with a "-INT_MIN is undefined"
comment): a format width has no sensible clamp, whereas an oversized skip
count does.

left() is not affected because its negative case adds n to the character
length instead of negating it, and since the length is non-negative and
bounded by the varlena size limit, that sum cannot overflow.

This dates to 49b27ab5514, which added left()/right() in 2010, and the
line has not been touched since; I could not find a previous report.  It's
the same shape as b4dfae2ffac (money, INT64_MIN / -1) from a few weeks
ago.

The patch adds the case to the existing left()/right() test in text.sql,
which currently covers only generate_series(-5, 5).  make check passes;
before adding the expected output I ran the suite deliberately and the
only difference was the new line.

-- 
Regards,
Ewan Young

Attachments:

v1-0001-Fix-right-with-the-most-negative-integer.patch (application/octet-stream, 3.5K) download From ccbcc7325d6096741b48f536c0eceea73e858ac1 Mon Sep 17 00:00:00 2001 From: Ewan Young <kdbase.hack@gmail.com> Date: Tue, 25 Aug 2026 22:43:10 +0800 Subject: [PATCH v1] Fix right() with the most negative integer A negative n means "return all but the first |n| characters", so text_right() negates n before clipping. Negating PG_INT32_MIN overflows; with -fwrapv the result is PG_INT32_MIN again, still negative, and pg_mbcharcliplen() then returns an offset of zero, so the whole string is returned where the correct answer is an empty string: SELECT right('abcdef', (-2147483648)::int4); -- 'abcdef', want '' SELECT right('abcdef', -2147483647); -- '', correct Clamp to PG_INT32_MAX instead. Any n whose absolute value is at least the string's length skips all of it, and a text value cannot be longer than PG_INT32_MAX, so this gives the same answer for every other input. Note that erroring out, as text_format_string_conversion() does for a width of INT_MIN a few hundred lines away, would not be right here: unlike a format width, an out-of-range skip count has a well-defined result. text_left() is not affected. Its negative case computes the character length plus n rather than negating n, and since the length is non-negative and bounded by the varlena size limit that sum cannot overflow. --- src/backend/utils/adt/varlena.c | 12 +++++++++++- src/test/regress/expected/text.out | 8 ++++++++ src/test/regress/sql/text.sql | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) 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; + } else n = pg_mbstrlen_with_len(p, len) - n; off = pg_mbcharcliplen(p, len, n); diff --git a/src/test/regress/expected/text.out b/src/test/regress/expected/text.out index 3f9982388ba..9ef23f4ddea 100644 --- a/src/test/regress/expected/text.out +++ b/src/test/regress/expected/text.out @@ -118,6 +118,14 @@ select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) ord 5 | ahoj | ahoj (11 rows) +-- the most negative value must skip the whole string, same as any other n +-- whose absolute value exceeds its length +select left('ahoj', (-2147483648)::int4), right('ahoj', (-2147483648)::int4); + left | right +------+------- + | +(1 row) + select quote_literal(''); quote_literal --------------- diff --git a/src/test/regress/sql/text.sql b/src/test/regress/sql/text.sql index 540e551254d..3a9be863bb7 100644 --- a/src/test/regress/sql/text.sql +++ b/src/test/regress/sql/text.sql @@ -37,6 +37,9 @@ select concat_ws('',10,20,null,30); select concat_ws(NULL,10,20,null,30) is null; select reverse('abcde'); select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i; +-- the most negative value must skip the whole string, same as any other n +-- whose absolute value exceeds its length +select left('ahoj', (-2147483648)::int4), right('ahoj', (-2147483648)::int4); select quote_literal(''); select quote_literal('abc'''); select quote_literal(e'\\'); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
@ 2026-08-25 07:29 ` Daniel Gustafsson <daniel@yesql.se>
  1 sibling, 0 replies; 14+ messages in thread

From: Daniel Gustafsson @ 2026-08-25 07:29 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; David Rowley <dgrowleyml@gmail.com>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

> On 25 Aug 2026, at 08:57, Ewan Young <kdbase.hack@gmail.com> wrote:

> Negating PG_INT32_MIN overflows; under -fwrapv it comes back as
> PG_INT32_MIN, still negative, and pg_mbcharcliplen() returns 0 for any
> negative limit, so off is 0 and the whole string is returned.

Thanks for the report and patch, the fix seems correct to me.

> This dates to 49b27ab5514, which added left()/right() in 2010, and the
> line has not been touched since; I could not find a previous report.  

Understandably so, it's not an issue likely to cause issues in production.

> It's the same shape as b4dfae2ffac (money, INT64_MIN / -1) from a few weeks
> ago.

Agreed, so backpatching all the way for this one as well makes sense.  I'll
have another look and will leave it open to input from others before applying.

--
Daniel Gustafsson







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

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
@ 2026-08-25 12:18 ` Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-25 12:33   ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
  2026-08-26 01:52   ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  1 sibling, 2 replies; 14+ messages in thread

From: Dagfinn Ilmari Mannsåker @ 2026-08-25 12:18 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; David Rowley <dgrowleyml@gmail.com>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

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;

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.

- ilmari





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

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
@ 2026-08-25 12:33   ` Daniel Gustafsson <daniel@yesql.se>
  2026-08-26 12:55     ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
  1 sibling, 1 reply; 14+ messages in thread

From: Daniel Gustafsson @ 2026-08-25 12:33 UTC (permalink / raw)
  To: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; +Cc: Ewan Young <kdbase.hack@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; David Rowley <dgrowleyml@gmail.com>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

> On 25 Aug 2026, at 14:18, Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> wrote:
> 
> Instead of open-coding this, how about about using pg_neg_s32_overflow?
> 
> if (pg_neg_s32_overflow(n, &n))
> n = PG_INT32_MAX;

Yes, that's indeed a good idea.

--
Daniel Gustafsson







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

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-25 12:33   ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
@ 2026-08-26 12:55     ` David Rowley <dgrowleyml@gmail.com>
  2026-08-27 02:26       ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: David Rowley @ 2026-08-26 12:55 UTC (permalink / raw)
  To: Daniel Gustafsson <daniel@yesql.se>; +Cc: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; Ewan Young <kdbase.hack@gmail.com>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

On Wed, 26 Aug 2026 at 00:33, Daniel Gustafsson <daniel@yesql.se> wrote:
>
> > On 25 Aug 2026, at 14:18, Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> wrote:
> >
> > Instead of open-coding this, how about about using pg_neg_s32_overflow?
> >
> > if (pg_neg_s32_overflow(n, &n))
> > n = PG_INT32_MAX;
>
> Yes, that's indeed a good idea.

I do see a few places where we do check for PG_INT32_MIN instead of
using pg_neg_s32_overflow(). The example in [1] does end up with less
code as a result of using pg_neg_s32_overflow(), so it might be worth
removing all applicable examples that use the other method from master
as a follow-up. It'd be worth verifying it doesn't make anything worse
for build systems that don't have __builtin_sub_overflow().

David

[1] https://godbolt.org/z/sfxY847E4






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

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-25 12:33   ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
  2026-08-26 12:55     ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
@ 2026-08-27 02:26       ` Ewan Young <kdbase.hack@gmail.com>
  2026-08-27 02:45         ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Ewan Young @ 2026-08-27 02:26 UTC (permalink / raw)
  To: David Rowley <dgrowleyml@gmail.com>; +Cc: Daniel Gustafsson <daniel@yesql.se>; Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

Hi David,

Thanks for the review!


On Wed, Aug 26, 2026 at 8:55 PM David Rowley <dgrowleyml@gmail.com> wrote:
>
> On Wed, 26 Aug 2026 at 00:33, Daniel Gustafsson <daniel@yesql.se> wrote:
> >
> > > On 25 Aug 2026, at 14:18, Dagfinn Ilmari Mannsåker <ilmari@ilmari.org> wrote:
> > >
> > > Instead of open-coding this, how about about using pg_neg_s32_overflow?
> > >
> > > if (pg_neg_s32_overflow(n, &n))
> > > n = PG_INT32_MAX;
> >
> > Yes, that's indeed a good idea.
>
> I do see a few places where we do check for PG_INT32_MIN instead of
> using pg_neg_s32_overflow(). The example in [1] does end up with less
> code as a result of using pg_neg_s32_overflow(), so it might be worth
> removing all applicable examples that use the other method from master
> as a follow-up. It'd be worth verifying it doesn't make anything worse
> for build systems that don't have __builtin_sub_overflow().

I verified that converting them is safe on platforms without
__builtin_sub_overflow() -- there, pg_neg_s{16,32,64}_overflow() inline to
exactly the same "if (a == PG_INTnn_MIN) ...; *result = -a;" code that is
being replaced, so the generated code is unchanged (I confirmed the
fallback path is bit-for-bit equivalent to the manual form, including at
the INT_MIN boundary).

Rather than pile that cleanup onto this bug-fix thread, I'll send it as a
separate follow-up so this fix can be considered on its own.  Will post
that shortly and CC you three.

>
> David
>
> [1] https://godbolt.org/z/sfxY847E4



-- 
Regards,
Ewan Young





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

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-25 12:33   ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
  2026-08-26 12:55     ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
  2026-08-27 02:26       ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
@ 2026-08-27 02:45         ` David Rowley <dgrowleyml@gmail.com>
  2026-08-27 05:17           ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: David Rowley @ 2026-08-27 02:45 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; +Cc: Daniel Gustafsson <daniel@yesql.se>; Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

On Thu, 27 Aug 2026 at 14:27, Ewan Young <kdbase.hack@gmail.com> wrote:
> Rather than pile that cleanup onto this bug-fix thread, I'll send it as a
> separate follow-up so this fix can be considered on its own.  Will post
> that shortly and CC you three.

Yes, another thread. Please also include a description of your method
for verifying the code matches for non-HAVE__BUILTIN_OP_OVERFLOW
builds.

Also, pg_neg_s32_overflow() was introduced in 2024 by 0a27c3d0f733,
which is v18+. Depending on Daniel's patience for backpatching two
different versions, it might be worth just using the == PG_INT32_MIN
method to fix the bug, then allowing the follow-up patch to switch
that in v20 only.  That would mean v18 and v19 don't get the most
optimal fix, but it's hard to imagine that's going to be performance
critical.

David






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

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-25 12:33   ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
  2026-08-26 12:55     ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
  2026-08-27 02:26       ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-27 02:45         ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
@ 2026-08-27 05:17           ` Ewan Young <kdbase.hack@gmail.com>
  2026-08-27 11:13             ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 1 reply; 14+ messages in thread

From: Ewan Young @ 2026-08-27 05:17 UTC (permalink / raw)
  To: David Rowley <dgrowleyml@gmail.com>; +Cc: Daniel Gustafsson <daniel@yesql.se>; Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

On Thu, Aug 27, 2026 at 10:45 AM David Rowley <dgrowleyml@gmail.com> wrote:
>
> On Thu, 27 Aug 2026 at 14:27, Ewan Young <kdbase.hack@gmail.com> wrote:
> > Rather than pile that cleanup onto this bug-fix thread, I'll send it as a
> > separate follow-up so this fix can be considered on its own.  Will post
> > that shortly and CC you three.
>
> Yes, another thread. Please also include a description of your method
> for verifying the code matches for non-HAVE__BUILTIN_OP_OVERFLOW
> builds.
>
> Also, pg_neg_s32_overflow() was introduced in 2024 by 0a27c3d0f733,
> which is v18+. Depending on Daniel's patience for backpatching two
> different versions, it might be worth just using the == PG_INT32_MIN
> method to fix the bug, then allowing the follow-up patch to switch
> that in v20 only.  That would mean v18 and v19 don't get the most
> optimal fix, but it's hard to imagine that's going to be performance
> critical.

Thanks, that plan makes sense to me -- let's keep the bug fix uniform
across all branches and do the pg_neg_s32_overflow() switch separately.

pg_neg_s32_overflow() only exists from v18 (0a27c3d0f733), while the bug
goes all the way back, so using it in the fix would mean carrying two
versions for the back-branches.  Not worth it for a one-liner.

Attached v4 goes back to the explicit PG_INT32_MIN test:

    if (unlikely(n == PG_INT32_MIN))
        n = PG_INT32_MAX;
    else
        n = -n;

which applies cleanly to every supported branch (I checked REL_14 through
REL_19 and master).  The fix and the regression case are otherwise
unchanged.

I'll take the pg_neg_s32_overflow() cleanup to its own thread, master
only, covering the other open-coded negation checks Ilmari pointed at
(int2/4/8 um/abs/div, lcm, cash, numericvar_to_int64); once this fix
lands I'll fold text_right() into that too.  So the back-branches keep the
plain form and only master gets the helper.

>
> David



-- 
Regards,
Ewan Young

Attachments:

v4-0001-Fix-right-with-the-most-negative-integer.patch (application/octet-stream, 3.5K) download From 4ae07928a849d47fce37e1388f319e6bbefc9b83 Mon Sep 17 00:00:00 2001 From: Ewan Young <kdbase.hack@gmail.com> Date: Tue, 25 Aug 2026 22:43:10 +0800 Subject: [PATCH] Fix right() with the most negative integer A negative n means "return all but the first |n| characters", so text_right() negates n before clipping. Negating PG_INT32_MIN overflows; with -fwrapv the result is PG_INT32_MIN again, still negative, and pg_mbcharcliplen() then returns an offset of zero, so the whole string is returned where the correct answer is an empty string: SELECT right('abcdef', (-2147483648)::int4); -- 'abcdef', want '' SELECT right('abcdef', -2147483647); -- '', correct Clamp to PG_INT32_MAX instead. Any n whose absolute value is at least the string's length skips all of it, and a text value cannot be longer than PG_INT32_MAX, so this gives the same answer for every other input. Note that erroring out, as text_format_string_conversion() does for a width of INT_MIN a few hundred lines away, would not be right here: unlike a format width, an out-of-range skip count has a well-defined result. text_left() is not affected. Its negative case computes the character length plus n rather than negating n, and since the length is non-negative and bounded by the varlena size limit that sum cannot overflow. --- src/backend/utils/adt/varlena.c | 12 +++++++++++- src/test/regress/expected/text.out | 8 ++++++++ src/test/regress/sql/text.sql | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) 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; + } else n = pg_mbstrlen_with_len(p, len) - n; off = pg_mbcharcliplen(p, len, n); diff --git a/src/test/regress/expected/text.out b/src/test/regress/expected/text.out index 3f9982388ba..9ef23f4ddea 100644 --- a/src/test/regress/expected/text.out +++ b/src/test/regress/expected/text.out @@ -118,6 +118,14 @@ select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) ord 5 | ahoj | ahoj (11 rows) +-- the most negative value must skip the whole string, same as any other n +-- whose absolute value exceeds its length +select left('ahoj', (-2147483648)::int4), right('ahoj', (-2147483648)::int4); + left | right +------+------- + | +(1 row) + select quote_literal(''); quote_literal --------------- diff --git a/src/test/regress/sql/text.sql b/src/test/regress/sql/text.sql index 540e551254d..3a9be863bb7 100644 --- a/src/test/regress/sql/text.sql +++ b/src/test/regress/sql/text.sql @@ -37,6 +37,9 @@ select concat_ws('',10,20,null,30); select concat_ws(NULL,10,20,null,30) is null; select reverse('abcde'); select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i; +-- the most negative value must skip the whole string, same as any other n +-- whose absolute value exceeds its length +select left('ahoj', (-2147483648)::int4), right('ahoj', (-2147483648)::int4); select quote_literal(''); select quote_literal('abc'''); select quote_literal(e'\\'); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-25 12:33   ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
  2026-08-26 12:55     ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
  2026-08-27 02:26       ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-27 02:45         ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
  2026-08-27 05:17           ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
@ 2026-08-27 11:13             ` Daniel Gustafsson <daniel@yesql.se>
  2026-09-01 08:23               ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 1 reply; 14+ messages in thread

From: Daniel Gustafsson @ 2026-08-27 11:13 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; +Cc: David Rowley <dgrowleyml@gmail.com>; Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

> On 27 Aug 2026, at 07:17, Ewan Young <kdbase.hack@gmail.com> wrote:
> On Thu, Aug 27, 2026 at 10:45 AM David Rowley <dgrowleyml@gmail.com> wrote:

>> Also, pg_neg_s32_overflow() was introduced in 2024 by 0a27c3d0f733,
>> which is v18+. Depending on Daniel's patience for backpatching two
>> different versions, it might be worth just using the == PG_INT32_MIN
>> method to fix the bug, then allowing the follow-up patch to switch
>> that in v20 only.  That would mean v18 and v19 don't get the most
>> optimal fix, but it's hard to imagine that's going to be performance
>> critical.
> 
> Thanks, that plan makes sense to me -- let's keep the bug fix uniform
> across all branches and do the pg_neg_s32_overflow() switch separately.

FWIW, I agree with this plan.

> Attached v4 goes back to the explicit PG_INT32_MIN test:
> 
>    if (unlikely(n == PG_INT32_MIN))
>        n = PG_INT32_MAX;
>    else
>        n = -n;
> 
> which applies cleanly to every supported branch (I checked REL_14 through
> REL_19 and master).  The fix and the regression case are otherwise
> unchanged.

I will prepare a backpatch all the way of this, and will ping the new thread
when done.  (There is a lof work being done for shipping v19 so have a little
patience.)

> I'll take the pg_neg_s32_overflow() cleanup to its own thread, master
> only, covering the other open-coded negation checks Ilmari pointed at
> (int2/4/8 um/abs/div, lcm, cash, numericvar_to_int64); once this fix
> lands I'll fold text_right() into that too.  So the back-branches keep the
> plain form and only master gets the helper.

+1

--
Daniel Gustafsson







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

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-25 12:33   ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
  2026-08-26 12:55     ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
  2026-08-27 02:26       ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-27 02:45         ` Re: right() returns the whole string for the most negative n David Rowley <dgrowleyml@gmail.com>
  2026-08-27 05:17           ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-27 11:13             ` Re: right() returns the whole string for the most negative n Daniel Gustafsson <daniel@yesql.se>
@ 2026-09-01 08:23               ` Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 0 replies; 14+ messages in thread

From: Daniel Gustafsson @ 2026-09-01 08:23 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; +Cc: David Rowley <dgrowleyml@gmail.com>; Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>

> On 27 Aug 2026, at 13:13, Daniel Gustafsson <daniel@yesql.se> wrote:
>> On 27 Aug 2026, at 07:17, Ewan Young <kdbase.hack@gmail.com> wrote:
>> On Thu, Aug 27, 2026 at 10:45 AM David Rowley <dgrowleyml@gmail.com> wrote:

>> Attached v4 goes back to the explicit PG_INT32_MIN test:
>> 
>>   if (unlikely(n == PG_INT32_MIN))
>>       n = PG_INT32_MAX;
>>   else
>>       n = -n;
>> 
>> which applies cleanly to every supported branch (I checked REL_14 through
>> REL_19 and master).  The fix and the regression case are otherwise
>> unchanged.
> 
> I will prepare a backpatch all the way of this, and will ping the new thread
> when done.  (There is a lof work being done for shipping v19 so have a little
> patience.)

Finally got around to backpatching this to all supported branches.  Thanks for
the submission!

--
Daniel Gustafsson







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

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
@ 2026-08-26 01:52   ` Ewan Young <kdbase.hack@gmail.com>
  2026-08-26 05:57     ` Re: right() returns the whole string for the most negative n Chao Li <li.evan.chao@gmail.com>
  1 sibling, 1 reply; 14+ messages in thread

From: Ewan Young @ 2026-08-26 01:52 UTC (permalink / raw)
  To: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; +Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; David Rowley <dgrowleyml@gmail.com>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Daniel Gustafsson <daniel@yesql.se>

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

Attachments:

v2-0001-Fix-right-with-the-most-negative-integer.patch (application/octet-stream, 3.5K) download From dbf5b844cdbb8161abf1065c84c491046111bd82 Mon Sep 17 00:00:00 2001 From: Ewan Young <kdbase.hack@gmail.com> Date: Tue, 25 Aug 2026 22:43:10 +0800 Subject: [PATCH v2] Fix right() with the most negative integer A negative n means "return all but the first |n| characters", so text_right() negates n before clipping. Negating PG_INT32_MIN overflows; with -fwrapv the result is PG_INT32_MIN again, still negative, and pg_mbcharcliplen() then returns an offset of zero, so the whole string is returned where the correct answer is an empty string: SELECT right('abcdef', (-2147483648)::int4); -- 'abcdef', want '' SELECT right('abcdef', -2147483647); -- '', correct Clamp to PG_INT32_MAX instead. Any n whose absolute value is at least the string's length skips all of it, and a text value cannot be longer than PG_INT32_MAX, so this gives the same answer for every other input. Note that erroring out, as text_format_string_conversion() does for a width of INT_MIN a few hundred lines away, would not be right here: unlike a format width, an out-of-range skip count has a well-defined result. text_left() is not affected. Its negative case computes the character length plus n rather than negating n, and since the length is non-negative and bounded by the varlena size limit that sum cannot overflow. --- src/backend/utils/adt/varlena.c | 10 +++++++++- src/test/regress/expected/text.out | 8 ++++++++ src/test/regress/sql/text.sql | 3 +++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index a09a9e5d5bb..fa96320edae 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -4714,7 +4714,15 @@ 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 (pg_neg_s32_overflow(n, &n)) + n = PG_INT32_MAX; + } else n = pg_mbstrlen_with_len(p, len) - n; off = pg_mbcharcliplen(p, len, n); diff --git a/src/test/regress/expected/text.out b/src/test/regress/expected/text.out index 3f9982388ba..9ef23f4ddea 100644 --- a/src/test/regress/expected/text.out +++ b/src/test/regress/expected/text.out @@ -118,6 +118,14 @@ select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) ord 5 | ahoj | ahoj (11 rows) +-- the most negative value must skip the whole string, same as any other n +-- whose absolute value exceeds its length +select left('ahoj', (-2147483648)::int4), right('ahoj', (-2147483648)::int4); + left | right +------+------- + | +(1 row) + select quote_literal(''); quote_literal --------------- diff --git a/src/test/regress/sql/text.sql b/src/test/regress/sql/text.sql index 540e551254d..3a9be863bb7 100644 --- a/src/test/regress/sql/text.sql +++ b/src/test/regress/sql/text.sql @@ -37,6 +37,9 @@ select concat_ws('',10,20,null,30); select concat_ws(NULL,10,20,null,30) is null; select reverse('abcde'); select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i; +-- the most negative value must skip the whole string, same as any other n +-- whose absolute value exceeds its length +select left('ahoj', (-2147483648)::int4), right('ahoj', (-2147483648)::int4); select quote_literal(''); select quote_literal('abc'''); select quote_literal(e'\\'); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-26 01:52   ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
@ 2026-08-26 05:57     ` Chao Li <li.evan.chao@gmail.com>
  2026-08-26 06:29       ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Chao Li @ 2026-08-26 05:57 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; +Cc: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; David Rowley <dgrowleyml@gmail.com>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Daniel Gustafsson <daniel@yesql.se>



> 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?

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










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

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-26 01:52   ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-26 05:57     ` Re: right() returns the whole string for the most negative n Chao Li <li.evan.chao@gmail.com>
@ 2026-08-26 06:29       ` Ewan Young <kdbase.hack@gmail.com>
  2026-08-26 07:37         ` Re: right() returns the whole string for the most negative n Chao Li <li.evan.chao@gmail.com>
  0 siblings, 1 reply; 14+ messages in thread

From: Ewan Young @ 2026-08-26 06:29 UTC (permalink / raw)
  To: Chao Li <li.evan.chao@gmail.com>; +Cc: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; David Rowley <dgrowleyml@gmail.com>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Daniel Gustafsson <daniel@yesql.se>

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.

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


-- 
Regards,
Ewan Young

Attachments:

v3-0001-Fix-right-with-the-most-negative-integer.patch (application/octet-stream, 3.6K) download From fef576fba4531a16e84687e58504bba72c8d55fa Mon Sep 17 00:00:00 2001 From: Ewan Young <kdbase.hack@gmail.com> Date: Tue, 25 Aug 2026 22:43:10 +0800 Subject: [PATCH v3] Fix right() with the most negative integer A negative n means "return all but the first |n| characters", so text_right() negates n before clipping. Negating PG_INT32_MIN overflows; with -fwrapv the result is PG_INT32_MIN again, still negative, and pg_mbcharcliplen() then returns an offset of zero, so the whole string is returned where the correct answer is an empty string: SELECT right('abcdef', (-2147483648)::int4); -- 'abcdef', want '' SELECT right('abcdef', -2147483647); -- '', correct Clamp to PG_INT32_MAX instead. Any n whose absolute value is at least the string's length skips all of it, and a text value cannot be longer than PG_INT32_MAX, so this gives the same answer for every other input. Note that erroring out, as text_format_string_conversion() does for a width of INT_MIN a few hundred lines away, would not be right here: unlike a format width, an out-of-range skip count has a well-defined result. text_left() is not affected. Its negative case computes the character length plus n rather than negating n, and since the length is non-negative and bounded by the varlena size limit that sum cannot overflow. --- src/backend/utils/adt/varlena.c | 12 +++++++++++- src/test/regress/expected/text.out | 8 ++++++++ src/test/regress/sql/text.sql | 3 +++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index a09a9e5d5bb..6d69a9d903c 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; + { + /* + * Negate n to get the number of characters to skip. When that would + * overflow (the most negative n), clamp to PG_INT32_MAX 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; + } else n = pg_mbstrlen_with_len(p, len) - n; off = pg_mbcharcliplen(p, len, n); diff --git a/src/test/regress/expected/text.out b/src/test/regress/expected/text.out index 3f9982388ba..9ef23f4ddea 100644 --- a/src/test/regress/expected/text.out +++ b/src/test/regress/expected/text.out @@ -118,6 +118,14 @@ select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) ord 5 | ahoj | ahoj (11 rows) +-- the most negative value must skip the whole string, same as any other n +-- whose absolute value exceeds its length +select left('ahoj', (-2147483648)::int4), right('ahoj', (-2147483648)::int4); + left | right +------+------- + | +(1 row) + select quote_literal(''); quote_literal --------------- diff --git a/src/test/regress/sql/text.sql b/src/test/regress/sql/text.sql index 540e551254d..3a9be863bb7 100644 --- a/src/test/regress/sql/text.sql +++ b/src/test/regress/sql/text.sql @@ -37,6 +37,9 @@ select concat_ws('',10,20,null,30); select concat_ws(NULL,10,20,null,30) is null; select reverse('abcde'); select i, left('ahoj', i), right('ahoj', i) from generate_series(-5, 5) t(i) order by i; +-- the most negative value must skip the whole string, same as any other n +-- whose absolute value exceeds its length +select left('ahoj', (-2147483648)::int4), right('ahoj', (-2147483648)::int4); select quote_literal(''); select quote_literal('abc'''); select quote_literal(e'\\'); -- 2.47.3
^ permalink raw reply [nested|flat] 14+ messages in thread

* Re: right() returns the whole string for the most negative n
  2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-25 12:18 ` Re: right() returns the whole string for the most negative n Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
  2026-08-26 01:52   ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
  2026-08-26 05:57     ` Re: right() returns the whole string for the most negative n Chao Li <li.evan.chao@gmail.com>
  2026-08-26 06:29       ` Re: right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
@ 2026-08-26 07:37         ` Chao Li <li.evan.chao@gmail.com>
  0 siblings, 0 replies; 14+ messages in thread

From: Chao Li @ 2026-08-26 07:37 UTC (permalink / raw)
  To: Ewan Young <kdbase.hack@gmail.com>; +Cc: Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>; PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>; David Rowley <dgrowleyml@gmail.com>; Andrey Rachitskiy <pl0h0yp1@gmail.com>; Tom Lane <tgl@sss.pgh.pa.us>; Daniel Gustafsson <daniel@yesql.se>



> 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/










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


end of thread, other threads:[~2026-09-01 08:23 UTC | newest]

Thread overview: 14+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-08-25 06:57 right() returns the whole string for the most negative n Ewan Young <kdbase.hack@gmail.com>
2026-08-25 07:29 ` Daniel Gustafsson <daniel@yesql.se>
2026-08-25 12:18 ` Dagfinn Ilmari Mannsåker <ilmari@ilmari.org>
2026-08-25 12:33   ` Daniel Gustafsson <daniel@yesql.se>
2026-08-26 12:55     ` David Rowley <dgrowleyml@gmail.com>
2026-08-27 02:26       ` Ewan Young <kdbase.hack@gmail.com>
2026-08-27 02:45         ` David Rowley <dgrowleyml@gmail.com>
2026-08-27 05:17           ` Ewan Young <kdbase.hack@gmail.com>
2026-08-27 11:13             ` Daniel Gustafsson <daniel@yesql.se>
2026-09-01 08:23               ` Daniel Gustafsson <daniel@yesql.se>
2026-08-26 01:52   ` Ewan Young <kdbase.hack@gmail.com>
2026-08-26 05:57     ` Chao Li <li.evan.chao@gmail.com>
2026-08-26 06:29       ` Ewan Young <kdbase.hack@gmail.com>
2026-08-26 07:37         ` Chao Li <li.evan.chao@gmail.com>

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