# PHP 5 libxml2 Error Handler Broken

- Thread: https://externals.io/message/5851
- Original archive: https://news-web.php.net/php.internals/5851
- Started by Adam Maccabee Trachtenberg on 2003-12-01T07:25:42+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #5851 — Adam Maccabee Trachtenberg — 2003-12-01T07:25:42+00:00

URL: https://externals.io/message/5851#5851

We've defined a custom libxml2 error handler,
php_libxml_error_handler(), that makes error messages go through
php_error() instead of printing to stderr.

Unfortunately, libxml2 doesn't make one call to the error function for
each error. Sometimes it seems to do it once per
character. Unfortunately, this makes the error log look crazy.

For instance:

$s = domDocument::load('http://www.php.net/news.rss');
$xpath = new domXpath($s);
$nodes = $xpath->query('//rss:title');

Gives:

PHP Warning:  XPath error Undefined namespace prefix in /home/adam/xml-test.php on line 7
PHP Warning:  / in /home/adam/xml-test.php on line 7
PHP Warning:  / in /home/adam/xml-test.php on line 7
PHP Warning:  r in /home/adam/xml-test.php on line 7
PHP Warning:  s in /home/adam/xml-test.php on line 7
PHP Warning:  s in /home/adam/xml-test.php on line 7
PHP Warning:  : in /home/adam/xml-test.php on line 7
PHP Warning:  t in /home/adam/xml-test.php on line 7
PHP Warning:  i in /home/adam/xml-test.php on line 7
PHP Warning:  t in /home/adam/xml-test.php on line 7
PHP Warning:  l in /home/adam/xml-test.php on line 7
PHP Warning:  e in /home/adam/xml-test.php on line 7
... (and about 12 more lines after this.)

It should look something like:

PHP Warning:  XPath error Undefined namespace prefix in /home/adam/xml-test.php on line 7
PHP Warning:  //rss:title in /home/adam/xml-test.php on line 7
PHP Warning:             ^ in /home/adam/xml-test.php on line 7

Reviewing libxml2 error messages, I propose we create a libxml2 error
buffer that only gets flushed when libxml2 sends us a string that ends in
"\n". Otherwise, we just append the new error to the buffer.

I have a patch that does this. Unfortunately, I'm unsure of the best
way to maintain the buffer between calls. I'm pretty sure I'm not
allowed to just declare it "static" within
php_libxml_error_handler(). My current patch makes it a global
variable, which seems just as bad.

I'm not worried too much about concurrency issues because if libxml2
doesn't make atomic error calls, we can't fix it on our end. However,
it would be nice if each extension / instance that uses this error
function maintain its own buffer, or is that not really an issue?

The patch is up at:

http://www.trachtenberg.com/patches/php_libxml_error_handler.txt

-adam

-- 
adam@trachtenberg.com
