php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73670 simplexml_load_string returns `FALSE` but does not set error (for empty strings
Submitted: 2016-12-07 01:18 UTC Modified: 2016-12-07 02:15 UTC
From: zerkms at zerkms dot ru Assigned:
Status: Open Package: SimpleXML related
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-12-07 01:18 UTC] zerkms at zerkms dot ru
Description:
------------
`simplexml_load_string()` documentation states

> Returns an object of class SimpleXMLElement with properties containing the data held within the xml document, or FALSE on failure.

So I expect if `FALSE` is returned - it was failed to parse xml, so the `libxml_get_last_error` returns the actual error.

What happens instead - for the empty string the `simplexml_load_string` returns `false` and `libxml_get_last_error` returns previous non actual error.

It should be either a documentation additionally clarify the case, or the implementation behave accordingly.

Test script:
---------------
$previous = libxml_use_internal_errors(true);
$doc = simplexml_load_string('foo');
libxml_use_internal_errors($previous);

$result = libxml_get_last_error();

$previous = libxml_use_internal_errors(true);
$doc = simplexml_load_string('');
libxml_use_internal_errors($previous);

$result = libxml_get_last_error();

var_dump($doc, $result);

Expected result:
----------------
bool(false)
bool(false)

Actual result:
--------------
bool(false)
object(LibXMLError)#2 (6) {
  ["level"]=>
  int(3)
  ["code"]=>
  int(4)
  ["column"]=>
  int(1)
  ["message"]=>
  string(34) "Start tag expected, '<' not found
"
  ["file"]=>
  string(0) ""
  ["line"]=>
  int(1)
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-07 02:15 UTC] requinix@php.net
-Type: Bug +Type: Documentation Problem
 [2016-12-07 02:15 UTC] requinix@php.net
libxml2 does not set error information if the input string is empty. It would be easy to update the Return Values to indicate that parsing an empty string returns false with no additional error messages.

Error information does not clear automatically. Since no new errors were added, libxml_get_last_error() will return the error from the previous call. Use libxml_clear_errors() to avoid confusion.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 04:01:28 2024 UTC