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 1wz8DG-003pcn-20 for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Aug 2026 07:38:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wz8DF-00BsWQ-1T for pgsql-hackers@arkaria.postgresql.org; Wed, 26 Aug 2026 07:38:21 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wz8DF-00BsVK-0W for pgsql-hackers@lists.postgresql.org; Wed, 26 Aug 2026 07:38:21 +0000 Received: from mail-pg1-x536.google.com ([2607:f8b0:4864:20::536]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wz8D7-00000002TJE-2CAL for pgsql-hackers@lists.postgresql.org; Wed, 26 Aug 2026 07:38:15 +0000 Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-cb5b8572b70so722655a12.2 for ; Wed, 26 Aug 2026 00:38:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787729892; x=1788334692; darn=lists.postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:content-type:from:to:cc :subject:date:message-id:reply-to:content-type; bh=ESgfpsh96rd2mZRuDZL6uPItYv34KI/alM9sjHipx+M=; b=pGMUqEyz3LeWWnGFArjpWvL7h6DWRtBAcnUDq64dSIIrD0FLqF2F4U4vxGD6glbDRU yxMc6Mi5hqwFYCWZvYSws2IeYJM2RQtv23fE3o6nUR+LkQg0fzHjNwoDWxFzozmFkUaI zf7O9ZaCK0VBmcsKpP0X5o5ZE3K/kdGBPIRBV3JVZirpKnw/WOObqGvuRbF2ISsx+p4I f+JhFStB58mgMNbUqCFwiDSdsEUtf2JFyqHtZOfQkPKMejWwg3agDwPQ+g+YuX9BmW1S E4pkBzHO73OJrrNhFNmx3Xlk4P9mAJFhqfajdeDVO1aG0jna5XqNiYS0NLSddP01pEEo 3MoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787729892; x=1788334692; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:content-type:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ESgfpsh96rd2mZRuDZL6uPItYv34KI/alM9sjHipx+M=; b=sbKAZ8vl2DfFcA0cbbuq6mO3u6BtDNyPj7ri2Rx79SV4EyC3JHF+MyXASFCCg0X+nv UqEr5Jl2GY+Dgm8Sf7Di3KbkS3XhjkvYjmQTZ8joOaed1LCicHRcEUhCeTdto1O3pDt+ iljh+hc1wNL5ck7WjrWtJADCKrjgHsIx7Q3wep+istW1V/Op5mnea2egVRwm/0aMlVgR EexLMB6Gpbtrmvu9a3UEPG+6zA4ITnjJA535ZLAu77EJtFhG3GKMft5A7zIZPtjF5kZj YPp5OZugFox4jMkxnDElJ40jnhon4CaW4ETPgZT/qrgGeLjB4aNKSjAnyJWdPnZAjZoz dQqQ== X-Forwarded-Encrypted: i=1; AHgh+RqyPZBKstkp9eLd/Lx9b+GiwrX9eBOjZOzRXFpu7JcvXYRmeVTCSMHGLtMcaKLVlrxYDzOFXvTOAUKF3BN/@lists.postgresql.org X-Gm-Message-State: AFuF++lF+j5SlZTAT9tRj7ZcWt226rmbpHwZD0XWzE7rxfy/gjQer3rE HQq7ETqiqgFNc7dV/3M4zmiv8z4VTmHMuvfuqsyq5DT3cprODlRw3cVX X-Gm-Gg: AR+sD11Mty16rHQuf7pHGfzUcsOrGlFJkILkBrw3NfdYRhqlQ0MWmmHwA5OR2fF8w03 qi2wD1TxjUNZM7qf4m1zmguzDXw7Xo4YifhUJvkP6XVllcHPH6lAiOEUWvQ+kuXfGwrL7mvsHOE 3BgDRxhCfiVBwJ2Nw7UTiis7BoIdTKPcdu/pUji0Al/oEQNq+19equLnkfn5wFFe7s4IypjBd+o FoaARFv4lqtw4NK0r3XuS+lhghp+dIGE3DNFtFdLekuDMmf+df8Mx4JOMiVEg4/fXzVjHUf6KRO mE77A6S/IBTHIxf4SPt2Z7i1dsFkIcxN5ExZo9ACG6I5bebSg0qGn2TDZrapUecV5YeXbWShFWR 9q+K+0LtikDYbVuYN1R4DDQRfNeI9tvrNLDedEqtcD7Wn2GxNJCaPgpD4NGFYHVCq6fzvKYuZvS I5GGJwh8nE3M9D10sOcKjWG+oF1x3DP0mGr9lgxoZlEMgWfqQgEL2BUIE6bXxlyq2TmSdgEBTpB sdc98BSqsib X-Received: by 2002:a05:6a00:330e:b0:846:bc60:5bf7 with SMTP id d2e1a72fcca58-8537259b748mr10321590b3a.6.1787729892419; Wed, 26 Aug 2026 00:38:12 -0700 (PDT) Received: from smtpclient.apple ([27.210.133.50]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8535cf0a701sm617421b3a.47.2026.08.26.00.38.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 26 Aug 2026 00:38:11 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Re: right() returns the whole string for the most negative n From: Chao Li In-Reply-To: Date: Wed, 26 Aug 2026 15:37:36 +0800 Cc: =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= , PostgreSQL Hackers , David Rowley , Andrey Rachitskiy , Tom Lane , Daniel Gustafsson Content-Transfer-Encoding: quoted-printable Message-Id: <77EDC0CB-4BA9-4E14-AC90-D92D0EAD58F6@gmail.com> References: <87bjaqz946.fsf@wibble.ilmari.org> <45315BCF-2D6E-4AE9-A62E-668E5A12C66B@gmail.com> To: Ewan Young X-Mailer: Apple Mail (2.3864.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Aug 26, 2026, at 14:29, Ewan Young wrote: >=20 > Hi >=20 > On Wed, Aug 26, 2026 at 1:58=E2=80=AFPM Chao Li = wrote: >>=20 >>=20 >>=20 >>> On Aug 26, 2026, at 09:52, Ewan Young wrote: >>>=20 >>> On Tue, Aug 25, 2026 at 8:18=E2=80=AFPM Dagfinn Ilmari Manns=C3=A5ker >>> wrote: >>>>=20 >>>> Ewan Young writes: >>>>=20 >>>>> 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; >>>>>=20 >>>>> if (n < 0) >>>>> - n =3D -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 =3D=3D PG_INT32_MIN)) >>>>> + n =3D PG_INT32_MAX; >>>>> + else >>>>> + n =3D -n; >>>>> + } >>>>=20 >>>> Instead of open-coding this, how about about using = pg_neg_s32_overflow? >>>>=20 >>>> if (pg_neg_s32_overflow(n, &n)) >>>> n =3D PG_INT32_MAX; >>>>=20 >>>=20 >>> Much nicer, thanks - done in v2. varlena.c already includes = common/int.h, >>> so no new header was needed. >>>=20 >>>> 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. >>>=20 >>> Agreed, I left it as the two-liner. >>>=20 >>> Behaviour and tests are unchanged from v1: right('abcdef', = INT32_MIN) now >>> returns '', the adjacent values and left() are untouched, and make = check >>> passes. >>>=20 >>>>=20 >>>> - ilmari >>>=20 >>>=20 >>>=20 >>> -- >>> Regards, >>> Ewan Young >>> >>=20 >> ``` >> + /* >> + * 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 =3D PG_INT32_MAX; >> ``` >>=20 >> 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? >=20 > Good point =E2=80=94 done. Reworded the comment in v3 to describe the = overflow > case generically; no other changes from v2. Patch attached. >=20 > Thanks for the review. >=20 > --=20 > Regards, > Ewan Young > Thanks for updating the patch. V3 LGTM. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/