xref: /php-src/ext/dom/tests/gh12002.phpt (revision 20ac42e1)
1--TEST--
2GH-12002 (DOMDocument::encoding memory leak with invalid encoding)
3--EXTENSIONS--
4dom
5--FILE--
6<?php
7
8function make_nonconst(string $x) {
9    // Defeat SCCP, even with inlining
10    return str_repeat($x, random_int(1, 1));
11}
12
13$dom = new DOMDocument();
14$dom->encoding = make_nonconst('utf-8');
15var_dump($dom->encoding);
16try {
17    $dom->encoding = make_nonconst('foobar');
18} catch (ValueError $e) {
19    echo $e->getMessage(), "\n";
20}
21var_dump($dom->encoding);
22$dom->encoding = make_nonconst('utf-16le');
23var_dump($dom->encoding);
24try {
25    $dom->encoding = NULL;
26} catch (ValueError $e) {
27    echo $e->getMessage(), "\n";
28}
29var_dump($dom->encoding);
30
31?>
32--EXPECT--
33string(5) "utf-8"
34Invalid document encoding
35string(5) "utf-8"
36string(8) "utf-16le"
37Invalid document encoding
38string(8) "utf-16le"
39