php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40160 create_function() result name starts with a '\0'. Request to chang to e.g. '1'
Submitted: 2007-01-18 13:34 UTC Modified: 2007-01-19 18:50 UTC
From: a dot steenveld at id dot umcn dot nl Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.4.4 OS: MS windows XP pro
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: a dot steenveld at id dot umcn dot nl
New email:
PHP Version: OS:

 

 [2007-01-18 13:34 UTC] a dot steenveld at id dot umcn dot nl
Description:
------------
create_function() results gets lost sometimes.
And yes, I've checked preveous bugreports, and yes, I know that the use of create_function() is advised againt in the way I'm using it here. But I wan't to use continuations in PHP.

The problem is akin to bug 10721 (which is not a bug).

The problem:
create_function() returns a string in the format '\0lambda_%d'
Subsequent calls with a result from create_function() as a argument will result in an empy string.
	   
The workaround is to remove (or replace) this 0.

Request to change the result from create_function().
Starting the name with a digit (or any character not in [_A-Za-z]) will result in an illegal name, safe enough to prevent name clashes.
This suggest a format like '%d_lambda' as return result from create_function().

Reproduce code:
---------------
<?php
/* cont.php   -   problem with continuations when using create_function()
   vim:nu

   Code inspired by http://www.ps.uni-sb.de/~duchier/python/continuations.html
*/

function writeln($s) { echo "$s\n"; }

function lambda  ($args, $code) { return        create_function ($args, $code);     }
function lambda0 ($args, $code) { return substr(create_function ($args, $code), 1); }
function L ($l) { if (strncmp($l, 'lambda_', 7) === 0) return "\0$l"; else return $l; }
	
function mul ($x, $y, $c) { echo "mul($x, $y, $c)\n"; $f = L($c); $f($x*$y); }
function add ($x, $y, $c) { echo "add($x, $y, $c)\n"; $f = L($c); $f($x+$y); }
function mal ($x, $y, $c) { echo "mal($x, $y, $c)\n"; mul(2, $x, lambda0 ('$v', "add(\$v, $y, $c);")); }
function ma  ($x, $y, $c) { echo "ma($x, $y, $c)\n";  $f = L($c); $f(2*$x+$y); }

function f_OK   ($x, $y) { mal($x, $y, lambda0 ('$v', "writeln(\"f($x, $y) = \$v\");")); }	
function f_FAIL ($x, $y) { mal($x, $y, lambda  ('$v', "writeln(\"f($x, $y) = \$v\");")); }	

	f_OK  (2, 2);
	f_FAIL(2, 2); // the result of create_function() gets truncated to an empty string! 
?>

Expected result:
----------------
// f_FAIL)() not active
mal(2, 2, lambda_1) 
mul(2, 2, lambda_2) 
add(4, 2, lambda_1) 
f(2, 2) = 6

Actual result:
--------------
// f_FAIL)() active
mal(2, 2, lambda_1) 
mul(2, 2, lambda_2) 
add(4, 2, lambda_1) 
f(2, 2) = 6 
mal(2, 2, \0lambda_3)
Parse error:  parse error, unexpected '}' in ...\cont.php(11) : runtime-created function on line 1
mul(2, 2, )
Fatal error:  Call to undefined function:  () in ...\cont.php on line 14

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-19 18:50 UTC] johannes@php.net
We're not going to change this behavior. (Might break BC and other reasons)

If you want to use a "guessed" name you can still use something like $name = "\0lambda_1"; $name($param);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 03:01:27 2024 UTC