Skip to content

Commit 4afe3bc

Browse files
committed
ext/tidy: config options checks strengthening.
1 parent 4c5a6b0 commit 4afe3bc

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

ext/tidy/tests/tidy_error1.phpt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,24 @@ tidy
77
--FILE--
88
<?php
99
$buffer = '<html></html>';
10-
$config = array('bogus' => 'willnotwork');
10+
$config = ['bogus' => 'willnotwork'];
1111

1212
$tidy = new tidy();
13-
var_dump($tidy->parseString($buffer, $config));
13+
14+
try {
15+
$tidy->parseString($buffer, $config);
16+
} catch (\Error $e) {
17+
echo $e->getMessage(), PHP_EOL;
18+
}
19+
20+
$config = ['neither'];
21+
try {
22+
$tidy->parseString($buffer, $config);
23+
} catch (\Error $e) {
24+
echo $e->getMessage(), PHP_EOL;
25+
}
1426

1527
?>
1628
--EXPECTF--
17-
Warning: tidy::parseString(): Unknown Tidy configuration option "bogus" in %s on line %d
18-
bool(true)
29+
tidy::parseString(): Argument #2 ($config) Unknown Tidy configuration option "bogus"
30+
tidy::parseString(): Argument #2 ($config) must be of type array with keys as string

ext/tidy/tidy.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ static void tidy_doc_update_properties(PHPTidyObj *);
130130
static void tidy_add_node_default_properties(PHPTidyObj *);
131131
static void *php_tidy_get_opt_val(PHPTidyDoc *, TidyOption, TidyOptionType *);
132132
static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetypes);
133-
static int _php_tidy_set_tidy_opt(TidyDoc, const char *, zval *);
134-
static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options);
133+
static int _php_tidy_set_tidy_opt(TidyDoc, const char *, zval *, int arg);
134+
static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options, int arg);
135135
static PHP_INI_MH(php_tidy_set_clean_output);
136136
static void php_tidy_clean_output_start(const char *name, size_t name_len);
137137
static php_output_handler *php_tidy_output_handler_init(const char *handler_name, size_t handler_name_len, size_t chunk_size, int flags);
@@ -209,10 +209,10 @@ static void php_tidy_load_config(TidyDoc doc, const char *path)
209209
}
210210
}
211211

