1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-30 21:42:05 +03:00

psql: Clean up more aggressively state of \bind[_named], \parse and \close

This fixes a couple of issues with the psql meta-commands mentioned
above when called repeatedly:
- The statement name is reset for each call.  If a command errors out,
its send_mode would still be set, causing an incorrect path to be taken
when processing a query.  For \bind_named, this could trigger an
assertion failure as a statement name is always expected for this
meta-command.  This issue has been introduced by d55322b0da.
- The memory allocated for bind parameters can be leaked.  This is a bug
enlarged by d55322b0da that exists since 5b66de3433, as it is also
possible to leak memory with \bind in v16 and v17.  This requires a fix
that will be done on the affected branches separately.  This issue is
taken care of here for HEAD.

This patch tightens the cleanup of the state used for the extended
protocol meta-commands (bind parameters, send mode, statement name) by
doing it before running each meta-command on top of doing it once a
query has been processed, avoiding any leaks and the inconsistencies
when mixing calls, by refactoring the cleanup in a single routine used
in all the code paths where this step is required.

Reported-by: Alexander Lakhin
Author: Anthonin Bonnefoy
Discussion: https://postgr.es/m/2e5b89af-a351-ff0a-000c-037ac28314ab@gmail.com
This commit is contained in:
Michael Paquier
2024-09-19 15:39:01 +09:00
parent d69a3f4d70
commit 87eeadaea1
5 changed files with 59 additions and 27 deletions

View File

@ -483,8 +483,7 @@ exec_command_bind(PsqlScanState scan_state, bool active_branch)
int nparams = 0;
int nalloc = 0;
pset.bind_params = NULL;
pset.stmtName = NULL;
clean_extended_state();
while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false)))
{
@ -521,8 +520,7 @@ exec_command_bind_named(PsqlScanState scan_state, bool active_branch,
int nparams = 0;
int nalloc = 0;
pset.bind_params = NULL;
pset.stmtName = NULL;
clean_extended_state();
/* get the mandatory prepared statement name */
opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
@ -719,7 +717,8 @@ exec_command_close(PsqlScanState scan_state, bool active_branch, const char *cmd
char *opt = psql_scan_slash_option(scan_state,
OT_NORMAL, NULL, false);
pset.stmtName = NULL;
clean_extended_state();
if (!opt)
{
pg_log_error("\\%s: missing required argument", cmd);
@ -2205,7 +2204,8 @@ exec_command_parse(PsqlScanState scan_state, bool active_branch,
char *opt = psql_scan_slash_option(scan_state,
OT_NORMAL, NULL, false);
pset.stmtName = NULL;
clean_extended_state();
if (!opt)
{
pg_log_error("\\%s: missing required argument", cmd);