Skip to content

LibxmlErrorBufferNeverCleared ​

warning native off by default

Group: Probable bugs

Repeated XML parsing can accumulate internally collected errors. Retrieve and clear the error buffer during the parsing loop. This inspection is disabled by default because the intended policy or use can vary.

Example ​

php
<?php
libxml_use_internal_errors(true); $d=new DOMDocument(); foreach($docs as $xml){$d->loadXML($xml);}

Reported:

  • line 2: Clear collected XML errors during repeated parsing.

Detection ​

  • A resolved libxml_use_internal_errors(true) precedes a loop containing a resolved DOMDocument::loadXML or simplexml_load_string; neither loop nor guaranteed cleanup calls libxml_clear_errors. Require definite enabled state and no unknown control escape.
  • Inspect loop initialization, condition, iterable and increment expressions as well as the body. A header that clears libxml errors or disables internal collection excludes the finding; unknown calls in the header discard collection-state proof. Exclude statically empty iterables and statically false conditions because the parse loop never runs.
  • Cleanup after the loop is guaranteed when a direct statement calling the resolved libxml_clear_errors follows the loop in the same statement list and every statement in between stays in that list. Reading the buffer first (assigning or iterating libxml_get_errors()) is the usual shape and is accepted. The search stops, and the finding is kept, at a statement that can leave the list: return, throw, exit, goto or a label, yield, or a break/continue that is not local to a nested loop or switch (a counted break/continue always counts as leaving). Nested function and class bodies are not searched. A clear nested in a conditional or loop is not guaranteed and does not exclude the finding. The search is bounded to 64 following statements.
  • Exclude parse operations with nested calls in their argument expressions, and DOMDocument construction with calls in constructor arguments. Those calls can clear errors or change error collection before parsing; unsupported argument side effects discard the accumulation proof.
  • Resolve builtin functions, classes, constants and methods; user-defined lookalikes are excluded. Follow only bounded local proof in one lexical scope.
  • Emit one finding per violating operation, not per supporting evidence statement. When the proof is inconclusive, nothing is reported.

Exceptions (no report) ​

  • Do not report unresolved or shadowed builtin names, unknown values, or incomplete receiver/type provenance. Unknown calls, aliases escaping, and intervening writes invalidate local proof. Similar code outside the stated detection contract is not a finding.
  • Both standard suppression forms suppress this native ID.

Fix ​

None. Choosing a repair requires intent or additional runtime information.

PHP versions ​

Available throughout supported PHP 5.3–8.5 where the referenced builtin/extension is available. Respect syntax/version-specific receiver contracts; unavailable APIs are not this rule’s concern.

Configure ​

In custos.json:

json
{
  "rules": {
    "LibxmlErrorBufferNeverCleared": {
      "enabled": true
    }
  }
}

Suppress ​

Before the statement or declaration (or the first statement of the file):

php
// @custos-ignore LibxmlErrorBufferNeverCleared

Released under the MIT License. Native rules and rules modeled on Php Inspections (EA Extended); independent clean-room implementation.