Skip to content

Commit 9300188

Browse files
committed
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 7320611, so no need for back-patch.
1 parent e03c952 commit 9300188

File tree

2 files changed

+29
-30
lines changed

2 files changed

+29
-30
lines changed

contrib/xml2/xpath.c

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static xmlChar *pgxml_texttoxmlchar(text *textstring);
5454
static xpath_workspace *pgxml_xpath(text *document, xmlChar *xpath,
5555
PgXmlErrorContext *xmlerrcxt);
5656

57-
static void cleanup_workspace(volatile xpath_workspace *workspace);
57+
static void cleanup_workspace(xpath_workspace *workspace);
5858

5959

6060
/*
@@ -88,8 +88,8 @@ Datum
8888
xml_encode_special_chars(PG_FUNCTION_ARGS)
8989
{
9090
text *tin = PG_GETARG_TEXT_PP(0);
91-
text *tout;
92-
volatile xmlChar *tt = NULL;
91+
text *volatile tout = NULL;
92+
xmlChar *volatile tt = NULL;
9393
PgXmlErrorContext *xmlerrcxt;
9494

9595
xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
@@ -111,7 +111,7 @@ xml_encode_special_chars(PG_FUNCTION_ARGS)
111111
PG_CATCH();
112112
{
113113
if (tt != NULL)
114-
xmlFree((xmlChar *) tt);
114+
xmlFree(tt);
115115

116116
pg_xml_done(xmlerrcxt, true);
117117

@@ -120,7 +120,7 @@ xml_encode_special_chars(PG_FUNCTION_ARGS)
120120
PG_END_TRY();
121121

122122
if (tt != NULL)
123-
xmlFree((xmlChar *) tt);
123+
xmlFree(tt);
124124

125125
pg_xml_done(xmlerrcxt, false);
126126

@@ -145,11 +145,10 @@ pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
145145
xmlChar *plainsep)
146146
{
147147
volatile xmlBufferPtr buf = NULL;
148-
xmlChar *result;
149-
int i;
148+
xmlChar *volatile result = NULL;
150149
PgXmlErrorContext *xmlerrcxt;
151150

152-
/* spin some error handling */
151+
/* spin up some error handling */
153152
xmlerrcxt = pg_xml_init(PG_XML_STRICTNESS_ALL);
154153

