Skip to content

Commit fe196d4

Browse files
committed
Fix memory leak in tidy_repair_file()
When dealing with a file, we must free the contents if the function fails. While here, also fix the error message because previously it sounded like the filename was too long while in fact the file itself is too large.
1 parent b44ad27 commit fe196d4

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

ext/tidy/tests/parsing_file_too_large.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ try {
4747
} catch (\Throwable $e) {
4848
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
4949
}
50+
51+
try {
52+
tidy_repair_file($path);
53+
} catch (\Throwable $e) {
54+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
55+
}
5056
?>
5157
--CLEAN--
5258
<?php
@@ -58,3 +64,4 @@ int(0)
5864
ValueError: Input string is too long
5965
ValueError: Input string is too long
6066
ValueError: Input string is too long
67+
ValueError: tidy_repair_file(): Argument #1 ($filename) Input string is too long

ext/tidy/tidy.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,12 @@ static void php_tidy_quick_repair(INTERNAL_FUNCTION_PARAMETERS, bool is_file)
304304
}
305305

306306
if (ZEND_SIZE_T_UINT_OVFL(ZSTR_LEN(data))) {
307-
zend_argument_value_error(1, "is too long");
307+
if (is_file) {
308+
zend_string_release_ex(data, false);
309+
zend_argument_value_error(1, "Input string is too long");
310+
} else {
311+
zend_argument_value_error(1, "is too long");
312+
}
308313
RETURN_THROWS();
309314
}
310315

0 commit comments

Comments
 (0)