Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85373 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 53887 invoked from network); 21 Mar 2015 16:07:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Mar 2015 16:07:44 -0000 Authentication-Results: pb1.pair.com header.from=jbafford@zort.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jbafford@zort.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zort.net designates 96.241.205.2 as permitted sender) X-PHP-List-Original-Sender: jbafford@zort.net X-Host-Fingerprint: 96.241.205.2 nova.zort.net Received: from [96.241.205.2] ([96.241.205.2:40342] helo=nova.zort.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A0/F5-18917-F479D055 for ; Sat, 21 Mar 2015 11:07:44 -0500 Received: from [10.0.1.2] (pulsar.zort.net [96.241.205.6]) (authenticated bits=0) by nova.zort.net (8.14.5/8.14.5) with ESMTP id t2LG7Yux003420 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Sat, 21 Mar 2015 12:07:34 -0400 Content-Type: text/plain; charset=windows-1252 Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\)) In-Reply-To: Date: Sat, 21 Mar 2015 12:07:33 -0400 Cc: Benjamin Eberlei , PHP Internals Content-Transfer-Encoding: quoted-printable Message-ID: <3136D99B-EA3D-4AF1-9B20-C13DF55E3A53@zort.net> References: To: "Georges.L" X-Mailer: Apple Mail (2.1878.6) Subject: Re: [PHP-DEV] RFC: Nested enclosing returns From: jbafford@zort.net (John Bafford) On Mar 21, 2015, at 10:17, Georges.L wrote: > The main purpose of this RFC is *not* to improve the exception system = of > PHP but to improve the code logic/hierarchy. >=20 >>> Hi php internals, >>>=20 >>> After some long and deep research i finally decided to write my = first RFC >>> about a feature i'd be interested to be improved in the php core: = *Nested >>> enclosing returns* Georges, This would make simply looking at code and reasoning about what it does = impossible. At present, if I have the following code: function foo() { if(doSomething()) { success(); } else { failure(); } =09 return 42; } try { bar(foo()); } catch($ex) { } Then I can make the following true statements about this code: * foo always calls doSomething() * foo always calls either success() or failure(), based on the = result of doSomething() * foo always returns 42 * bar is always called (with foo=92s return value, 42) * Alternatively to the above, any of the called functions may = throw an exception, which will be caught by the catch block If any of doSomething(), success(), failure(), or bar() can arbitrarily = return to some higher calling scope, then the only thing I can say for = sure is that doSomething() is called, after which my application could = be in some dangerously inconsistent state because I have no idea what = will be executed next. This then provides significant security concerns. For example, if we = have this: function API_Function_With_Callback($callback) { try { $callback(); =09 //do more stuff =09 return true; } catch($ex) { //do error stuff =09 return false; } } function doEvil() { $sentinel =3D //some unique value =09 $result =3D API_Function_With_Callback(function() use($sentinel) = { $backtrace =3D debug_backtrace(); $nestingLevel =3D //determine nesting level from = backtrace if($nestingLevel =3D=3D 2) return $sentinel, 2; else if($nestingLevel =3D=3D 3) return $sentinel, 3; else if($nestingLevel =3D=3D 4) return $sentinel, 4; // etc } =09 // Exploit inconsistent state of Call_API_Function here if($result =3D=3D=3D $sentinel) { =85 } } Then we can short-circuit code from some other library which isn=92t = prepared to deal with this kind of hijacking. More seriously, this sort = of hijacking *can=92t* be defended against (at least not without a = weakening of your original proposal). Any function that takes a callback = is potentially vulnerable to this sort of attack. Can you suggest an actual, practical, example, where this would be such = a benefit as to override the inherent difficulty about reasoning about = this code, and the potential security concerns? Are there any other = languages that make something like this possible? I suspect that any code that could be =93improved=94 with this = functionality is already in significant need of improvement by more = conventional means. -John