pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Fix assorted bugs in ecpg's macro mechanism.
Date: Tue, 16 Apr 2024 16:31:47 +0000
Message-ID: <E1rwlil-002Ml6-4A@gemulon.postgresql.org> (raw)

Fix assorted bugs in ecpg's macro mechanism.

The code associated with EXEC SQL DEFINE was unreadable and full of
bugs, notably:

* It'd attempt to free a non-malloced string if the ecpg program
tries to redefine a macro that was defined on the command line.

* Possible memory stomp if user writes "-D=foo".

* Undef'ing or redefining a macro defined on the command line would
change the state visible to the next file, when multiple files are
specified on the command line.  (While possibly that could have been
an intentional choice, the code clearly intends to revert to the
original macro state; it's just failing to consider this interaction.)

* Missing "break" in defining a new macro meant that redefinition
of an existing name would cause an extra entry to be added to the
definition list.  While not immediately harmful, a subsequent undef
would result in the prior entry becoming visible again.

* The interactions with input buffering are subtle and were entirely
undocumented.

It's not that surprising that we hadn't noticed these bugs,
because there was no test coverage at all of either the -D
command line switch or multiple input files.  This patch adds
such coverage (in a rather hacky way I guess).

In addition to the code bugs, the user documentation was confused
about whether the -D switch defines a C macro or an ecpg one, and
it failed to mention that you can write "-Dsymbol=value".

These problems are old, so back-patch to all supported branches.

Discussion: https://postgr.es/m/998011.1713217712@sss.pgh.pa.us

Branch
------
REL_14_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/4631646391ec105c54ee3ed27211ae85d2f2a3d6

Modified Files
--------------
doc/src/sgml/ecpg.sgml                             |   8 ++
doc/src/sgml/ref/ecpg-ref.sgml                     |   6 +-
src/interfaces/ecpg/preproc/ecpg.c                 |  79 +++++++------
src/interfaces/ecpg/preproc/pgc.l                  | 127 +++++++++++++++------
src/interfaces/ecpg/preproc/type.h                 |  22 +++-
src/interfaces/ecpg/test/expected/sql-define.c     |  65 ++++++++++-
.../ecpg/test/expected/sql-define.stderr           |  24 ++++
.../ecpg/test/expected/sql-define.stdout           |   3 +
src/interfaces/ecpg/test/sql/Makefile              |   3 +
src/interfaces/ecpg/test/sql/define.pgc            |  25 ++++
src/interfaces/ecpg/test/sql/define_prelim.pgc     |   6 +
11 files changed, 288 insertions(+), 80 deletions(-)



view thread (8+ messages)  latest in thread

Message-ID: <E1rwlil-002Ml6-4A@gemulon.postgresql.org>
Permalink:  ../E1rwlil-002Ml6-4A@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1rwlil-002Ml6-4A@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: Fix assorted bugs in ecpg's macro mechanism.
  In-Reply-To: <E1rwlil-002Ml6-4A@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox