twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.17k stars 1.25k forks source link

Twig throws out of memory if non existing variable passed to added function #3571

Closed julesbl closed 3 years ago

julesbl commented 3 years ago

Hi twig v3.3.2 php 7.3 in codeigniter 4.1.3

Added the following function $site_url = new TwigFunction('site_url', function ($url, $protocol = null){ return site_url($url, $protocol); }); $twig->addFunction($site_url);

have this in a template {{ site_url(edit_path) }}

The edit_path variable did not exist, got

ErrorException #1

Allowed memory size of 536870912 bytes exhausted (tried to allocate 505969872 bytes)

SYSTEMPATH/ThirdParty/Escaper/Escaper.php at line 147

140 of special meaning. Internally this will use htmlspecialchars(). 141 142 @param string $string 143 @return string 144 */ 145 public function escapeHtml($string) 146 { 147 return htmlspecialchars($string, $this->htmlSpecialCharsFlags, $this->encoding); 148 } 149 150 /* 151 Escape a string for the HTML Attribute context. We use an extended set of characters 152 to escape that are not covered by htmlspecialchars() to cover cases where an attribute 153 might be unquoted or quoted illegally (e.g. backticks are valid quotes for IE). 154 *

Tried adding error triggering or throw exceptions no such luck the problem seems to happen before it ever hits the defined function.

Jules

sstok commented 3 years ago

Is it possible this error is actually triggered by the site_url function? If this function throws some kind of error it might result in a error template being rendered which in turn tries to use this function, causing an end-less loop.

julesbl commented 3 years ago

You are quite right, I had tried to add error handling before this but that was not helping, now just return with empty string if variable empty and works fine