Re: Return INT_MAX or INT_MIN from strtoint() on overflow

From: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Prashant Chinnam <crprashant(at)hotmail(dot)com>, surya poondla <suryapoondla4(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Takeshi Ideriha <iderihatakeshi(at)gmail(dot)com>
Subject: Re: Return INT_MAX or INT_MIN from strtoint() on overflow
Date: 2026-10-31 00:15:55
Message-ID: 7F74F9C7-2AEB-46EA-A0EA-8E4BBAF725C3@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

> On Oct 30, 2026, at 23:07, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> Prashant Chinnam <crprashant(at)hotmail(dot)com> writes:
>> Hi Tom,
>>> My thought here is to back-patch the strtoint() change.
>>> I cannot envision any case that that would make worse.
>>> And failing to do so leaves a land-mine for potential
>>> future back-patches of code using strtoint().
>
>> Thanks -- back-patching the strtoint() change removes my
>> only reason for preferring strtoi64() in patch [1]. Once
>> it has landed, I will switch that patch back to
>> strtoint(); that was the shape of my original v1, minus
>> the errno workaround that the saturating return value
>> makes unnecessary.
>
> Pushed. I made the code look like this:
>
> val = strtol(str, endptr, base);
> if (val != (int) val)
> + {
> + val = (val < 0) ? INT_MIN : INT_MAX;
> errno = ERANGE;
> + }
> return (int) val;
>
> I was concerned that Chao's original formulation could provoke
> compiler warnings on machines with 32-bit long: it would not
> be surprising to get "condition is always false" warnings
> from "val > INT_MAX" etc. (In principle the existing test
> could provoke such warnings too, but it doesn't. I very
> vaguely recall that we settled on the cast-and-compare
> method specifically to avoid warnings, way back when.)

Happy to learn that.

Thanks for pushing.

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

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2026-10-31 00:34:27 Re: pgsql: Set HEAP_XMAX_INVALID for tuples updated by aborted subtransacti
Previous Message Chao Li 2026-10-31 00:05:28 Re: Remove unused is_redo argument from pgstat_execute_transactional_drops()