You are right for your example.
But your patch does not work in this case:
global variable 'foo' = 1;
user reassigns 'foo' in local template scope 'foo' = 2;
he calls a template function (not touching any variable)
back in template scope 'foo' is now modified to 'foo' = 1;
But I think I got it now:
foreach (Smarty::$global_tpl_vars as $key => $value){
if ($_smarty_tpl->tpl_vars[$key] === $value) $saved_tpl_vars[$key] = $value;
}
$_smarty_tpl->tpl_vars = $saved_tpl_vars;
It's updated on Github and the fix will be included in 3.1.22