1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-18 17:42:25 +03:00

Fix up misuse of "volatile" in contrib/xml2.

What we want in these places is "xmlChar *volatile ptr",
not "volatile xmlChar *ptr".  The former means that the
pointer variable itself needs to be treated as volatile,
while the latter says that what it points to is volatile.
Since the point here is to ensure that the pointer variables
don't go crazy after a longjmp, it's the former semantics
that we need.  The misplacement of "volatile" also led
to needing to cast away volatile in some places.

Also fix a number of places where variables that are assigned to
within a PG_TRY and then used after it were not initialized or
not marked as volatile.  (A few buildfarm members were issuing
"may be used uninitialized" warnings about some of these variables,
which is what drew my attention to this area.)  In most cases
these variables were being set as the last step within the PG_TRY
block, which might mean that we could get away without the "volatile"
marking.  But doing that seems unsafe and is definitely not per our
coding conventions.

These problems seem to have come in with 732061150, so no need
for back-patch.
This commit is contained in:
Tom Lane
2025-07-08 17:00:34 -04:00
parent e03c952877
commit 93001888d8
2 changed files with 29 additions and 30 deletions

View File

@ -54,7 +54,7 @@ static xmlChar *pgxml_texttoxmlchar(text *textstring);
static xpath_workspace *pgxml_xpath(text *document, xmlChar *xpath, static xpath_workspace *pgxml_xpath(text *document, xmlChar *xpath,
PgXmlErrorContext *xmlerrcxt); PgXmlErrorContext *xmlerrcxt);
static void cleanup_workspace(volatile xpath_workspace *workspace); static void cleanup_workspace(xpath_workspace *workspace);
/* /*
@ -88,8 +88,8 @@ Datum
xml_encode_special_chars(PG_FUNCTION_ARGS) xml_encode_special_chars(PG_FUNCTION_ARGS)
{ {
text *tin = PG_GETARG_TEXT_PP(0); text *tin = PG_GETARG_TEXT_PP(0);
text *tout; text *volatile tout = NULL;
volatile xmlChar *tt = NULL; xmlChar *volatile tt = NULL;
PgXmlErrorContext *xmlerrcxt; PgXmlErrorContext *xmlerrcxt;
xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL); xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
@ -111,7 +111,7 @@ xml_encode_special_chars(PG_FUNCTION_ARGS)
PG_CATCH(); PG_CATCH();
{ {
if (tt != NULL) if (tt != NULL)
xmlFree((xmlChar *) tt); xmlFree(tt);
pg_xml_done(xmlerrcxt, true); pg_xml_done(xmlerrcxt, true);
@ -120,7 +120,7 @@ xml_encode_special_chars(PG_FUNCTION_ARGS)
PG_END_TRY(); PG_END_TRY();
if (tt != NULL) if (tt != NULL)
xmlFree((xmlChar *) tt); xmlFree(tt);
pg_xml_done(xmlerrcxt, false); pg_xml_done(xmlerrcxt, false);
@ -145,11 +145,10 @@ pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
xmlChar *plainsep) xmlChar *plainsep)
{ {
volatile xmlBufferPtr buf = NULL; volatile xmlBufferPtr buf = NULL;
xmlChar *result; xmlChar *volatile result = NULL;
int i;
PgXmlErrorContext *xmlerrcxt; PgXmlErrorContext *xmlerrcxt;
/* spin some error handling */ /* spin up some error handling */
xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL); xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
PG_TRY(); PG_TRY();
@ -168,7 +167,7 @@ pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
} }
if (nodeset != NULL) if (nodeset != NULL)
{ {
for (i = 0; i < nodeset->nodeNr; i++) for (int i = 0; i < nodeset->nodeNr; i++)
{ {
if (plainsep != NULL) if (plainsep != NULL)
{ {
@ -257,8 +256,8 @@ xpath_nodeset(PG_FUNCTION_ARGS)
xmlChar *toptag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2)); xmlChar *toptag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
xmlChar *septag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(3)); xmlChar *septag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(3));
xmlChar *xpath; xmlChar *xpath;
text *xpres; text *volatile xpres = NULL;
volatile xpath_workspace *workspace; xpath_workspace *volatile workspace = NULL;
PgXmlErrorContext *xmlerrcxt; PgXmlErrorContext *xmlerrcxt;
xpath = pgxml_texttoxmlchar(xpathsupp); xpath = pgxml_texttoxmlchar(xpathsupp);
@ -302,8 +301,8 @@ xpath_list(PG_FUNCTION_ARGS)
text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */ text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
xmlChar *plainsep = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2)); xmlChar *plainsep = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
xmlChar *xpath; xmlChar *xpath;
text *xpres; text *volatile xpres = NULL;
volatile xpath_workspace *workspace; xpath_workspace *volatile workspace = NULL;
PgXmlErrorContext *xmlerrcxt; PgXmlErrorContext *xmlerrcxt;
xpath = pgxml_texttoxmlchar(xpathsupp); xpath = pgxml_texttoxmlchar(xpathsupp);
@ -344,8 +343,8 @@ xpath_string(PG_FUNCTION_ARGS)
text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */ text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
xmlChar *xpath; xmlChar *xpath;
int32 pathsize; int32 pathsize;
text *xpres; text *volatile xpres = NULL;
volatile xpath_workspace *workspace; xpath_workspace *volatile workspace = NULL;
PgXmlErrorContext *xmlerrcxt; PgXmlErrorContext *xmlerrcxt;
pathsize = VARSIZE_ANY_EXHDR(xpathsupp); pathsize = VARSIZE_ANY_EXHDR(xpathsupp);
@ -398,9 +397,9 @@ xpath_number(PG_FUNCTION_ARGS)
text *document = PG_GETARG_TEXT_PP(0); text *document = PG_GETARG_TEXT_PP(0);
text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */ text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
xmlChar *xpath; xmlChar *xpath;
float4 fRes = 0.0; volatile float4 fRes = 0.0;
bool isNull = false; volatile bool isNull = false;
volatile xpath_workspace *workspace = NULL; xpath_workspace *volatile workspace = NULL;
PgXmlErrorContext *xmlerrcxt; PgXmlErrorContext *xmlerrcxt;
xpath = pgxml_texttoxmlchar(xpathsupp); xpath = pgxml_texttoxmlchar(xpathsupp);
@ -444,8 +443,8 @@ xpath_bool(PG_FUNCTION_ARGS)
text *document = PG_GETARG_TEXT_PP(0); text *document = PG_GETARG_TEXT_PP(0);
text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */ text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
xmlChar *xpath; xmlChar *xpath;
int bRes; volatile int bRes = 0;
volatile xpath_workspace *workspace = NULL; xpath_workspace *volatile workspace = NULL;
PgXmlErrorContext *xmlerrcxt; PgXmlErrorContext *xmlerrcxt;
xpath = pgxml_texttoxmlchar(xpathsupp); xpath = pgxml_texttoxmlchar(xpathsupp);
@ -518,7 +517,7 @@ pgxml_xpath(text *document, xmlChar *xpath, PgXmlErrorContext *xmlerrcxt)
/* Clean up after processing the result of pgxml_xpath() */ /* Clean up after processing the result of pgxml_xpath() */
static void static void
cleanup_workspace(volatile xpath_workspace *workspace) cleanup_workspace(xpath_workspace *workspace)
{ {
if (workspace->res) if (workspace->res)
xmlXPathFreeObject(workspace->res); xmlXPathFreeObject(workspace->res);
@ -537,9 +536,9 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
xmlChar *septag, xmlChar *septag,
xmlChar *plainsep) xmlChar *plainsep)
{ {
volatile xmlChar *xpresstr = NULL; xmlChar *volatile xpresstr = NULL;
text *volatile xpres = NULL;
PgXmlErrorContext *xmlerrcxt; PgXmlErrorContext *xmlerrcxt;
text *xpres;
if (res == NULL) if (res == NULL)
return NULL; return NULL;
@ -578,7 +577,7 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
PG_CATCH(); PG_CATCH();
{ {
if (xpresstr != NULL) if (xpresstr != NULL)
xmlFree((xmlChar *) xpresstr); xmlFree(xpresstr);
pg_xml_done(xmlerrcxt, true); pg_xml_done(xmlerrcxt, true);
@ -587,7 +586,7 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
PG_END_TRY(); PG_END_TRY();
/* Free various storage */ /* Free various storage */
xmlFree((xmlChar *) xpresstr); xmlFree(xpresstr);
pg_xml_done(xmlerrcxt, false); pg_xml_done(xmlerrcxt, false);

View File

@ -48,7 +48,7 @@ xslt_process(PG_FUNCTION_ARGS)
text *doct = PG_GETARG_TEXT_PP(0); text *doct = PG_GETARG_TEXT_PP(0);
text *ssheet = PG_GETARG_TEXT_PP(1); text *ssheet = PG_GETARG_TEXT_PP(1);
text *result; text *volatile result = NULL;
text *paramstr; text *paramstr;
const char **params; const char **params;
PgXmlErrorContext *xmlerrcxt; PgXmlErrorContext *xmlerrcxt;
@ -58,8 +58,7 @@ xslt_process(PG_FUNCTION_ARGS)
volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL; volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
volatile xsltTransformContextPtr xslt_ctxt = NULL; volatile xsltTransformContextPtr xslt_ctxt = NULL;
volatile int resstat = -1; volatile int resstat = -1;
volatile xmlChar *resstr = NULL; xmlChar *volatile resstr = NULL;
int reslen = 0;
if (fcinfo->nargs == 3) if (fcinfo->nargs == 3)
{ {
@ -80,6 +79,7 @@ xslt_process(PG_FUNCTION_ARGS)
{ {
xmlDocPtr ssdoc; xmlDocPtr ssdoc;
bool xslt_sec_prefs_error; bool xslt_sec_prefs_error;
int reslen = 0;
/* Parse document */ /* Parse document */
doctree = xmlReadMemory((char *) VARDATA_ANY(doct), doctree = xmlReadMemory((char *) VARDATA_ANY(doct),
@ -160,7 +160,7 @@ xslt_process(PG_FUNCTION_ARGS)
if (doctree != NULL) if (doctree != NULL)
xmlFreeDoc(doctree); xmlFreeDoc(doctree);
if (resstr != NULL) if (resstr != NULL)
xmlFree((xmlChar *) resstr); xmlFree(resstr);
xsltCleanupGlobals(); xsltCleanupGlobals();
pg_xml_done(xmlerrcxt, true); pg_xml_done(xmlerrcxt, true);
@ -177,7 +177,7 @@ xslt_process(PG_FUNCTION_ARGS)
xsltCleanupGlobals(); xsltCleanupGlobals();
if (resstr) if (resstr)
xmlFree((xmlChar *) resstr); xmlFree(resstr);
pg_xml_done(xmlerrcxt, false); pg_xml_done(xmlerrcxt, false);