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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>
Cc: surya poondla <suryapoondla4(at)gmail(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Prashant Chinnam <crprashant(at)hotmail(dot)com>
Subject: Re: Return INT_MAX or INT_MIN from strtoint() on overflow
Date: 2026-10-30 01:29:08
Message-ID: 2261183.1793323748@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com> writes:
>> On Oct 30, 2026, at 04:44, surya poondla <suryapoondla4(at)gmail(dot)com> wrote:
>> strtoint() job has always been to detect overflow through errno. All the core callers in that style (datetime.c, read.c, tsearchcmds.c, jsonfuncs.c, psql, option_utils.c) check errno == ERANGE and never
>> look at the return value after an overflow. For them v1 changes nothing.

> That’s true, but it doesn’t mean the current return-value behavior matches the header comment.

Yeah. POSIX is 100% clear about what strtol is required to do:

If the correct value is outside the range of representable values,
{LONG_MIN}, {LONG_MAX}, {LLONG_MIN}, or {LLONG_MAX} shall be
returned (according to the sign of the value), and errno set to
[ERANGE].

So a bitwise-truncated value is not OK; we must preserve the original
sign in case the caller wants to know that, and we must ensure that
callers' range checks deliver sensible results. Even if none of our
in-core callers cared at the moment, it makes sense to me that we
should match this behavior. All the more reason if there are places
that could immediately make use of the tighter API spec.

regards, tom lane

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message jian he 2026-10-30 01:54:11 Re: CAST(... ON DEFAULT) - WIP build on top of Error-Safe User Functions
Previous Message Fujii Masao 2026-10-30 01:20:16 Re: Reduce logging during prepared transaction recovery