Re: Fix minor memory leak in connection string validation - Mailing list pgsql-hackers

From Nathan Bossart
Subject Re: Fix minor memory leak in connection string validation
Date
Msg-id 20240113023705.GA4039878@nathanxps13
Whole thread Raw
In response to Fix minor memory leak in connection string validation  (Jeff Davis <pgsql@j-davis.com>)
Responses Re: Fix minor memory leak in connection string validation
List pgsql-hackers
On Fri, Jan 12, 2024 at 03:06:26PM -0800, Jeff Davis wrote:
> It makes me wonder if we should use the resowner mechanism to track
> pointers to malloc'd memory. Then we could use a standard pattern for
> these kinds of cases, and it would also catch more remote issues, like
> if a pstrdup() fails in an error path (which can happen a few lines up
> if the parse fails).

That seems worth exploring.

>          if (!uses_password)
> +        {
> +            /* malloc'd, so we must free it explicitly */
> +            PQconninfoFree(opts);
> +
>              ereport(ERROR,
>                      (errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
>                       errmsg("password is required"),
>                       errdetail("Non-superusers must provide a password in the connection string.")));
> +        }
>      }
>  
>      PQconninfoFree(opts);

Another option could be to surround this with PG_TRY/PG_FINALLY, but your
patch seems sufficient, too.

-- 
Nathan Bossart
Amazon Web Services: https://aws.amazon.com



pgsql-hackers by date:

Previous
From: Jeff Davis
Date:
Subject: Re: [17] CREATE SUBSCRIPTION ... SERVER
Next
From: Tom Lane
Date:
Subject: Re: Fix minor memory leak in connection string validation