-
Notifications
You must be signed in to change notification settings - Fork 116
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
Fixed memory leak in HTML5::saveHTML() #187
Conversation
thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just saw your Tweet and I saw that leaks could also occurs in save method if an exception is thrown.
Working with streams, there should always be a one-one relation between fopen
and fclose
.
@@ -234,6 +234,10 @@ public function saveHTML($dom, $options = array()) | |||
$stream = fopen('php://temp', 'wb'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fopen
could return false
$stream = fopen('php://temp', 'wb');
if ($stream === false) {
// Acts accordingly I would throw an exception
throw new \RuntimeException('Cannot create temporary stream');
}
try {
$this->save($dom, $stream, array_merge($this->defaultOptions, $options));
return stream_get_contents($stream, -1, 0);
} finally {
fclose($stream);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good catch. Would you mind to submit a pr ?
If you can't, no problem, I'll do it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I do not use this library and I am already occupied on other things. Feel free to submit a PR to fix it.
Btw, the same trick should be used in the above method where return from fopen is not checked for correctness.
I would split save
method in with a private method doSaveStream(...)
and save
calling this directly whether $file
is a resource or a filename.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arf, this lib should run on PHP 5.3 (yes :p )
I can not do that... I think we will keep this code like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$stream = fopen('php://temp', 'wb'); | |
$stream = fopen('php://temp', 'wb'); | |
if ($stream === false) { | |
// Acts accordingly I would throw an exception | |
throw new \RuntimeException('Cannot create temporary stream'); | |
} | |
try { | |
$this->save($dom, $stream, array_merge($this->defaultOptions, $options)); | |
$result = stream_get_contents($stream, -1, 0); | |
} catch (\Exception $exception) { | |
fclose($stream); | |
throw $exception; | |
} | |
fclose($stream); | |
return $result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arf, this lib should run on PHP 5.3 (yes :p )
I can not do that... I think we will keep this code like this
I think it would be preferable to update the library to require at least PHP 5.6.
@lyrixx @mundschenk-at is someone of you interested in providing the fix for the situation where |
With the following reproducer:
Without my patch: 8.51
With my patch: 0.67
Note: there are another leak, but I can not find it for now...
But this patch is really mandatory!