]> granicus.if.org Git - php/commitdiff
Don't crash on uninitialized tidy object
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 22 Oct 2020 14:04:22 +0000 (16:04 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 22 Oct 2020 14:04:22 +0000 (16:04 +0200)
"Uninitialized" here means that the object was created ordinarily
-- no constructor skipping involved. Most tidy methods seem to
handle this fine, but these three need to be guarded.

ext/tidy/tests/uninitialized.phpt [new file with mode: 0644]
ext/tidy/tidy.c

diff --git a/ext/tidy/tests/uninitialized.phpt b/ext/tidy/tests/uninitialized.phpt
new file mode 100644 (file)
index 0000000..3533f0d
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+Operations on uninitialized tidy object
+--SKIPIF--
+<?php if (!extension_loaded("tidy")) print "skip"; ?>
+--FILE--
+<?php
+
+$tidy = new tidy;
+try {
+    var_dump($tidy->getHtmlVer());
+} catch (Error $e) {
+    echo $e->getMessage(), "\n";
+}
+try {
+    var_dump($tidy->isXhtml());
+} catch (Error $e) {
+    echo $e->getMessage(), "\n";
+}
+try {
+    var_dump($tidy->isXml());
+} catch (Error $e) {
+    echo $e->getMessage(), "\n";
+}
+
+?>
+--EXPECT--
+tidy object is not initialized
+tidy object is not initialized
+tidy object is not initialized
index 60170585ce5910619f310eb7ca8f9345f08fb9de..34fe525a1ea2923e2c6acb0c3ec11ea2e3f7e970 100644 (file)
        }       \
        obj = Z_TIDY_P(object); \
 
+#define TIDY_FETCH_INITIALIZED_OBJECT \
+       TIDY_FETCH_OBJECT; \
+       if (!obj->ptdoc->initialized) { \
+               zend_throw_error(NULL, "tidy object is not initialized"); \
+               return; \
+       }
+
 #define TIDY_FETCH_ONLY_OBJECT \
        PHPTidyObj *obj;        \
        TIDY_SET_CONTEXT; \
@@ -1474,7 +1481,7 @@ static PHP_FUNCTION(tidy_get_status)
    Get the Detected HTML version for the specified document. */
 static PHP_FUNCTION(tidy_get_html_ver)
 {
-       TIDY_FETCH_OBJECT;
+       TIDY_FETCH_INITIALIZED_OBJECT;
 
        RETURN_LONG(tidyDetectedHtmlVersion(obj->ptdoc->doc));
 }
@@ -1484,7 +1491,7 @@ static PHP_FUNCTION(tidy_get_html_ver)
    Indicates if the document is a XHTML document. */
 static PHP_FUNCTION(tidy_is_xhtml)
 {
-       TIDY_FETCH_OBJECT;
+       TIDY_FETCH_INITIALIZED_OBJECT;
 
        RETURN_BOOL(tidyDetectedXhtml(obj->ptdoc->doc));
 }
@@ -1494,7 +1501,7 @@ static PHP_FUNCTION(tidy_is_xhtml)
    Indicates if the document is a generic (non HTML/XHTML) XML document. */
 static PHP_FUNCTION(tidy_is_xml)
 {
-       TIDY_FETCH_OBJECT;
+       TIDY_FETCH_INITIALIZED_OBJECT;
 
        RETURN_BOOL(tidyDetectedGenericXml(obj->ptdoc->doc));
 }