BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow)
The following bug has been logged on the website:
Bug reference: 19905
Logged by: Zheng Hacker
Email address: hackerzheng666@gmail.com
PostgreSQL version: 19rc2
Operating system: Linux x86_64 (Ubuntu 22.04)
Description:
Tested on git master (20devel, commit 70c4a57b64d3; the code in question is
unchanged at master 81b2179 as of 2026-11-14). Not tested on 19rc2 or older
branches; the version selected in the form is only the closest listed one.
select loread(2147483647, 2147483647);
ERROR: invalid memory alloc request size 18446744071562067971
In src/backend/libpq/be-fsstubs.c, be_loread() (the SQL function loread)
does
retval = (bytea *) palloc(VARHDRSZ + len);
with "int len". For len > INT_MAX - VARHDRSZ the sum overflows to a negative
int and is converted to a huge size_t, so the user sees an internal-looking
error with a nonsensical size instead of a clean limit error. Clamping len
to MaxAllocSize - VARHDRSZ (or raising "requested length too large" before
the allocation, as lo_get() style paths do) would give a proper message.
On 15 Nov 2026, at 05:49, PG Bug reporting form <noreply@postgresql.org> wrote:
Clamping len
to MaxAllocSize - VARHDRSZ (or raising "requested length too large" before
the allocation, as lo_get() style paths do) would give a proper message.
As far as bugs go, this is pretty low on the prioritylist; but since I had a
look at it I think we should error out like lo_get().
--
Daniel Gustafsson
Attachments:
loread.diffapplication/octet-stream; name=loread.diff; x-unix-mode=0644Download+5-0
Daniel Gustafsson <daniel@yesql.se> writes:
On 15 Nov 2026, at 05:49, PG Bug reporting form <noreply@postgresql.org> wrote:
Clamping len
to MaxAllocSize - VARHDRSZ (or raising "requested length too large" before
the allocation, as lo_get() style paths do) would give a proper message.
As far as bugs go, this is pretty low on the prioritylist; but since I had a
look at it I think we should error out like lo_get().
Is it really worth the trouble at all? (It's not just your time to
commit this, it's that you're loading more work on the translators.)
I don't see any non-nefarious case in which we'd hit this error,
so letting the palloc error come out seems fine to me.
regards, tom lane
On 16 Nov 2026, at 19:37, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Daniel Gustafsson <daniel@yesql.se> writes:
On 15 Nov 2026, at 05:49, PG Bug reporting form <noreply@postgresql.org> wrote:
Clamping len
to MaxAllocSize - VARHDRSZ (or raising "requested length too large" before
the allocation, as lo_get() style paths do) would give a proper message.As far as bugs go, this is pretty low on the prioritylist; but since I had a
look at it I think we should error out like lo_get().Is it really worth the trouble at all?
Not really, other than the relaxing feeling of simple patches when one need a
mental break over a coffee.
(It's not just your time to
commit this, it's that you're loading more work on the translators.)
I made sure to re-use an exsting error message from the large object code to
avoid translation work for no reason. Otherwise this would've been net-negative
for sure.
I don't see any non-nefarious case in which we'd hit this error,
so letting the palloc error come out seems fine to me.
Same here, I can't see any risk.
--
Daniel Gustafsson
Daniel Gustafsson <daniel@yesql.se> writes:
On 16 Nov 2026, at 19:37, Tom Lane <tgl@sss.pgh.pa.us> wrote:
(It's not just your time to
commit this, it's that you're loading more work on the translators.)
I made sure to re-use an exsting error message from the large object code to
avoid translation work for no reason. Otherwise this would've been net-negative
for sure.
Oh, okay. In that case I have no objection if you want to push this.
But probably master-only (else I'm gonna have to write a release note)
regards, tom lane
On 16 Nov 2026, at 20:31, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Daniel Gustafsson <daniel@yesql.se> writes:
On 16 Nov 2026, at 19:37, Tom Lane <tgl@sss.pgh.pa.us> wrote:
(It's not just your time to
commit this, it's that you're loading more work on the translators.)I made sure to re-use an exsting error message from the large object code to
avoid translation work for no reason. Otherwise this would've been net-negative
for sure.Oh, okay. In that case I have no objection if you want to push this.
But probably master-only (else I'm gonna have to write a release note)
Absolutely, this has no business in the backbranches.
--
Daniel Gustafsson