Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:9016 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 89221 invoked by uid 1010); 9 Apr 2004 02:28:12 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 89113 invoked by uid 1007); 9 Apr 2004 02:28:11 -0000 To: internals@lists.php.net, truth@proposaltech.com Message-ID: <40760A39.7040202@benramsey.com> Date: Thu, 08 Apr 2004 22:28:09 -0400 User-Agent: Mozilla Thunderbird 0.5 (Windows/20040207) X-Accept-Language: en-us, en MIME-Version: 1.0 CC: Jevon Wright References: <20040406192114.8125.qmail@pb1.pair.com> <40735688.1090609@benramsey.com> <20040408222823.7283.qmail@pb1.pair.com> <006001c41db9$748a4330$0a00a8c0@home.jevon.org> <4075D551.7050800@benramsey.com> <1081475639.2789.57.camel@notebook.local> In-Reply-To: <1081475639.2789.57.camel@notebook.local> X-Enigmail-Version: 0.83.5.0 X-Enigmail-Supports: pgp-inline, pgp-mime Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Posted-By: 24.53.104.230 Subject: Re: Access Violations / Corruptions / Refs to temps From: lists@benramsey.com (Ben Ramsey) After I posted my initial message, I thought about references in the code with "=&". So, I went through looking for them, but I didn't find any at a quick glance. Still, they could've been in there. I'll check it out again and see what I can find. -Ben Todd Ruth wrote: >>>I have a feeling that PHP has a memory problem somewhere :/. > > > I'm not sure if it caused any of the issues either of you spoke of, > but I can report that PHP does have some pointer problems. I've > been putting off this email until I had written up something nicer, > but it's probably better to just get the word out (assuming this isn't > already known)... > > PHP has problems related to using a temporary as the source for a > reference. An obvious no-no is to write "$x =& NULL;" or > "$x =& @$y[5];". These are parse errors. On the other hand, PHP won't > complain about: > > function &f() { > $y = array(); > return @$y[5]; > } > $x =& f(); > > It even acts OK. The above code could be a mistake or it could be > a misguided attempt at optimization (believing that keeping a ref > to a temporary is better than setting $x equal to the temporary). > Maybe there was a desire to modify $y if applicable, but otherwise > be OK with modifying a temporary. In any case, if you have a large > enough system and code like that lying around, there's a good chance > you'll get crashes. You may not get a crash but instead see unrelated > data mysteriously changed in functions that get called later. > There are at least 2 ways to adjust the code above to make PHP > happy. One is to remove the "&" in the assignment. Another is > to create a "real" temporary variable instead of letting PHP make its > own temporary. ie > > function &f() { > $y = array(); > $temp = @$y[5]; > return $temp; > } > $x =& f(); > > I've had crashes go away and mysterious data overwrites go away with > the only change to the system being replacing a piece of code like > the first block of this email with a piece of code like the second > block. This has happened for just about every kind of "=& temp" you > can think of. Here are a few examples (in addition to the case above) > of using temporaries as sources for references. I think we've had a > crash / mysterious data overwrite for just about all of these (if not > all). > > function f1() { > $y = 5; > return $y; > } > $x =& f1(); // Get rid of the & or add an & to f1 > > function &f2() { > return NULL; // Get rid of & below or use "$r = NULL; return $r;" > } > $x =& f2(); > > function g() { > return NULL; > } > function h(&$x) { > $x = 5; > } > h(g()); > > That last one could use some combination of the above recommendations > to be fixed. For the record, I agree that there are few (if any) cases > where the above code is the best code for a design problem, but I also > dislike that these code blocks can lead to crashes / corruptions. > > BTW, we have never had a problem with using a temporary reference as > the source for a non-reference copy. That is to say the following has > never caused us a problem: > > function &f() { > $y = 5; > return $y; > } > $x = f(); > > Best of luck, > Todd -- Regards, Ben Ramsey http://benramsey.com http://www.phpcommunity.org/wiki/People/BenRamsey