| From: | Dagfinn Ilmari Mannsåker <ilmari(at)ilmari(dot)org> |
|---|---|
| To: | Ewan Young <kdbase(dot)hack(at)gmail(dot)com> |
| Cc: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, David Rowley <dgrowleyml(at)gmail(dot)com>, Andrey Rachitskiy <pl0h0yp1(at)gmail(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> |
| Subject: | Re: right() returns the whole string for the most negative n |
| Date: | 2026-08-25 12:18:33 |
| Message-ID: | 87bjaqz946.fsf@wibble.ilmari.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Ewan Young <kdbase(dot)hack(at)gmail(dot)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
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Sehrope Sarkuni | 2026-08-25 12:27:54 | Re: Replace px_memset() with explicit_bzero() |
| Previous Message | Peter Eisentraut | 2026-08-25 12:14:44 | Re: fix more casting away of qualifiers |