Skip to content

Commit a07375e

Browse files
committed
Fix core dump in contrib/xml2's xpath_table() when the input query returns
a NULL value. Per bug #4058.
1 parent e5fab26 commit a07375e

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

contrib/xml2/xpath.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,10 @@ xpath_table(PG_FUNCTION_ARGS)
808808
xmlXPathCompExprPtr comppath;
809809

810810
/* Extract the row data as C Strings */
811-
812811
spi_tuple = tuptable->vals[i];
813812
pkey = SPI_getvalue(spi_tuple, spi_tupdesc, 1);
814813
xmldoc = SPI_getvalue(spi_tuple, spi_tupdesc, 2);
815814

816-
817815
/*
818816
* Clear the values array, so that not-well-formed documents return
819817
* NULL in all columns.
@@ -827,11 +825,14 @@ xpath_table(PG_FUNCTION_ARGS)
827825
values[0] = pkey;
828826

829827
/* Parse the document */
830-
doctree = xmlParseMemory(xmldoc, strlen(xmldoc));
828+
if (xmldoc)
829+
doctree = xmlParseMemory(xmldoc, strlen(xmldoc));
830+
else /* treat NULL as not well-formed */
831+
doctree = NULL;
831832

832833
if (doctree == NULL)
833-
{ /* not well-formed, so output all-NULL tuple */
834-
834+
{
835+
/* not well-formed, so output all-NULL tuple */
835836
ret_tuple = BuildTupleFromCStrings(attinmeta, values);
836837
oldcontext = MemoryContextSwitchTo(per_query_ctx);
837838
tuplestore_puttuple(tupstore, ret_tuple);
@@ -923,8 +924,10 @@ xpath_table(PG_FUNCTION_ARGS)
923924

924925
xmlFreeDoc(doctree);
925926

926-
pfree(pkey);
927-
pfree(xmldoc);
927+
if (pkey)
928+
pfree(pkey);
929+
if (xmldoc)
930+
pfree(xmldoc);
928931
}
929932

930933
xmlCleanupParser();

0 commit comments

Comments
 (0)