Skip to content
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

Merged
merged 1 commit into from
Jun 30, 2020
Merged
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
6 changes: 5 additions & 1 deletion src/HTML5.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ public function saveHTML($dom, $options = array())
$stream = fopen('php://temp', 'wb');

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);
        }

Copy link
Contributor Author

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

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.

Copy link
Contributor Author

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$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;

Copy link
Contributor

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.

$this->save($dom, $stream, array_merge($this->defaultOptions, $options));

return stream_get_contents($stream, -1, 0);
$html = stream_get_contents($stream, -1, 0);

fclose($stream);

return $html;
}
}