Re: Invalid memory alloc request size for repeat()

Japin Li <japinli@hotmail.com>

From: Japin Li <japinli@hotmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: "pgsql-hackers@lists.postgresql.org" <pgsql-hackers@lists.postgresql.org>
Date: 2022-05-26T01:03:54Z
Lists: pgsql-hackers
On Wed, 25 May 2022 at 22:50, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Japin Li <japinli@hotmail.com> writes:
>> Today, I try to use repeat() to generate 1GB text, and it occurs invalid memory
>> alloc request size [1].  It is a limit from palloc(), then I try to reduce it,
>> it still complains out of memory which comes from enlargeStringInfo() [2].  The
>> documentation about repect() [3] doesn't mentaion the limitation.
>
> It would probably make sense for repeat() to check this explicitly:
>
>     if (unlikely(pg_mul_s32_overflow(count, slen, &tlen)) ||
> -       unlikely(pg_add_s32_overflow(tlen, VARHDRSZ, &tlen)))
> +       unlikely(pg_add_s32_overflow(tlen, VARHDRSZ, &tlen)) ||
> +       unlikely(!AllocSizeIsValid(tlen)))
>         ereport(ERROR,
>                 (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
>                  errmsg("requested length too large")));
>

LGTM.  Thanks for your patch!


-- 
Regrads,
Japin Li.
ChengDu WenWu Information Technology Co.,Ltd.



Commits

  1. Avoid ERRCODE_INTERNAL_ERROR in oracle_compat.c functions.