Skip to content

Commit 52376c1

Browse files
marandallnikic
authored andcommitted
Fix bug #78563
Make XmlParser final, unclonable and unserializable. Closes phpGH-4778.
1 parent dd61edf commit 52376c1

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

ext/xml/tests/bug78563.phpt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #78563: parsers should not be clonable
3+
--SKIPIF--
4+
<?php include("skipif.inc"); ?>
5+
--FILE--
6+
<?php
7+
8+
$parser = xml_parser_create();
9+
clone $parser;
10+
11+
?>
12+
===DONE===
13+
--EXPECTF--
14+
Fatal error: Uncaught Error: Trying to clone an uncloneable object of class XmlParser in %s:%d
15+
Stack trace:
16+
#0 {main}
17+
thrown in %s on line %d

ext/xml/tests/bug78563_final.phpt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Bug #78563: parsers should not be extendable
3+
--SKIPIF--
4+
<?php include("skipif.inc"); ?>
5+
--FILE--
6+
<?php
7+
8+
class Dummy extends Xmlparser {
9+
10+
}
11+
12+
?>
13+
===DONE===
14+
--EXPECTF--
15+
Fatal error: Class Dummy may not inherit from final class (XmlParser) in %s on line %d

ext/xml/tests/bug78563_serialize.phpt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
Bug #78563: parsers should not be serializable
3+
--SKIPIF--
4+
<?php include("skipif.inc"); ?>
5+
--FILE--
6+
<?php
7+
8+
$parser = xml_parser_create();
9+
serialize($parser);
10+
11+
?>
12+
===DONE===
13+
--EXPECTF--
14+
Fatal error: Uncaught Exception: Serialization of 'XmlParser' is not allowed in %s:%d
15+
Stack trace:
16+
#0 %s(%d): serialize(Object(XmlParser))
17+
#1 {main}
18+
thrown in %s on line %d

ext/xml/xml.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "ext/standard/php_string.h"
2727
#include "ext/standard/info.h"
2828
#include "ext/standard/html.h"
29+
#include "zend_interfaces.h"
2930

3031
#if HAVE_XML
3132

@@ -308,15 +309,18 @@ PHP_MINIT_FUNCTION(xml)
308309
{
309310
zend_class_entry ce;
310311
INIT_CLASS_ENTRY(ce, "XmlParser", xml_parser_methods);
311-
ce.create_object = xml_parser_create_object;
312-
ce.ce_flags |= ZEND_ACC_FINAL;
313312
xml_parser_ce = zend_register_internal_class(&ce);
313+
xml_parser_ce->create_object = xml_parser_create_object;
314+
xml_parser_ce->ce_flags |= ZEND_ACC_FINAL;
315+
xml_parser_ce->serialize = zend_class_serialize_deny;
316+
xml_parser_ce->unserialize = zend_class_unserialize_deny;
314317

315318
memcpy(&xml_parser_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
316319
xml_parser_object_handlers.offset = XtOffsetOf(xml_parser, std);
317320
xml_parser_object_handlers.free_obj = xml_parser_free_obj;
318321
xml_parser_object_handlers.get_gc = xml_parser_get_gc;
319322
xml_parser_object_handlers.get_constructor = xml_parser_get_constructor;
323+
xml_parser_object_handlers.clone_obj = NULL;
320324

321325
REGISTER_LONG_CONSTANT("XML_ERROR_NONE", XML_ERROR_NONE, CONST_CS|CONST_PERSISTENT);
322326
REGISTER_LONG_CONSTANT("XML_ERROR_NO_MEMORY", XML_ERROR_NO_MEMORY, CONST_CS|CONST_PERSISTENT);

0 commit comments

Comments
 (0)