postgres.git / summary / log / commit / refs
commit 2c077c7d602dba034e209227ea04a14b10797c42
Author: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Thu Nov 19 09:14:51 2026 +0000
Improve error message for to lrage LO read requests
Calling loread() with a length argument which exceeds the max
allocation yields an invalid memory alloc error message, and
if the length overflows it will be a nonsensical value which
can be confusing.
select loread(1, 2147483647);
ERROR: invalid memory alloc request size 18446744071562067971
Fix by detecting an oversize length request and error out like
how other LO read functions do. The error message is identical
to already existing messages to avoid new translation work.
This is not backpatched as the current behavior is safe, this
is only an improvement in the messaging.
Author: Daniel Gustafsson <daniel@yesql.se>
Reported-by: Zheng Hacker <hackerzheng666@gmail.com>
Reviewed-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/19905-3f33374cbee82db1@postgresql.org
src/backend/libpq/be-fsstubs.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/src/backend/libpq/be-fsstubs.c b/src/backend/libpq/be-fsstubs.c
index 10cc05d5e4b..5b14445f1bb 100644
--- a/src/backend/libpq/be-fsstubs.c
+++ b/src/backend/libpq/be-fsstubs.c
@@ -369,6 +369,11 @@ 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);
[parent: 4280abf88409]