]> git.ipfire.org Git - thirdparty/postgresql.git/commit
Fix plpgsql's handling of -- comments following expressions.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 10 Apr 2024 19:45:59 +0000 (15:45 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 10 Apr 2024 19:45:59 +0000 (15:45 -0400)
commitd85db0a8e95cb7284a288d18f6fe48afde65c9f9
treee1ab1e6c0331861a3a87c6d3f55a2f90ed3330a1
parentef124d0952e18f1c7a7e9f99b2d10e2f0737c24a
Fix plpgsql's handling of -- comments following expressions.

Up to now, read_sql_construct() has collected all the source text from
the statement or expression's initial token up to the character just
before the "until" token.  It normally tries to strip trailing
whitespace from that, largely for neatness.  If there was a "-- text"
comment after the expression, this resulted in removing the newline
that terminates the comment, which creates a hazard if we try to paste
the collected text into a larger SQL construct without inserting a
newline after it.  In particular this caused our handling of CASE
constructs to fail if there's a comment after a WHEN expression.

Commit 4adead1d2 noticed a similar problem with cursor arguments,
and worked around it through the rather crude hack of suppressing
the whitespace-trimming behavior for those.  Rather than do that
and leave the hazard open for future hackers to trip over, let's
fix it properly.  pl_scanner.c already has enough infrastructure
to report the end location of the expression's last token, so
we can copy up to that location and never collect any trailing
whitespace or comment to begin with.

Erik Wienhold and Tom Lane, per report from Michal Bartak.
Back-patch to all supported branches.

Discussion: https://postgr.es/m/CAAVzF_FjRoi8fOVuLCZhQJx6HATQ7MKm=aFOHWZODFnLmjX-xA@mail.gmail.com
src/pl/plpgsql/src/expected/plpgsql_control.out
src/pl/plpgsql/src/pl_gram.y
src/pl/plpgsql/src/pl_scanner.c
src/pl/plpgsql/src/plpgsql.h
src/pl/plpgsql/src/sql/plpgsql_control.sql
src/test/regress/expected/plpgsql.out
src/test/regress/sql/plpgsql.sql