Skip to content

Commit 5d37a32

Browse files
committed
bug #2995 Fix partial output leak when a PHP fatal error occurs (fabpot)
This PR was merged into the 1.x branch. Discussion ---------- Fix partial output leak when a PHP fatal error occurs closes #1962 (thank you @fluff for the "trick") Commits ------- 6196fe5 fixed partial output leak when a PHP fatal error occurs
2 parents ababe1a + 6196fe5 commit 5d37a32

10 files changed

Lines changed: 12 additions & 11 deletions

File tree

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
* 1.41.0 (2019-XX-XX)
22

3+
* fixed partial output leak when a PHP fatal error occurs
34
* optimized context access on PHP 7.4
45

56
* 1.40.1 (2019-04-29)

src/Extension/DebugExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function twig_var_dump(Environment $env, $context, array $vars = [])
5454
return;
5555
}
5656

57-
ob_start();
57+
ob_start(function () { return ''; });
5858

5959
if (!$vars) {
6060
$vars = [];

src/Node/MacroNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function compile(Compiler $compiler)
104104
->outdent()
105105
->write("]);\n\n")
106106
->write("\$blocks = [];\n\n")
107-
->write("ob_start();\n")
107+
->write("ob_start(function () { return ''; });\n")
108108
->write("try {\n")
109109
->indent()
110110
->subcompile($this->getNode('body'))

src/Node/SetNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function compile(Compiler $compiler)
5858
} else {
5959
if ($this->getAttribute('capture')) {
6060
$compiler
61-
->write("ob_start();\n")
61+
->write("ob_start(function () { return ''; });\n")
6262
->subcompile($this->getNode('values'))
6363
;
6464
}

src/Node/SpacelessNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function compile(Compiler $compiler)
3131
{
3232
$compiler
3333
->addDebugInfo($this)
34-
->write("ob_start();\n")
34+
->write("ob_start(function () { return ''; });\n")
3535
->subcompile($this->getNode('body'))
3636
->write("echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));\n")
3737
;

src/Template.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function displayBlock($name, array $context, array $blocks = [], $useBloc
253253
*/
254254
public function renderParentBlock($name, array $context, array $blocks = [])
255255
{
256-
ob_start();
256+
ob_start(function () { return ''; });
257257
$this->displayParentBlock($name, $context, $blocks);
258258

259259
return ob_get_clean();
@@ -274,7 +274,7 @@ public function renderParentBlock($name, array $context, array $blocks = [])
274274
*/
275275
public function renderBlock($name, array $context, array $blocks = [], $useBlocks = true)
276276
{
277-
ob_start();
277+
ob_start(function () { return ''; });
278278
$this->displayBlock($name, $context, $blocks, $useBlocks);
279279

280280
return ob_get_clean();
@@ -417,7 +417,7 @@ public function display(array $context, array $blocks = [])
417417
public function render(array $context)
418418
{
419419
$level = ob_get_level();
420-
ob_start();
420+
ob_start(function () { return ''; });
421421
try {
422422
$this->display($context);
423423
} catch (\Exception $e) {

src/TemplateWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function renderBlock($name, $context = [])
9696
{
9797
$context = $this->env->mergeGlobals($context);
9898
$level = ob_get_level();
99-
ob_start();
99+
ob_start(function () { return ''; });
100100
try {
101101
$this->template->displayBlock($name, $context);
102102
} catch (\Exception $e) {

test/Twig/Tests/Node/MacroTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getfoo(\$__foo__ = null, \$__bar__ = "Foo"$declaration)
5959
6060
\$blocks = [];
6161
62-
ob_start();
62+
ob_start(function () { return ''; });
6363
try {
6464
echo "foo";
6565
} catch (\Exception \$e) {

test/Twig/Tests/Node/SetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function getTests()
4949
$node = new SetNode(true, $names, $values, 1);
5050
$tests[] = [$node, <<<EOF
5151
// line 1
52-
ob_start();
52+
ob_start(function () { return ''; });
5353
echo "foo";
5454
\$context["foo"] = ('' === \$tmp = ob_get_clean()) ? '' : new Markup(\$tmp, \$this->env->getCharset());
5555
EOF

test/Twig/Tests/Node/SpacelessTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function getTests()
3232
return [
3333
[$node, <<<EOF
3434
// line 1
35-
ob_start();
35+
ob_start(function () { return ''; });
3636
echo "<div> <div> foo </div> </div>";
3737
echo trim(preg_replace('/>\s+</', '><', ob_get_clean()));
3838
EOF

0 commit comments

Comments
 (0)