Skip to content

Commit c34def5

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. Closes GH-14862.
1 parent b44ad27 commit c34def5

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ PHP NEWS
5252
. Fix 32-bit wordwrap test failures. (orlitzky)
5353
. Fixed bug GH-14774 (time_sleep_until overflow). (David Carlier)
5454

55+
- Tidy:
56+
. Fix memory leak in tidy_repair_file(). (nielsdos)
57+
5558
- Treewide:
5659
. Fix compatibility with libxml2 2.13.2. (nielsdos)
5760

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)