Fix a host of strto*() bugs

Поиск
Список
Период
Сортировка
Искать

Fix a host of strto*() bugs

От:
"Tristan Partin" <tristan@partin.io>
Дата:
Вложения:
In bug #19584[0], the user reported that tid parsing was platform dependent 
due to the way that strtoul() works. Take the following code:

	strtoul("", &endptr, 10);

On glibc, the return value is 0, errno is not set, and endptr points to 
the empty string. On Apple libc, the return value is 0, errno is set to 
EINVAL, and endptr points to the empty string.

The tid parsing code was correctly handling EINVAL, but did not handle 
the case where endptr == input, which we do in quite a few places around 
the codebase. The end effect was that glibc systems accepted "(1,)" and 
"(,1)" as valid tids for parsing purposes, while Apple libc systems did 
not.

The first patch in the series fixes this issue. I did some analysis on 
other uses of strtoul() and friends and found a few more places where 
integers were not being correctly parsed from strings. Those are each 
attached as individual patches to ease backpatching if it is determined 
that we should. Otherwise, I suggest squashing the series. I wonder if 
we should come up with a helper macro for helping callers handle errors 
correctly? Or should we just always check this case?

The tid issue was discovered back in 2004[1] funnily enough. Tom 
diagnosed it correctly, but a patch was seemingly never committed, so 
here we are.

[0]: https://www.postgresql.org/message-id/19584-e60c446ba6f57c9c%40postgresql.org
[1]: https://www.postgresql.org/message-id/16E80DD4-85B9-11D8-A231-0003935B359C%40cegroup.it

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)

Re: Fix a host of strto*() bugs

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Fri, Jul 31, 2026 at 05:03:38PM +0900, Michael Paquier wrote:
> @@ -961,7 +961,7 @@ precheck_tar_backup_file(verifier_context *context, char *relpath,
>           * Report an error if we didn't consume at least one character, if the
>           * result is 0, or if the value is too large to be a valid OID.
>           */
> -        if (suffix == NULL || num <= 0 || num > OID_MAX)
> +        if (suffix == relpath || num <= 0 || num > OID_MAX)
> 
> Because that's something part of the C standard.  My Linux man page
> has a reference to that, as well:
> "If there were no digits at all, strtoul() stores the original value
> of nptr in *endptr (and returns 0)."

0002 and 0005 have been merged together, and applied as 355814931141.
--
Michael

Re: Fix a host of strto*() bugs

От:
Michael Paquier <michael@paquier.xyz>
Дата:
Вложения:
On Fri, Jul 31, 2026 at 05:03:38PM +0900, Michael Paquier wrote:
> Regarding 0001, yes, that's a bug.  It is definitely not something
> that we could backpatch as we'd begin to reject inputs that were
> accidentally rejected, even if I am hoping that nobody in their right
> mind would use a '(1,)'::tid or a '(,1)'::tid to map to respectively
> (1,0) or (0,1).

One question that I have been asking to myself here is: could the ODBC
driver internally generate a tid value that may not be cross-platform
portable?  And after diving into the code I think that the answer is
no by itself, but there are paths where a client application can
provide its own value.  So I think that there is no practical argument
against making the strtoul() call with tid parsing more compliant to
the C standard.  At least let's try it so on HEAD.  If that proves to
be a experiment failure, we could always revert during the beta cycle
and move back to the old behavior.  Here I'd favor more consistency in
our code base.
--
Michael

Re: Fix a host of strto*() bugs

От:
"Tristan Partin" <tristan@partin.io>
Дата:
On Sun Aug 2, 2026 at 10:26 AM UTC, Michael Paquier wrote:
> On Fri, Jul 31, 2026 at 05:03:38PM +0900, Michael Paquier wrote:
>> @@ -961,7 +961,7 @@ precheck_tar_backup_file(verifier_context *context, char *relpath,
>>           * Report an error if we didn't consume at least one character, if the
>>           * result is 0, or if the value is too large to be a valid OID.
>>           */
>> -        if (suffix == NULL || num <= 0 || num > OID_MAX)
>> +        if (suffix == relpath || num <= 0 || num > OID_MAX)
>> 
>> Because that's something part of the C standard.  My Linux man page
>> has a reference to that, as well:
>> "If there were no digits at all, strtoul() stores the original value
>> of nptr in *endptr (and returns 0)."
>
> 0002 and 0005 have been merged together, and applied as 355814931141.

Here are some re-spins of the previous uncommitted patches. They have 
been rebased on master. I am really curious how the formatting got 
messed up initially. Thanks for committing the the two patches.

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)


Re: Fix a host of strto*() bugs

От:
"Tristan Partin" <tristan@partin.io>
Дата:
Вложения:
On Mon Aug 3, 2026 at 10:08 PM UTC, Michael Paquier wrote:
> On Mon, Aug 03, 2026 at 08:32:38PM +0000, Tristan Partin wrote:
>> Here are some re-spins of the previous uncommitted patches. They have 
>> been rebased on master. I am really curious how the formatting got 
>> messed up initially. Thanks for committing the the two patches.
>
> It looks to me that you have missed the main material of the
> discussion: the patches themselves.

Uhh... check this email...

-- 
Tristan Partin
PostgreSQL Contributors Team
AWS (https://aws.amazon.com)
FAQ