Re: Avoid a possible out-of-bounds access (src/backend/optimizer/util/relnode.c)

From: Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
To: Ashutosh Bapat <ashutosh(dot)bapat(dot)oss(at)gmail(dot)com>
Cc: David Rowley <dgrowleyml(at)gmail(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Avoid a possible out-of-bounds access (src/backend/optimizer/util/relnode.c)
Date: 2023-09-26 17:10:04
Message-ID: CAEudQArQ_5ak=JihexAFN0SwqBZh_0xwsn6ReV_-hEtVw7+pVA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Em ter., 26 de set. de 2023 às 09:30, Ranier Vilela <ranier(dot)vf(at)gmail(dot)com>
escreveu:

> Em ter., 26 de set. de 2023 às 07:34, Ashutosh Bapat <
> ashutosh(dot)bapat(dot)oss(at)gmail(dot)com> escreveu:
>
>> On Tue, Sep 26, 2023 at 3:32 PM David Rowley <dgrowleyml(at)gmail(dot)com>
>> wrote:
>> >
>> > find_base_rel() could be made more robust for free by just casting the
>> > relid and simple_rel_array_size to uint32 while checking that relid <
>> > root->simple_rel_array_size. The 0th element should be NULL anyway,
>> > so "if (rel)" should let relid==0 calls through and allow that to
>> > ERROR still. I see that just changes a "jle" to "jnb" vs adding an
>> > additional jump for Ranier's version. [1]
>>
>> That's a good suggestion.
>>
>> I am fine with find_base_rel() as it is today as well. But
>> future-proofing it seems to be fine too.
>>
>> >
>> > It seems worth not making find_base_rel() more expensive than it is
>> > today as commonly we just reference root->simple_rel_array[n] directly
>> > anyway because it's cheaper. It would be nice if we didn't add further
>> > overhead to find_base_rel() as this would make the case for using
>> > PlannerInfo.simple_rel_array directly even stronger.
>>
>> I am curious, is the overhead in find_base_rel() impacting overall
>> performance?
>>
> It seems to me that it adds a LEA instruction.
> https://godbolt.org/z/b4jK3PErE
>
> Although it doesn't seem like much,
> I believe the solution (casting to unsigned) seems better.
> So +1.
>
As suggested, casting is the best option that does not add any overhead and
improves the robustness of the find_base_rel function.
I propose patch v1, with the additional addition of fixing the
find_base_rel_ignore_join function,
which despite not appearing in Coverity reports, suffers from the same
problem.

Taking advantage, I also propose a scope reduction,
as well as the const of the root parameter, which is very appropriate.

best regards,
Ranier Vilela

Attachment Content-Type Size
v1-0001-Avoid-possible-out-of-bounds-access.patch application/octet-stream 2.0 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Karl O. Pinc 2023-09-26 17:45:53 Re: [PGdocs] fix description for handling pf non-ASCII characters
Previous Message Jeff Davis 2023-09-26 17:00:12 Re: Questions about the new subscription parameter: password_required