Re: [PATCH v1] fix potential memory leak in untransformRelOptions - Mailing list pgsql-hackers

From Junwang Zhao
Subject Re: [PATCH v1] fix potential memory leak in untransformRelOptions
Date
Msg-id CAEG8a3Kt2zjRhCnvgH8_+MkYTyaoXqaVFARE2oAJ=Jk3dBSu5w@mail.gmail.com
Whole thread Raw
In response to Re: [PATCH v1] fix potential memory leak in untransformRelOptions  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: [PATCH v1] fix potential memory leak in untransformRelOptions
List pgsql-hackers
On Thu, Sep 1, 2022 at 10:10 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Junwang Zhao <zhjwpku@gmail.com> writes:
> >   result = lappend(result, makeDefElem(pstrdup(s), val, -1));
> > + pfree(s);
>
> I wonder why it's pstrdup'ing s in the first place.
>
Maybe it's pstrdup'ing s so that the caller should take care of the free?

I'm a little confused when we should call *pfree* and when we should not.
A few lines before there is a call *text_to_cstring* in which it invokes
*pfree* to free the unpacked text [0]. I'm just thinking that since *s* has
been duplicated, we should free it, that's where the patch comes from.

[0]:
```
char *
text_to_cstring(const text *t)
{
    /* must cast away the const, unfortunately */
    text *tunpacked = pg_detoast_datum_packed(unconstify(text *, t));
    int len = VARSIZE_ANY_EXHDR(tunpacked);
    char *result;

    result = (char *) palloc(len + 1);
    memcpy(result, VARDATA_ANY(tunpacked), len);
    result[len] = '\0';

    if (tunpacked != t)
        pfree(tunpacked);

    return result;
}
```

>                         regards, tom lane



-- 
Regards
Junwang Zhao



pgsql-hackers by date:

Previous
From: Tom Lane
Date:
Subject: Re: [PATCH v1] fix potential memory leak in untransformRelOptions
Next
From: "Imseih (AWS), Sami"
Date:
Subject: Re: [PATCH] Query Jumbling for CALL and SET utility statements