Re: BUG #3860: xpath crashes backend when is querying xmlagg result

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alvaro Herrera <alvherre(at)commandprompt(dot)com>
Cc: Sokolov Yura <funny(dot)falcon(at)gmail(dot)com>, pgsql-bugs(at)postgresql(dot)org, Peter Eisentraut <peter_e(at)gmx(dot)net>, Patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: BUG #3860: xpath crashes backend when is querying xmlagg result
Date: 2008-01-11 00:23:06
Message-ID: 12875.1200010986@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-patches

Alvaro Herrera <alvherre(at)commandprompt(dot)com> writes:
> + void
> + AtEOXact_xml(void)
> + {
> + if (LibxmlContext == NULL)
> + return;
> +
> + MemoryContextDelete(LibxmlContext);
> + LibxmlContext = NULL;
> +
> + xmlCleanupParser();
> + }

[ blink... ] Shouldn't that be the other way around? It looks to me
like xmlCleanupParser will be pfree'ing already-deleted data. Did you
test this with CLOBBER_FREED_MEMORY active?

Also, I don't see how this patch fixes what I believe to be the
fundamental problem, which is that we have code paths that invoke
libxml without having previously initialized the alloc support.
I think the sequence

if (LibxmlContext == NULL)
{
create LibxmlContext;
xmlMemSetup(...);
}

ought to be executed before *any* use of libxml stuff (which suggests
factoring it out as a subroutine...)

We also need to do some testing to see if letting the thing go until
transaction end is OK, or whether we will see unacceptable memory leaks
over long series of XML calls. (In particular I suspect that we'd
better zap the context at subtransaction abort; but even without any
error, are we sure that normal operations don't leak memory?)

One thing I was wondering about earlier today is whether libxml isn't
expecting NULL-return-on-failure from the malloc-substitute routine.
If we take control away from it unexpectedly, I wouldn't be a bit
surprised if its data structures are left corrupt. This might lead to
failures during cleanup.

I do like the idea of getting rid of the PG_TRY blocks and the
associated cleanup attempts; with the approach you're converging on
here, we need only assume that xmlCleanupParser() will get rid of
all staticly-allocated pointers and not crash, whereas right now we
are assuming a great deal of libxml stuff still works after an ereport
(which makes throwing ereport from xml_palloc even more risky).

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Alvaro Herrera 2008-01-11 03:41:40 Re: BUG #3860: xpath crashes backend when is querying xmlagg result
Previous Message Tom Lane 2008-01-10 22:00:02 Re: BUG #3865: ERROR: failed to build any 8-way joins

Browse pgsql-patches by date

  From Date Subject
Next Message Alvaro Herrera 2008-01-11 03:41:40 Re: BUG #3860: xpath crashes backend when is querying xmlagg result
Previous Message Alvaro Herrera 2008-01-10 21:16:40 Re: BUG #3860: xpath crashes backend when is querying xmlagg result