Skip to content

Commit 12af82b

Browse files
committed
ext/tidy: config options checks strengthening.
1 parent 1aae2ad commit 12af82b

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,14 +222,14 @@ 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

@@ -238,7 +238,7 @@ static int _php_tidy_set_tidy_opt(TidyDoc doc, const char *optname, zval *value)
238238
#else
239239
if (tidyOptIsReadOnly(opt)) {
240240
#endif
241-
php_error_docref(NULL, E_WARNING, "Attempting to set read-only option \"%s\"", optname);
241+
zend_argument_value_error(arg, "Attempting to set read-only option \"%s\"", optname);
242242
return FAILURE;
243243
}
244244

@@ -345,7 +345,7 @@ static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, bool is_file)
345345

346346
TIDY_SET_DEFAULT_CONFIG(doc);
347347

348-
if (php_tidy_apply_config(doc, config_str, config_ht) != SUCCESS) {
348+
if (php_tidy_apply_config(doc, config_str, config_ht, 2) != SUCCESS) {
349349
RETVAL_FALSE;
350350
} else if (enc_len) {
351351
if (tidySetCharEncoding(doc, enc) < 0) {
@@ -783,7 +783,7 @@ static void php_tidy_create_node(INTERNAL_FUNCTION_PARAMETERS, tidy_base_nodetyp
783783
tidy_create_node_object(return_value, obj->ptdoc, node);
784784
}
785785

786-
static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options)
786+
static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options, int arg)
787787
{
788788
zval *opt_val;
789789
zend_string *opt_name;
@@ -793,10 +793,13 @@ static int _php_tidy_apply_config_array(TidyDoc doc, const HashTable *ht_options
793793
if (opt_name == NULL) {
794794
continue;
795795
}
796-
_php_tidy_set_tidy_opt(doc, ZSTR_VAL(opt_name), opt_val);
796+
_php_tidy_set_tidy_opt(doc, ZSTR_VAL(opt_name), opt_val, arg);
797797
} ZEND_HASH_FOREACH_END();
798+
return SUCCESS;
799+
} else {
800+
zend_argument_type_error(arg, "must be of type array with keys as string");
801+
return FAILURE;
798802
}
799-
return SUCCESS;
800803
}
801804

802805
static int php_tidy_parse_string(PHPTidyObj *obj, const char *string, uint32_t len, const char *enc)
@@ -1018,7 +1021,7 @@ PHP_FUNCTION(tidy_parse_string)
10181021
object_init_ex(return_value, tidy_ce_doc);
10191022
obj = Z_TIDY_P(return_value);
10201023

1021-
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) != SUCCESS
1024+
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) != SUCCESS
10221025
|| php_tidy_parse_string(obj, ZSTR_VAL(input), (uint32_t)ZSTR_LEN(input), enc) != SUCCESS) {
10231026
zval_ptr_dtor(return_value);
10241027
RETURN_FALSE;
@@ -1086,7 +1089,7 @@ PHP_FUNCTION(tidy_parse_file)
10861089
object_init_ex(return_value, tidy_ce_doc);
10871090
obj = Z_TIDY_P(return_value);
10881091

1089-
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) != SUCCESS
1092+
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) != SUCCESS
10901093
|| php_tidy_parse_string(obj, ZSTR_VAL(contents), (uint32_t)ZSTR_LEN(contents), enc) != SUCCESS) {
10911094
zval_ptr_dtor(return_value);
10921095
RETVAL_FALSE;
@@ -1381,7 +1384,7 @@ PHP_METHOD(tidy, __construct)
13811384

13821385
zend_error_handling error_handling;
13831386
zend_replace_error_handling(EH_THROW, NULL, &error_handling);
1384-
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) != SUCCESS) {
1387+
if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) != SUCCESS) {
13851388
zend_restore_error_handling(&error_handling);
13861389
zend_string_release_ex(contents, 0);
13871390
RETURN_THROWS();
@@ -1425,7 +1428,7 @@ PHP_METHOD(tidy, parseFile)
14251428
RETURN_THROWS();
14261429
}
14271430

1428-
RETVAL_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) == SUCCESS
1431+
RETVAL_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) == SUCCESS
14291432
&& php_tidy_parse_string(obj, ZSTR_VAL(contents), (uint32_t)ZSTR_LEN(contents), enc) == SUCCESS);
14301433

14311434
zend_string_release_ex(contents, 0);
@@ -1454,7 +1457,7 @@ PHP_METHOD(tidy, parseString)
14541457
TIDY_SET_CONTEXT;
14551458
obj = Z_TIDY_P(object);
14561459

1457-
RETURN_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht) == SUCCESS
1460+
RETURN_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) == SUCCESS
14581461
&& php_tidy_parse_string(obj, ZSTR_VAL(input), (uint32_t)ZSTR_LEN(input), enc) == SUCCESS);
14591462
}
14601463

0 commit comments

Comments
 (0)