Re: Invalid memory alloc request size for repeat()

From: Japin Li <japinli(at)hotmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Invalid memory alloc request size for repeat()
Date: 2022-05-26 01:03:54
Message-ID: MEYP282MB16697F171101020C5A26306DB6D99@MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On Wed, 25 May 2022 at 22:50, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Japin Li <japinli(at)hotmail(dot)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.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Paquier 2022-05-26 01:18:42 Re: Remove support for Visual Studio 2013
Previous Message Japin Li 2022-05-26 01:02:08 Re: Invalid memory alloc request size for repeat()