Fix a host of strto*() bugs

From: "Tristan Partin" <tristan(at)partin(dot)io>
To: "pgsql-hackers" <pgsql-hackers(at)postgresql(dot)org>
Cc: <malis(at)pgrust(dot)com>
Subject: Fix a host of strto*() bugs
Date: 2026-07-30 08:55:02
Message-ID: DKBS2Z9CGARC.2T07O6TJYSE8B@partin.io
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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)

Attachment Content-Type Size
v1-0001-Make-tid-parsing-consistent-across-libcs.patch text/plain 6.1 KB
v1-0002-Fix-integer-parsing-in-manifest-parser-for-portab.patch text/plain 3.6 KB
v1-0003-Fix-empty-proto_version-integer-parsing-in-pgoutp.patch text/plain 3.4 KB
v1-0004-Fix-strto-usage-in-ecpg.patch text/plain 45.7 KB
v1-0005-Fix-invalid-endptr-comparison-in-pg_verifybackup..patch text/plain 1.2 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Florin Irion 2026-07-30 08:55:17 Re: pg_plan_advice: add NO_ scan and join method tags
Previous Message Michael Paquier 2026-07-30 08:45:22 Re: Fix hashchar() and hashcharextended() to not depend on char signedness