Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wyq71-003evj-3A for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Aug 2026 12:18:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wyq70-007mJb-2F for pgsql-hackers@arkaria.postgresql.org; Tue, 25 Aug 2026 12:18:42 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wyq6z-007mJP-2u for pgsql-hackers@lists.postgresql.org; Tue, 25 Aug 2026 12:18:42 +0000 Received: from fhigh-b5-smtp.messagingengine.com ([202.12.124.156]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wyq6x-00000001BFv-01FI for pgsql-hackers@lists.postgresql.org; Tue, 25 Aug 2026 12:18:41 +0000 Received: from phl-compute-06.internal (phl-compute-06.internal [10.202.2.46]) by mailfhigh.stl.internal (Postfix) with ESMTP id 3BE467A0115; Tue, 25 Aug 2026 08:18:36 -0400 (EDT) Received: from phl-frontend-03 ([10.202.2.162]) by phl-compute-06.internal (MEProxy); Tue, 25 Aug 2026 08:18:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ilmari.org; h=cc :cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm2; t=1787660316; x=1787746716; bh=EXc6/k2V6d 1ou2wNCwxSGzyrZtLZhjB42WLOqPWhY+c=; b=cwKJGpSFg/hnETVXQQeaiGcl34 WSAqggHapczluHgiv+MdZTUM306/Au6QStEwxolvIqYMEc2+EnrIPzm9o8CdaCMl rmvmOoWwfBDRhSBAW5lxyBcoxhjn8bluqjn3ShVDwYpTfjr5aiV5bGlKMTegExYR RUdOlwEVzMnZPJY+/vQycWGX0rypUaeC/QZhaja4RnFdYWIVSmjJEgePNala/bOz JUgD4zVnDdXcklVLEnVOCUmyUaS1xF9k6g4PuVzehkO9tHRfoGG+C+NUB/ORsu9D 2MESZuRVGSYgI8bSHR+T1oMzC2vdiTsSehJYes4mRR7MmaothmmhGytrkxkA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1787660316; x=1787746716; bh=EXc6/k2V6d1ou2wNCwxSGzyrZtLZhjB42WL OqPWhY+c=; b=YqfX1ykMxpVvyq8ZlMFw89KD6r5q0CCJv+oa9V85SgZXg7QIRcb N5rWDcwQc04wqWo3hBl6b0lkA/epE2bKlPUfo3jziLYNJ2F4m7F6s8E8oMFT28+B EGjmQTYIMNhHs40JOouXQwoR2tYsaSeUJPAxXrVji+xdZrA4rvzrBNyPigR95r7g 6wrD0mN79p+BehXnDbqE3fh2tYk96R0fjiLiC76ODH0Hw9PQFgOr4Xh8i9aRfEc4 gubj1N8VUaaTteX1sihIZV+OGXhRKcG4TEdc/CFm5yx95RJ9mhyKbecEvEWDkfMb 6aCxbMff/SVMfvJRHNZJjubuX8FTj7v+wow== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTE+gLJcXc7hPVtdM9XHMBp573Us3DcgdwRAxujRuwY2zMI+3bWXt1GTMck9hApTEI sMe9HRaUU/gaahnYNzNWZadDsb313aRqHgKInZ2mViWtfrdoSGKmHv2I0UaQ3naxMXgiUn AXDavt1Y9K2T2GJSfmnRODwvSYrnK0Vm0cGLxAHouVw5aaUM+oBiK7ifLa78dwcY6HI2U5 tmwcB0JT4CCvB4CAvHVLi0uiXqr/t7pc/TeFg98CML6oSK1gmybEz9m3qqUo++1nwoOthM Zo9NVs3h2Y8O3oU/mXqCf3tKgalqcQVaBBTRvjt7A7uVd7RjhwA9z7t4WvLiULV/x7AEhh yQ5JjWoypyqqDRaG4eWanzFDjOD48PqIzx71qV7Cm28KBskg1ULCOVfh+g2hUfs0WlQ0D3 PsQ5koEL8r+T10f2OB4eLYn+BYg+hWiT3rI3BQ2uB7MpP/c0NsKKdyd6NBmI7y7c8489YU 5/psxPO4JpKpfJr1vIkh5KHnBZB743hc93FZPwQ7t9QB+JZWFpARSgR0j7mpUJCp2k9eUE Te/qQBBuVlKbPY8AQeN2/QT0iPgkgkQ0Su+SncY6TfWOOqxRmk5Y+MnTn5qslmqa3GujkY SBrNaHIKQ6ECUjqLS8yRO2g8IygbUa0ym1M3ziolxDfmJJk4gcU/Z4/qC+tQ X-ME-Proxy: Feedback-ID: i1ff147bf:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 25 Aug 2026 08:18:34 -0400 (EDT) From: =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= To: Ewan Young Cc: PostgreSQL Hackers , David Rowley , Andrey Rachitskiy , Tom Lane Subject: Re: right() returns the whole string for the most negative n References: Date: Tue, 25 Aug 2026 13:18:33 +0100 In-Reply-To: (Ewan Young's message of "Tue, 25 Aug 2026 14:57:18 +0800") Message-ID: <87bjaqz946.fsf@wibble.ilmari.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Ewan Young 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