Browse Source

Fixed bug #25756 (SimpleXML's validate_schema_file() broken)

PEAR_1_4DEV
Moriyoshi Koizumi 23 years ago
parent
commit
503d74aa29
  1. 42
      ext/simplexml/simplexml.c
  2. 68
      ext/simplexml/tests/bug25756.phpt
  3. 24
      ext/simplexml/tests/bug25756.xsd
  4. 13
      ext/simplexml/tests/bug25756_1.xml
  5. 13
      ext/simplexml/tests/bug25756_2.xml

42
ext/simplexml/simplexml.c

@ -538,7 +538,7 @@ simplexml_ce_xpath_search(INTERNAL_FUNCTION_PARAMETERS)
#define SCHEMA_BLOB 1
#define SCHEMA_OBJECT 2
#ifdef xmlSchemaParserCtxtPtr
#ifdef LIBXML_SCHEMAS_ENABLED
/* {{{ simplexml_ce_schema_validate_file()
*/
@ -562,28 +562,48 @@ simplexml_ce_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type)
case SCHEMA_FILE:
convert_to_string_ex(&source);
parser = xmlSchemaNewParserCtxt(Z_STRVAL_P(source));
if (parser == NULL) {
php_error_docref1(NULL TSRMLS_CC, Z_STRVAL_P(source), E_WARNING, "Unable to load XML Schema file");
RETURN_FALSE;
}
sptr = xmlSchemaParse(parser);
xmlSchemaFreeParserCtxt(parser);
break;
case SCHEMA_BLOB:
convert_to_string_ex(&source);
parser = xmlSchemaNewMemParserCtxt(Z_STRVAL_P(source), Z_STRLEN_P(source));
sptr = xmlSchemaParse(parser);
xmlSchemaFreeParserCtxt(parser);
break;
}
if (sptr == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Malformed XML Schema");
xmlSchemaFreeParserCtxt(parser);
RETURN_FALSE;
}
vptr = xmlSchemaNewValidCtxt(sptr);
is_valid = xmlSchemaValidateDoc(vptr, (xmlDocPtr) sxe->document->ptr);
xmlSchemaFree(sptr);
xmlSchemaFreeValidCtxt(vptr);
xmlSchemaFreeParserCtxt(parser);
if (is_valid) {
RETURN_TRUE;
} else {
if (vptr == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to create XML Schema validation context");
xmlSchemaFreeParserCtxt(parser);
RETURN_FALSE;
}
switch (xmlSchemaValidateDoc(vptr, (xmlDocPtr) sxe->document->ptr)) {
case 0: /* validated */
RETVAL_TRUE;
break;
case -1: /* internal error */
RETVAL_FALSE;
break;
default: /* error */
RETVAL_TRUE;
break;
}
xmlSchemaFree(sptr);
xmlSchemaFreeValidCtxt(vptr);
xmlSchemaFreeParserCtxt(parser);
}
/* }}} */
@ -660,7 +680,7 @@ sxe_call_method(char *method, INTERNAL_FUNCTION_PARAMETERS)
{
if (!strcmp(method, "xsearch")) {
simplexml_ce_xpath_search(INTERNAL_FUNCTION_PARAM_PASSTHRU);
#ifdef xmlSchemaParserCtxtPtr
#ifdef LIBXML_SCHEMAS_ENABLED
} else if (!strcmp(method, "validate_schema_file")) {
simplexml_ce_schema_validate(INTERNAL_FUNCTION_PARAM_PASSTHRU, SCHEMA_FILE);
} else if (!strcmp(method, "validate_schema_buffer")) {

68
ext/simplexml/tests/bug25756.phpt

@ -0,0 +1,68 @@
--TEST--
Bug #25756 (validate_schema_file() broken)
--FILE--
<?php
$dir = dirname(__FILE__);
$valid_schema_file = "$dir/bug25756.xsd";
$invalid_schema_file = "$dir/bug25756_1.xml";
$xml_file_1 = "$dir/bug25756_1.xml";
$xml_file_2 = "$dir/bug25756_2.xml";
$s = simplexml_load_file($xml_file_1);
var_dump($s);
var_dump($s->validate_schema_file($valid_schema_file));
var_dump($s->validate_schema_file($invalid_schema_file));
$s = simplexml_load_file($xml_file_2);
var_dump($s);
var_dump($s->validate_schema_file($valid_schema_file));
?>
--EXPECTF--
object(simplexml_element)#1 (1) {
["items"]=>
object(simplexml_element)#2 (1) {
["item"]=>
array(2) {
[0]=>
object(simplexml_element)#3 (2) {
["product-name"]=>
string(3) "abc"
["quantity"]=>
string(3) "123"
}
[1]=>
object(simplexml_element)#4 (2) {
["product-name"]=>
string(3) "def"
["quantity"]=>
string(3) "456"
}
}
}
}
bool(true)
Warning: Unknown: Malformed XML Schema in %s on line %d
bool(false)
object(simplexml_element)#5 (1) {
["items"]=>
object(simplexml_element)#1 (1) {
["item"]=>
array(2) {
[0]=>
object(simplexml_element)#6 (2) {
["product-name"]=>
string(3) "abc"
["quantity"]=>
string(3) "abc"
}
[1]=>
object(simplexml_element)#7 (2) {
["product-name"]=>
string(3) "abc"
["quantity"]=>
string(3) "123"
}
}
}
}
bool(false)

24
ext/simplexml/tests/bug25756.xsd

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="foo" type="foo-type" />
<xsd:complexType name="item-type">
<xsd:all>
<xsd:element name="product-name" type="xsd:string"
minOccurs="1" maxOccurs="1"/>
<xsd:element name="quantity" type="xsd:decimal"
minOccurs="1" maxOccurs="1"/>
</xsd:all>
</xsd:complexType>
<xsd:complexType name="foo-type">
<xsd:sequence>
<xsd:element name="items" minoccurs="1" maxOccurs="1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="item" type="item-type"
minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>

13
ext/simplexml/tests/bug25756_1.xml

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<foo>
<items>
<item>
<product-name>abc</product-name>
<quantity>123</quantity>
</item>
<item>
<product-name>def</product-name>
<quantity>456</quantity>
</item>
</items>
</foo>

13
ext/simplexml/tests/bug25756_2.xml

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<foo>
<items>
<item>
<product-name>abc</product-name>
<quantity>abc</quantity>
</item>
<item>
<product-name>abc</product-name>
<quantity>123</quantity>
</item>
</items>
</foo>
Loading…
Cancel
Save