pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow)
6+ messages / 3 participants
[nested] [flat]

* BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow)
@ 2026-11-15 04:49 PG Bug reporting form <noreply@postgresql.org>
  2026-11-16 12:23 ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 1 reply; 6+ messages in thread

From: PG Bug reporting form @ 2026-11-15 04:49 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: hackerzheng666@gmail.com

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.








^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow)
  2026-11-15 04:49 BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) PG Bug reporting form <noreply@postgresql.org>
@ 2026-11-16 12:23 ` Daniel Gustafsson <daniel@yesql.se>
  2026-11-16 18:37   ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 6+ messages in thread

From: Daniel Gustafsson @ 2026-11-16 12:23 UTC (permalink / raw)
  To: hackerzheng666@gmail.com; pgsql-bugs@lists.postgresql.org

> 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.diff (application/octet-stream, 567B) download diff --git a/src/backend/libpq/be-fsstubs.c b/src/backend/libpq/be-fsstubs.c index 10cc05d5e4b..425139bcd27 100644 --- a/src/backend/libpq/be-fsstubs.c +++ b/src/backend/libpq/be-fsstubs.c @@ -369,6 +369,12 @@ be_loread(PG_FUNCTION_ARGS) if (len < 0) len = 0; + if (len > MaxAllocSize - VARHDRSZ) + ereport(ERROR, + (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED), + errmsg("large object read request is too large"))); + retval = (bytea *) palloc(VARHDRSZ + len); totalread = lo_read(fd, VARDATA(retval), len); SET_VARSIZE(retval, totalread + VARHDRSZ);
^ permalink raw reply [nested|flat] 6+ messages in thread

* Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow)
  2026-11-15 04:49 BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) PG Bug reporting form <noreply@postgresql.org>
  2026-11-16 12:23 ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Daniel Gustafsson <daniel@yesql.se>
@ 2026-11-16 18:37   ` Tom Lane <tgl@sss.pgh.pa.us>
  2026-11-16 18:49     ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 1 reply; 6+ messages in thread

From: Tom Lane @ 2026-11-16 18:37 UTC (permalink / raw)
  To: Daniel Gustafsson <daniel@yesql.se>; +Cc: hackerzheng666@gmail.com; pgsql-bugs@lists.postgresql.org

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






^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow)
  2026-11-15 04:49 BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) PG Bug reporting form <noreply@postgresql.org>
  2026-11-16 12:23 ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Daniel Gustafsson <daniel@yesql.se>
  2026-11-16 18:37   ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-11-16 18:49     ` Daniel Gustafsson <daniel@yesql.se>
  2026-11-16 19:31       ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 6+ messages in thread

From: Daniel Gustafsson @ 2026-11-16 18:49 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: hackerzheng666@gmail.com; pgsql-bugs@lists.postgresql.org

> 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







^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow)
  2026-11-15 04:49 BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) PG Bug reporting form <noreply@postgresql.org>
  2026-11-16 12:23 ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Daniel Gustafsson <daniel@yesql.se>
  2026-11-16 18:37   ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Tom Lane <tgl@sss.pgh.pa.us>
  2026-11-16 18:49     ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Daniel Gustafsson <daniel@yesql.se>
@ 2026-11-16 19:31       ` Tom Lane <tgl@sss.pgh.pa.us>
  2026-11-16 19:33         ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 1 reply; 6+ messages in thread

From: Tom Lane @ 2026-11-16 19:31 UTC (permalink / raw)
  To: Daniel Gustafsson <daniel@yesql.se>; +Cc: hackerzheng666@gmail.com; pgsql-bugs@lists.postgresql.org

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





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow)
  2026-11-15 04:49 BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) PG Bug reporting form <noreply@postgresql.org>
  2026-11-16 12:23 ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Daniel Gustafsson <daniel@yesql.se>
  2026-11-16 18:37   ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Tom Lane <tgl@sss.pgh.pa.us>
  2026-11-16 18:49     ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Daniel Gustafsson <daniel@yesql.se>
  2026-11-16 19:31       ` Re: BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-11-16 19:33         ` Daniel Gustafsson <daniel@yesql.se>
  0 siblings, 0 replies; 6+ messages in thread

From: Daniel Gustafsson @ 2026-11-16 19:33 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: hackerzheng666@gmail.com; pgsql-bugs@lists.postgresql.org

> 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







^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2026-11-16 19:33 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 04:49 BUG #19905: loread() with length near INT_MAX: "invalid memory alloc request size" (int32 overflow) PG Bug reporting form <noreply@postgresql.org>
2026-11-16 12:23 ` Daniel Gustafsson <daniel@yesql.se>
2026-11-16 18:37   ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-16 18:49     ` Daniel Gustafsson <daniel@yesql.se>
2026-11-16 19:31       ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-16 19:33         ` Daniel Gustafsson <daniel@yesql.se>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox