Avoid corrupting DefElem nodes when parsing publication_names and publish options - Mailing list pgsql-hackers

From sunil s
Subject Avoid corrupting DefElem nodes when parsing publication_names and publish options
Date
Msg-id CAOG6S4_Xs_XU+03xk=P=qPR5Rgus5BDwrffnwDYMSRruaLZ7dQ@mail.gmail.com
Whole thread Raw
Responses Re: Avoid corrupting DefElem nodes when parsing publication_names and publish options
List pgsql-hackers
Hello Hackers,

I noticed that two call sites of SplitIdentifierString() were not
following the API contract documented in varlena.c, which states:
  • rawstring: the input string; must be overwritable! On return, it's
               been modified to contain the separated identifiers.
  • namelist: filled with a palloc'd list of pointers to identifiers within
              rawstring.

The function modifies the input string in-place (replacing separators
with null terminators) and returns a list containing pointers directly
into that modified string.
The two problematic call sites were:
1. parse_publication_options() in publicationcmds.c - passed the result
   of defGetString() directly without copying.
2. pgoutput_startup() in pgoutput.c - passed strVal(defel->arg) directly
   without copying.
All other callers of SplitIdentifierString() in the codebase correctly
use pstrdup() or equivalent (like text_to_cstring()) before calling
the function.
While these two cases happen to work in practice because:
- In publicationcmds.c, the parsed list is used immediately within the
  same loop iteration and then discarded
- In pgoutput.c, the DefElem nodes remain valid for the connection lifetime
...it's still incorrect to rely on this, and the code would break if
someone later tried to use the results after freeing the options list,
or if the memory management changed.
The attached patch adds pstrdup() to both call sites for consistency
and correctness.

Thanks,
Sunil Seetharama
Attachment

pgsql-hackers by date:

Previous
From: Andrey Rudometov
Date:
Subject: Re: Resetting recovery target parameters in pg_createsubscriber
Next
From: sunil s
Date:
Subject: Re: Avoid corrupting DefElem nodes when parsing publication_names and publish options