Skip to content

ext/tidy: throwing exception instead of warning for php calls. #19417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/tidy/php_tidy.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef PHP_TIDY_H
#define PHP_TIDY_H

extern zend_class_entry *tidy_ce_exception;
extern zend_module_entry tidy_module_entry;
#define phpext_tidy_ptr &tidy_module_entry

Expand Down
28 changes: 17 additions & 11 deletions ext/tidy/tests/parsing_inexistent_file.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ tidy
<?php

$tidy = new tidy;
var_dump($tidy->parseFile("does_not_exist.html"));

var_dump(tidy_parse_file("does_not_exist.html"));
try {
$tidy->parseFile("does_not_exist.html");
} catch (Throwable $e) {
echo $e::class . '::' . $e->getMessage(), PHP_EOL;
}

try {
tidy_parse_file("does_not_exist.html");
} catch (Throwable $e) {
echo $e::class . '::' . $e->getMessage(), PHP_EOL;
}

try {
$tidy = new tidy("does_not_exist.html");
} catch (Exception $e) {
echo $e->getMessage(), "\n";
} catch (Throwable $e) {
echo $e::class . '::' . $e->getMessage(), PHP_EOL;
}
?>
--EXPECTF--
Warning: tidy::parseFile(): Cannot load "does_not_exist.html" into memory in %s on line %d
bool(false)

Warning: tidy_parse_file(): Cannot load "does_not_exist.html" into memory in %s on line %d
bool(false)
Cannot load "does_not_exist.html" into memory
--EXPECT--
TidyException::Cannot load "does_not_exist.html" into memory
TidyException::Cannot load "does_not_exist.html" into memory
TidyException::Cannot load "does_not_exist.html" into memory
25 changes: 15 additions & 10 deletions ext/tidy/tidy.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ static zend_class_entry *tidy_ce_doc, *tidy_ce_node;
static zend_object_handlers tidy_object_handlers_doc;
static zend_object_handlers tidy_object_handlers_node;

zend_class_entry *tidy_ce_exception;

zend_module_entry tidy_module_entry = {
STANDARD_MODULE_HEADER,
"tidy",
Expand Down Expand Up @@ -826,6 +828,9 @@ static PHP_MINIT_FUNCTION(tidy)
tidy_object_handlers_node.offset = tidy_object_handlers_doc.offset = XtOffsetOf(PHPTidyObj, std);
tidy_object_handlers_node.free_obj = tidy_object_handlers_doc.free_obj = tidy_object_free_storage;

tidy_ce_exception = register_class_TidyException(zend_ce_exception);
tidy_ce_exception->create_object = zend_ce_exception->create_object;

register_tidy_symbols(module_number);

php_output_handler_alias_register(ZEND_STRL("ob_tidyhandler"), php_tidy_output_handler_init);
Expand Down Expand Up @@ -1051,12 +1056,12 @@ PHP_FUNCTION(tidy_parse_file)
ZEND_PARSE_PARAMETERS_END();

if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path))) {
php_error_docref(NULL, E_WARNING, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
RETURN_FALSE;
zend_throw_error(tidy_ce_exception, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
RETURN_THROWS();
}

if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(contents))) {
zend_string_release_ex(contents, 0);
zend_string_release_ex(contents, false);
zend_value_error("File content is too long");
RETURN_THROWS();
}
Expand Down Expand Up @@ -1327,11 +1332,9 @@ PHP_METHOD(tidy, __construct)
Z_PARAM_BOOL(use_include_path)
ZEND_PARSE_PARAMETERS_END();

obj = Z_TIDY_P(ZEND_THIS);

if (inputfile) {
if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path))) {
zend_throw_error(zend_ce_exception, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
zend_throw_error(tidy_ce_exception, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
RETURN_THROWS();
}

Expand All @@ -1343,6 +1346,8 @@ PHP_METHOD(tidy, __construct)

zend_error_handling error_handling;
zend_replace_error_handling(EH_THROW, NULL, &error_handling);
obj = Z_TIDY_P(ZEND_THIS);

if (php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) != SUCCESS) {
zend_restore_error_handling(&error_handling);
zend_string_release_ex(contents, 0);
Expand Down Expand Up @@ -1373,11 +1378,9 @@ PHP_METHOD(tidy, parseFile)
Z_PARAM_BOOL(use_include_path)
ZEND_PARSE_PARAMETERS_END();

obj = Z_TIDY_P(ZEND_THIS);

if (!(contents = php_tidy_file_to_mem(inputfile, use_include_path))) {
php_error_docref(NULL, E_WARNING, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
RETURN_FALSE;
zend_throw_error(tidy_ce_exception, "Cannot load \"%s\" into memory%s", ZSTR_VAL(inputfile), (use_include_path) ? " (using include path)" : "");
RETURN_THROWS();
}

if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(contents))) {
Expand All @@ -1386,6 +1389,8 @@ PHP_METHOD(tidy, parseFile)
RETURN_THROWS();
}

obj = Z_TIDY_P(ZEND_THIS);

RETVAL_BOOL(php_tidy_apply_config(obj->ptdoc->doc, options_str, options_ht, 2) == SUCCESS
&& php_tidy_parse_string(obj, contents, enc) == SUCCESS);

Expand Down
4 changes: 4 additions & 0 deletions ext/tidy/tidy.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,10 @@
const TIDY_TAG_VIDEO = UNKNOWN;
# endif

class TidyException extends Exception
{
}

function tidy_parse_string(string $string, array|string|null $config = null, ?string $encoding = null): tidy|false {}

function tidy_get_error_buffer(tidy $tidy): string|false {}
Expand Down
12 changes: 11 additions & 1 deletion ext/tidy/tidy_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.