212-
static zend_result php_tidy_apply_config(TidyDoc doc, const zend_string *str_string, const HashTable *ht_options)
212+
static zend_result php_tidy_apply_config(TidyDoc doc, const zend_string *str_string, const HashTable *ht_options, int arg)
213213
{
214214
if (ht_options) {
215-
return _php_tidy_apply_config_array(doc, ht_options);
215+
return _php_tidy_apply_config_array(doc, ht_options, arg);
216216
} else if (str_string) {
217217
if (php_check_open_basedir(ZSTR_VAL(str_string))) {
218218
return FAILURE;
@@ -222,19 +222,19 @@ static zend_result php_tidy_apply_config(TidyDoc doc, const zend_string *str_str
222222
return SUCCESS;
223223
}
224224

225-
static int _php_tidy_set_tidy_opt(TidyDoc doc, const char *optname, zval *value)
225+
static int _php_tidy_set_tidy_opt(TidyDoc doc, const char *optname, zval *value, int arg)
226226
{
227227
TidyOption opt = tidyGetOptionByName(doc, optname);
228228
zend_string *str, *tmp_str;
229229
zend_long lval;
230230

231231
if (!opt) {
232-
php_error_docref(NULL, E_WARNING, "Unknown Tidy configuration option \"%s\"", optname);
232+
zend_argument_value_error(arg, "Unknown Tidy configuration option \"%s\"", optname);
233233
return FAILURE;
234234
}
235235

236236
if (tidyOptIsReadOnly(opt)) {
237-
php_error_docref(NULL, E_WARNING, "Attempting to set read-only option \"%s\"", optname);
237+
zend_argument_value_error(arg, "Attempting to set read-only option \"%s\"", optname);
238238
return FAILURE;
239239
}
240240

@@ -341,7 +341,7 @@ static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, bool is_file)
341341

342342
TIDY_SET_DEFAULT_CONFIG(doc);
343343

344-
if (php_tidy_apply_config(doc, config_str, config_ht) != SUCCESS) {
344+
if (php_tidy_apply_config(doc, config_str, config_ht, 2) != SUCCESS) {
345345
RETVAL_FALSE;
346346
} else if (enc_len) {
347347
if (tidySetCharEncoding(doc, enc) < 0) {
@@ -779,7 +779,7 @@ static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetyp
779779
tidy_create_node_object(return_value, obj->ptdoc, node);
780780
}
781781

782-
static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options)
782+
static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options, int arg)
783783
{
784784
zval *opt_val;
785785
zend_string *opt_name;
@@ -789,10 +789,13 @@ static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options
789789
if (opt_name == NULL) {
790790
continue;
791791
}
792-
_php_tidy_set_tidy_opt(doc, ZSTR_VAL(opt_name), opt_val);
792+
_php_tidy_set_tidy_opt(doc, ZSTR_VAL(opt_name), opt_val, arg);
793793
} ZEND_HASH_FOREACH_END();
794+
return SUCCESS;
795+
} else {
796+
zend_argument_type_error(arg, "must be of type array with keys as string");
797+
return FAILURE;
794798
}
795-
return SUCCESS;
796799
}
797800

798801
static int php_tidy_parse_string(PHPTidyObj *obj, const char *string, uint32_t len, const char *enc)
@@ -1014,7 +1017,7 @@ PHP_FUNCTION(tidy_parse_string)
10141017
object_init_ex(return_value, tidy_ce_doc);
10151018
obj = Z_TIDY_P(return_value);
10161019

1017-
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) != SUCCESS
1020+
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) != SUCCESS
10181021
|| php_tidy_parse_string(obj, ZSTR_VAL(input), (uint32_t)ZSTR_LEN(input), enc) != SUCCESS) {
10191022
zval_ptr_dtor(return_value);
10201023
RETURN_FALSE;
@@ -1082,7 +1085,7 @@ PHP_FUNCTION(tidy_parse_file)
10821085
object_init_ex(return_value, tidy_ce_doc);
10831086
obj = Z_TIDY_P(return_value);
10841087

1085-
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) != SUCCESS
1088+
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) != SUCCESS
10861089
|| php_tidy_parse_string(obj, ZSTR_VAL(contents), (uint32_t)ZSTR_LEN(contents), enc) != SUCCESS) {
10871090
zval_ptr_dtor(return_value);
10881091
RETVAL_FALSE;
@@ -1377,7 +1380,7 @@ PHP_METHOD(tidy, __construct)
13771380

13781381
zend_error_handling error_handling;
13791382
zend_replace_error_handling(EH_THROW, NULL, &error_handling);
1380-
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) != SUCCESS) {
1383+
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) != SUCCESS) {
13811384
zend_restore_error_handling(&error_handling);
13821385
zend_string_release_ex(contents, 0);
13831386
RETURN_THROWS();
@@ -1421,7 +1424,7 @@ PHP_METHOD(tidy, parseFile)
14211424
RETURN_THROWS();
14221425
}
14231426

1424-
RETVAL_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) == SUCCESS
1427+
RETVAL_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) == SUCCESS
14251428
&& php_tidy_parse_string(obj, ZSTR_VAL(contents), (uint32_t)ZSTR_LEN(contents), enc) == SUCCESS);
14261429

14271430
zend_string_release_ex(contents, 0);
@@ -1450,7 +1453,7 @@ PHP_METHOD(tidy, parseString)
14501453
TIDY_SET_CONTEXT;
14511454
obj = Z_TIDY_P(object);
14521455

1453-
RETURN_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) == SUCCESS
1456+
RETURN_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) == SUCCESS
14541457
&& php_tidy_parse_string(obj, ZSTR_VAL(input), (uint32_t)ZSTR_LEN(input), enc) == SUCCESS);
14551458
}
14561459

0 commit comments

Comments
 (0)