155154
PG_TRY();
@@ -168,7 +167,7 @@ pgxmlNodeSetToText(xmlNodeSetPtr nodeset,
168167
}
169168
if (nodeset != NULL)
170169
{
171-
for (i = 0; i < nodeset->nodeNr; i++)
170+
for (int i = 0; i < nodeset->nodeNr; i++)
172171
{
173172
if (plainsep != NULL)
174173
{
@@ -257,8 +256,8 @@ xpath_nodeset(PG_FUNCTION_ARGS)
257256
xmlChar *toptag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
258257
xmlChar *septag = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(3));
259258
xmlChar *xpath;
260-
text *xpres;
261-
volatile xpath_workspace *workspace;
259+
text *volatile xpres = NULL;
260+
xpath_workspace *volatile workspace = NULL;
262261
PgXmlErrorContext *xmlerrcxt;
263262

264263
xpath = pgxml_texttoxmlchar(xpathsupp);
@@ -302,8 +301,8 @@ xpath_list(PG_FUNCTION_ARGS)
302301
text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
303302
xmlChar *plainsep = pgxml_texttoxmlchar(PG_GETARG_TEXT_PP(2));
304303
xmlChar *xpath;
305-
text *xpres;
306-
volatile xpath_workspace *workspace;
304+
text *volatile xpres = NULL;
305+
xpath_workspace *volatile workspace = NULL;
307306
PgXmlErrorContext *xmlerrcxt;
308307

309308
xpath = pgxml_texttoxmlchar(xpathsupp);
@@ -344,8 +343,8 @@ xpath_string(PG_FUNCTION_ARGS)
344343
text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
345344
xmlChar *xpath;
346345
int32 pathsize;
347-
text *xpres;
348-
volatile xpath_workspace *workspace;
346+
text *volatile xpres = NULL;
347+
xpath_workspace *volatile workspace = NULL;
349348
PgXmlErrorContext *xmlerrcxt;
350349

351350
pathsize = VARSIZE_ANY_EXHDR(xpathsupp);
@@ -398,9 +397,9 @@ xpath_number(PG_FUNCTION_ARGS)
398397
text *document = PG_GETARG_TEXT_PP(0);
399398
text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
400399
xmlChar *xpath;
401-
float4 fRes = 0.0;
402-
bool isNull = false;
403-
volatile xpath_workspace *workspace = NULL;
400+
volatile float4 fRes = 0.0;
401+
volatile bool isNull = false;
402+
xpath_workspace *volatile workspace = NULL;
404403
PgXmlErrorContext *xmlerrcxt;
405404

406405
xpath = pgxml_texttoxmlchar(xpathsupp);
@@ -444,8 +443,8 @@ xpath_bool(PG_FUNCTION_ARGS)
444443
text *document = PG_GETARG_TEXT_PP(0);
445444
text *xpathsupp = PG_GETARG_TEXT_PP(1); /* XPath expression */
446445
xmlChar *xpath;
447-
int bRes;
448-
volatile xpath_workspace *workspace = NULL;
446+
volatile int bRes = 0;
447+
xpath_workspace *volatile workspace = NULL;
449448
PgXmlErrorContext *xmlerrcxt;
450449

451450
xpath = pgxml_texttoxmlchar(xpathsupp);
@@ -518,7 +517,7 @@ pgxml_xpath(text *document, xmlChar *xpath, PgXmlErrorContext *xmlerrcxt)
518517

519518
/* Clean up after processing the result of pgxml_xpath() */
520519
static void
521-
cleanup_workspace(volatile xpath_workspace *workspace)
520+
cleanup_workspace(xpath_workspace *workspace)
522521
{
523522
if (workspace->res)
524523
xmlXPathFreeObject(workspace->res);
@@ -537,9 +536,9 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
537536
xmlChar *septag,
538537
xmlChar *plainsep)
539538
{
540-
volatile xmlChar *xpresstr = NULL;
539+
xmlChar *volatile xpresstr = NULL;
540+
text *volatile xpres = NULL;
541541
PgXmlErrorContext *xmlerrcxt;
542-
text *xpres;
543542

544543
if (res == NULL)
545544
return NULL;
@@ -578,7 +577,7 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
578577
PG_CATCH();
579578
{
580579
if (xpresstr != NULL)
581-
xmlFree((xmlChar *) xpresstr);
580+
xmlFree(xpresstr);
582581

583582
pg_xml_done(xmlerrcxt, true);
584583

@@ -587,7 +586,7 @@ pgxml_result_to_text(xmlXPathObjectPtr res,
587586
PG_END_TRY();
588587

589588
/* Free various storage */
590-
xmlFree((xmlChar *) xpresstr);
589+
xmlFree(xpresstr);
591590

592591
pg_xml_done(xmlerrcxt, false);
593592

contrib/xml2/xslt_proc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ xslt_process(PG_FUNCTION_ARGS)
4848

4949
text *doct = PG_GETARG_TEXT_PP(0);
5050
text *ssheet = PG_GETARG_TEXT_PP(1);
51-
text *result;
51+
text *volatile result = NULL;
5252
text *paramstr;
5353
const char **params;
5454
PgXmlErrorContext *xmlerrcxt;
@@ -58,8 +58,7 @@ xslt_process(PG_FUNCTION_ARGS)
5858
volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
5959
volatile xsltTransformContextPtr xslt_ctxt = NULL;
6060
volatile int resstat = -1;
61-
volatile xmlChar *resstr = NULL;
62-
int reslen = 0;
61+
xmlChar *volatile resstr = NULL;
6362

6463
if (fcinfo->nargs == 3)
6564
{
@@ -80,6 +79,7 @@ xslt_process(PG_FUNCTION_ARGS)
8079
{
8180
xmlDocPtr ssdoc;
8281
bool xslt_sec_prefs_error;
82+
int reslen = 0;
8383

8484
/* Parse document */
8585
doctree = xmlReadMemory((char *) VARDATA_ANY(doct),
@@ -160,7 +160,7 @@ xslt_process(PG_FUNCTION_ARGS)
160160
if (doctree != NULL)
161161
xmlFreeDoc(doctree);
162162
if (resstr != NULL)
163-
xmlFree((xmlChar *) resstr);
163+
xmlFree(resstr);
164164
xsltCleanupGlobals();
165165

166166
pg_xml_done(xmlerrcxt, true);
@@ -177,7 +177,7 @@ xslt_process(PG_FUNCTION_ARGS)
177177
xsltCleanupGlobals();
178178

179179
if (resstr)
180-
xmlFree((xmlChar *) resstr);
180+
xmlFree(resstr);
181181

182182
pg_xml_done(xmlerrcxt, false);
183183

0 commit comments

Comments
 (0)