Skip to content

Commit d4bf079

Browse files
committed
Don't crash on uninitialized tidy object
"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.
1 parent d4200ba commit d4bf079

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

ext/tidy/tests/uninitialized.phpt

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
--TEST--
2+
Operations on uninitialized tidy object
3+
--SKIPIF--
4+
<?php if (!extension_loaded("tidy")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
8+
$tidy = new tidy;
9+
try {
10+
var_dump($tidy->getHtmlVer());
11+
} catch (Error $e) {
12+
echo $e->getMessage(), "\n";
13+
}
14+
try {
15+
var_dump($tidy->isXhtml());
16+
} catch (Error $e) {
17+
echo $e->getMessage(), "\n";
18+
}
19+
try {
20+
var_dump($tidy->isXml());
21+
} catch (Error $e) {
22+
echo $e->getMessage(), "\n";
23+
}
24+
25+
?>
26+
--EXPECT--
27+
tidy object is not initialized
28+
tidy object is not initialized
29+
tidy object is not initialized

ext/tidy/tidy.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@
6666
} \
6767
obj = Z_TIDY_P(object); \
6868

69+
#define TIDY_FETCH_INITIALIZED_OBJECT \
70+
TIDY_FETCH_OBJECT; \
71+
if (!obj->ptdoc->initialized) { \
72+
zend_throw_error(NULL, "tidy object is not initialized"); \
73+
return; \
74+
}
75+
6976
#define TIDY_FETCH_ONLY_OBJECT \
7077
PHPTidyObj *obj; \
7178
TIDY_SET_CONTEXT; \
@@ -1474,7 +1481,7 @@ static PHP_FUNCTION(tidy_get_status)
14741481
Get the Detected HTML version for the specified document. */
14751482
static PHP_FUNCTION(tidy_get_html_ver)
14761483
{
1477-
TIDY_FETCH_OBJECT;
1484+
TIDY_FETCH_INITIALIZED_OBJECT;
14781485

14791486
RETURN_LONG(tidyDetectedHtmlVersion(obj->ptdoc->doc));
14801487
}
@@ -1484,7 +1491,7 @@ static PHP_FUNCTION(tidy_get_html_ver)
14841491
Indicates if the document is a XHTML document. */
14851492
static PHP_FUNCTION(tidy_is_xhtml)
14861493
{
1487-
TIDY_FETCH_OBJECT;
1494+
TIDY_FETCH_INITIALIZED_OBJECT;
14881495

14891496
RETURN_BOOL(tidyDetectedXhtml(obj->ptdoc->doc));
14901497
}
@@ -1494,7 +1501,7 @@ static PHP_FUNCTION(tidy_is_xhtml)
14941501
Indicates if the document is a generic (non HTML/XHTML) XML document. */
14951502
static PHP_FUNCTION(tidy_is_xml)
14961503
{
1497-
TIDY_FETCH_OBJECT;
1504+
TIDY_FETCH_INITIALIZED_OBJECT;
14981505

14991506
RETURN_BOOL(tidyDetectedGenericXml(obj->ptdoc->doc));
15001507
}

0 commit comments

Comments
 (0)