Fix up misuse of "volatile" in contrib/xml2.
authorTom Lane <[email protected]>
Tue, 8 Jul 2025 21:00:34 +0000 (17:00 -0400)
committerTom Lane <[email protected]>
Tue, 8 Jul 2025 21:00:34 +0000 (17:00 -0400)
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.

contrib/xml2/xpath.c
contrib/xml2/xslt_proc.c

index 11216b9b7f9acb0125cb71ad54777c39bfe96c3a..4ac291c8251f7d0982f9f73a53a20d8672291e58 100644 (file)
@@ -54,7 +54,7 @@ static xmlChar *pgxml_texttoxmlchar(text *textstring);
 static xpath_workspace *pgxml_xpath(text *document, xmlChar *xpath,
                                    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)
 {
    text       *tin = PG_GETARG_TEXT_PP(0);
-   text       *tout;
-   volatile xmlChar *tt = NULL;
+   text       *volatile tout = NULL;
+   xmlChar    *volatile tt = NULL;
    PgXmlErrorContext *xmlerrcxt;
 
    xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
@@ -111,7 +111,7 @@ xml_encode_special_chars(PG_FUNCTION_ARGS)
    PG_CATCH();
    {
        if (tt != NULL)
-           xmlFree((xmlChar *) tt);
+           xmlFree(tt);
 
        pg_xml_done(xmlerrcxt, true);
 
@@ -120,7 +120,7 @@ xml_encode_special_chars(PG_FUNCTION_ARGS)
    PG_END_TRY();
 
    if (tt != NULL)
-       xmlFree((xmlChar *) tt);
+       xmlFree(tt);
 
    pg_xml_done(xmlerrcxt, false);
 
@@ -145,11 +145,10 @@ pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
                   xmlChar *plainsep)
 {
    volatile xmlBufferPtr buf = NULL;
-   xmlChar    *result;
-   int         i;
+   xmlChar    *volatile result = NULL;
    PgXmlErrorContext *xmlerrcxt;
 
-   /* spin some error handling */
+   /* spin up some error handling */
    xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
 
    PG_TRY();
@@ -168,7 +167,7 @@ pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
        }
        if (nodeset != NULL)
        {
-           for (i = 0; i < nodeset->nodeNr; i++)
+           for (int i = 0; i < nodeset->nodeNr; i++)
            {
                if (plainsep != NULL)
                {
@@ -257,8 +256,8 @@ xpath_nodeset(PG_FUNCTION_ARGS)
    xmlChar    *toptag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
    xmlChar    *septag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(3));
    xmlChar    *xpath;
-   text       *xpres;
-   volatile xpath_workspace *workspace;
+   text       *volatile xpres = NULL;
+   xpath_workspace *volatile workspace = NULL;
    PgXmlErrorContext *xmlerrcxt;
 
    xpath = pgxml_texttoxmlchar(xpathsupp);
@@ -302,8 +301,8 @@ xpath_list(PG_FUNCTION_ARGS)
    text       *xpathsupp = PG_GETARG_TEXT_PP(1);   /* XPath expression */
    xmlChar    *plainsep = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
    xmlChar    *xpath;
-   text       *xpres;
-   volatile xpath_workspace *workspace;
+   text       *volatile xpres = NULL;
+   xpath_workspace *volatile workspace = NULL;
    PgXmlErrorContext *xmlerrcxt;
 
    xpath = pgxml_texttoxmlchar(xpathsupp);
@@ -344,8 +343,8 @@ xpath_string(PG_FUNCTION_ARGS)
    text       *xpathsupp = PG_GETARG_TEXT_PP(1);   /* XPath expression */
    xmlChar    *xpath;
    int32       pathsize;
-   text       *xpres;
-   volatile xpath_workspace *workspace;
+   text       *volatile xpres = NULL;
+   xpath_workspace *volatile workspace = NULL;
    PgXmlErrorContext *xmlerrcxt;
 
    pathsize = VARSIZE_ANY_EXHDR(xpathsupp);
@@ -398,9 +397,9 @@ xpath_number(PG_FUNCTION_ARGS)
    text       *document = PG_GETARG_TEXT_PP(0);
    text       *xpathsupp = PG_GETARG_TEXT_PP(1);   /* XPath expression */
    xmlChar    *xpath;
-   float4      fRes = 0.0;
-   bool        isNull = false;
-   volatile xpath_workspace *workspace = NULL;
+   volatile float4 fRes = 0.0;
+   volatile bool isNull = false;
+   xpath_workspace *volatile workspace = NULL;
    PgXmlErrorContext *xmlerrcxt;
 
    xpath = pgxml_texttoxmlchar(xpathsupp);
@@ -444,8 +443,8 @@ xpath_bool(PG_FUNCTION_ARGS)
    text       *document = PG_GETARG_TEXT_PP(0);
    text       *xpathsupp = PG_GETARG_TEXT_PP(1);   /* XPath expression */
    xmlChar    *xpath;
-   int         bRes;
-   volatile xpath_workspace *workspace = NULL;
+   volatile int bRes = 0;
+   xpath_workspace *volatile workspace = NULL;
    PgXmlErrorContext *xmlerrcxt;
 
    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() */
 static void
-cleanup_workspace(volatile xpath_workspace *workspace)
+cleanup_workspace(xpath_workspace *workspace)
 {
    if (workspace->res)
        xmlXPathFreeObject(workspace->res);
@@ -537,9 +536,9 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
                     xmlChar *septag,
                     xmlChar *plainsep)
 {
-   volatile xmlChar *xpresstr = NULL;
+   xmlChar    *volatile xpresstr = NULL;
+   text       *volatile xpres = NULL;
    PgXmlErrorContext *xmlerrcxt;
-   text       *xpres;
 
    if (res == NULL)
        return NULL;
@@ -578,7 +577,7 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
    PG_CATCH();
    {
        if (xpresstr != NULL)
-           xmlFree((xmlChar *) xpresstr);
+           xmlFree(xpresstr);
 
        pg_xml_done(xmlerrcxt, true);
 
@@ -587,7 +586,7 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
    PG_END_TRY();
 
    /* Free various storage */
-   xmlFree((xmlChar *) xpresstr);
+   xmlFree(xpresstr);
 
    pg_xml_done(xmlerrcxt, false);
 
index c8e7dd45ed5b4c10ba92a6fa38442e5cb340b2b7..53550c7dc24069042029fb390cf5690b9b0f6357 100644 (file)
@@ -48,7 +48,7 @@ xslt_process(PG_FUNCTION_ARGS)
 
    text       *doct = PG_GETARG_TEXT_PP(0);
    text       *ssheet = PG_GETARG_TEXT_PP(1);
-   text       *result;
+   text       *volatile result = NULL;
    text       *paramstr;
    const char **params;
    PgXmlErrorContext *xmlerrcxt;
@@ -58,8 +58,7 @@ xslt_process(PG_FUNCTION_ARGS)
    volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
    volatile xsltTransformContextPtr xslt_ctxt = NULL;
    volatile int resstat = -1;
-   volatile xmlChar *resstr = NULL;
-   int         reslen = 0;
+   xmlChar    *volatile resstr = NULL;
 
    if (fcinfo->nargs == 3)
    {
@@ -80,6 +79,7 @@ xslt_process(PG_FUNCTION_ARGS)
    {
        xmlDocPtr   ssdoc;
        bool        xslt_sec_prefs_error;
+       int         reslen = 0;
 
        /* Parse document */
        doctree = xmlReadMemory((char *) VARDATA_ANY(doct),
@@ -160,7 +160,7 @@ xslt_process(PG_FUNCTION_ARGS)
        if (doctree != NULL)
            xmlFreeDoc(doctree);
        if (resstr != NULL)
-           xmlFree((xmlChar *) resstr);
+           xmlFree(resstr);
        xsltCleanupGlobals();
 
        pg_xml_done(xmlerrcxt, true);
@@ -177,7 +177,7 @@ xslt_process(PG_FUNCTION_ARGS)
    xsltCleanupGlobals();
 
    if (resstr)
-       xmlFree((xmlChar *) resstr);
+       xmlFree(resstr);
 
    pg_xml_done(xmlerrcxt, false);