]> granicus.if.org Git - php/commitdiff
Fixed bug #25756 (SimpleXML's validate_schema_file() broken)
authorMoriyoshi Koizumi <moriyoshi@php.net>
Mon, 6 Oct 2003 01:02:28 +0000 (01:02 +0000)
committerMoriyoshi Koizumi <moriyoshi@php.net>
Mon, 6 Oct 2003 01:02:28 +0000 (01:02 +0000)
ext/simplexml/simplexml.c
ext/simplexml/tests/bug25756.phpt [new file with mode: 0644]
ext/simplexml/tests/bug25756.xsd [new file with mode: 0644]
ext/simplexml/tests/bug25756_1.xml [new file with mode: 0644]
ext/simplexml/tests/bug25756_2.xml [new file with mode: 0644]

index a1821feaa5967f985305d61cd4fc974b8a14fe36..8b3980f596e63776b7386a235ac8dee6f9f5e7bc 100644 (file)
@@ -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")) {
diff --git a/ext/simplexml/tests/bug25756.phpt b/ext/simplexml/tests/bug25756.phpt
new file mode 100644 (file)
index 0000000..7331643
--- /dev/null
@@ -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)
diff --git a/ext/simplexml/tests/bug25756.xsd b/ext/simplexml/tests/bug25756.xsd
new file mode 100644 (file)
index 0000000..427b7a1
--- /dev/null
@@ -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>
diff --git a/ext/simplexml/tests/bug25756_1.xml b/ext/simplexml/tests/bug25756_1.xml
new file mode 100644 (file)
index 0000000..33ab30b
--- /dev/null
@@ -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>
diff --git a/ext/simplexml/tests/bug25756_2.xml b/ext/simplexml/tests/bug25756_2.xml
new file mode 100644 (file)
index 0000000..53037ef
--- /dev/null
@@ -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>