Re: BUG #18598: AddressSanitizer detects use after free inside json_unique_hash_match()

From: Junwang Zhao <zhjwpku(at)gmail(dot)com>
To: Tomas Vondra <tomas(at)vondra(dot)me>
Cc: exclusion(at)gmail(dot)com, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #18598: AddressSanitizer detects use after free inside json_unique_hash_match()
Date: 2024-09-11 14:56:33
Message-ID: CAEG8a3K6CGUOcFL0vb-+TLkxwqH6-39j7bULBrpvkXVyS-o6Ug@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

Hi Tomas,

On Wed, Sep 11, 2024 at 8:08 PM Tomas Vondra <tomas(at)vondra(dot)me> wrote:
>
> On 9/10/24 21:47, Tomas Vondra wrote:
> > ...
> >
> > The only question that bothers me a little bit is the possibility of a
> > memory leak - could it happen that we keep the copied key much longer
> > than needed? Or does aggcontext have with the right life span? AFAICS
> > that's where we allocate the aggregate state, so it seems fine.
> >
> > Also, how far back do we need to backpatch this? ITSM PG15 does not have
> > this issue, and it was introduced with the SQL/JSON stuff in PG16. Is
> > that correct?
> >
>
> Nah, I spent a bit of time looking for a memory leak, but I don't think
> there's one, or at least not a new one. We use the same memory context
> as for the hash table / buffer, so that should be fine.
>
> But this made me realize the code in json_build_object_worker() can
> simply use pstrdup() to copy the key into CurrentMemoryContext, which is
> where the hash table of unique keys is. In fact, using unique_check.mcxt
> would not be quite right:
>
> MemoryContext mcxt; /* context for saving skipped keys */
>
> And this has nothing to do with skipped keys.
>
> So I adjusted that way and pushed.
>

I didn't get the time to reply to you quickly, sorry about that.
Thank you for improving the patch and appreciate your time
for working on this.

>
>
> Thanks for the report / patch.
>
> --
> Tomas Vondra

--
Regards
Junwang Zhao

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message a.kozhemyakin 2024-09-12 02:30:06 Re: BUG #18610: llvm error: __aarch64_swp4_acq_rel which could not be resolved
Previous Message Tomas Vondra 2024-09-11 12:08:23 Re: BUG #18598: AddressSanitizer detects use after free inside json_unique_hash_match()

Browse pgsql-hackers by date

  From Date Subject
Next Message David E. Wheeler 2024-09-11 15:00:09 Re: Document DateStyle effect on jsonpath string()
Previous Message Fujii Masao 2024-09-11 14:55:47 Re: Remove old RULE privilege completely