pgsql: Fix corner-case uninitialized-variable issues in plpgsql. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix corner-case uninitialized-variable issues in plpgsql.
Date
Msg-id E1m5t8q-0002dY-Rl@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix corner-case uninitialized-variable issues in plpgsql.

If an error was raised during our initial attempt to check whether
a successfully-compiled expression is "simple", subsequent calls of
exec_stmt_execsql would suppose that stmt->mod_stmt was already computed
when it had not been.  This could lead to assertion failures in debug
builds; in production builds the effect would typically be to act as
if INTO STRICT had been specified even when it had not been.  Of course
that only matters if the subsequent attempt to execute the expression
succeeds, so that the problem can only be reached by fixing a failure
in some referenced, inline-able SQL function and then retrying the
calling plpgsql function in the same session.

(There might be even-more-obscure ways to change the expression's
behavior without changing the plpgsql function, but that one seems
like the only one people would be likely to hit in practice.)

The most foolproof way to fix this would be to arrange for
exec_prepare_plan to not set expr->plan until we've finished the
subsidiary simple-expression check.  But it seems hard to do that
without creating reference-count leak issues.  So settle for documenting
the hazard in a comment and fixing exec_stmt_execsql to test separately
for whether it's computed stmt->mod_stmt.  (That adds a test-and-branch
per execution, but hopefully that's negligible in context.)  In v11 and
up, also fix exec_stmt_call which had a variant of the same issue.

Per bug #17113 from Alexander Lakhin.  Back-patch to all
supported branches.

Discussion: https://postgr.es/m/17113-077605ce00e0e7ec@postgresql.org

Branch
------
REL_11_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/7321d5c3ffc28178bcd72e9e905acfeaa953866b

Modified Files
--------------
src/pl/plpgsql/src/pl_exec.c | 76 +++++++++++++++++++++++++++++++-------------
src/pl/plpgsql/src/pl_gram.y |  2 +-
src/pl/plpgsql/src/plpgsql.h |  4 +--
3 files changed, 57 insertions(+), 25 deletions(-)


pgsql-committers by date:

Previous
From: Michael Paquier
Date:
Subject: pgsql: Re-enable TAP tests of pg_receivewal for ZLIB on Windows
Next
From: Andrew Dunstan
Date:
Subject: Re: pgsql: Re-enable TAP tests of pg_receivewal for ZLIB on Windows