@@ -803,12 +803,10 @@ xpath_table(PG_FUNCTION_ARGS)
803803xmlXPathCompExprPtr comppath ;
804804
805805/* Extract the row data as C Strings */
806-
807806spi_tuple = tuptable -> vals [i ];
808807pkey = SPI_getvalue (spi_tuple ,spi_tupdesc ,1 );
809808xmldoc = SPI_getvalue (spi_tuple ,spi_tupdesc ,2 );
810809
811-
812810/*
813811 * Clear the values array, so that not-well-formed documents return
814812 * NULL in all columns.
@@ -822,11 +820,14 @@ xpath_table(PG_FUNCTION_ARGS)
822820values [0 ]= pkey ;
823821
824822/* Parse the document */
825- doctree = xmlParseMemory (xmldoc ,strlen (xmldoc ));
823+ if (xmldoc )
824+ doctree = xmlParseMemory (xmldoc ,strlen (xmldoc ));
825+ else /* treat NULL as not well-formed */
826+ doctree = NULL ;
826827
827828if (doctree == NULL )
828- {/* not well-formed, so output all-NULL tuple */
829-
829+ {
830+ /* not well-formed, so output all-NULL tuple */
830831ret_tuple = BuildTupleFromCStrings (attinmeta ,values );
831832oldcontext = MemoryContextSwitchTo (per_query_ctx );
832833tuplestore_puttuple (tupstore ,ret_tuple );
@@ -918,8 +919,10 @@ xpath_table(PG_FUNCTION_ARGS)
918919
919920xmlFreeDoc (doctree );
920921
921- pfree (pkey );
922- pfree (xmldoc );
922+ if (pkey )
923+ pfree (pkey );
924+ if (xmldoc )
925+ pfree (xmldoc );
923926}
924927
925928xmlCleanupParser ();