php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11678 ob_get_contents in ob_start() callback causes infinite loop
Submitted: 2001-06-25 20:54 UTC Modified: 2001-06-26 14:32 UTC
From: rick at eastcore dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.4pl1 OS: Linux w/ 2.4.4 kernel
Private report: No CVE-ID: None
 [2001-06-25 20:54 UTC] rick at eastcore dot net
SCRIPT:

ob_start("my_callback");
echo "foo<br>\n";
ob_end_flush();

function my_callback($buffer)
{
	ob_start();
        echo "bar<br>\n";
        $bar = ob_get_contents();
        ob_end_clean();
	$buffer .= $baz;
}


BEHAVIOR:

PHP (static command-line, or as an Apache module) goes
into some sort of loop (I can guess which sort, see below)
and begins sucking CPU like a cyber-hooker.


CONFIGURE:

'./configure' '--prefix=/d/test_make/php'
                  '--with-config-file-path=/d/test_make/php'
                  '--with-apache=/home/rick/corengine-2.0.1pre1/src/apache_1.3.12'
                  '--with-mysql=/usr'



Looks to me like some sort of scoping/context issue (can't
start a new buffer inside the callback) -- in the callback
ob_start is getting called but when ob_end_clean is
called I'd guess that the state (the callback == "my_callback") has not been properly restricted to the
enclosing buffering (does that sentence parse?) and the
callback from the outer buffering is applied to the inner.
When that happens... infinite loop (and depending upon how
it's implemented it may not even be grabbing stack space
which would make it truly a CPU-sucker).

Why did this come up?

I had some object functionality with a "render" method
which simply printed out its output.  I moved a system to 
using buffered output and found that it would be more 
efficient to call the object->render() method after the
buffering was done (with a search & replace) since I then
had more information and could do less computation.  So, 
rather than rewrite the render method to return its output
I decided to nest a buffer and just capture the output (the
render method delegates to other objects which makes buffering even more attractive in this case).  Since the
best time to capture the buffer is when I'm cleaning up the
other one... boom I hit this bug.

I currently do not have PHP > 4.0.4pl1 installed so this
bug may be present in later releases I just haven't been
able to check yet.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-06-25 20:57 UTC] rick at eastcore dot net
The occurrence of $baz should be $bar in function my_callback (this doesn't change the bug, just points out my mad typ1ng skillz, etc.).
 [2001-06-26 14:32 UTC] zeev@php.net
Output buffering indeed cannot be used inside output handler functions.  However, the code that handled that error situation contained a bug that caused PHP to enter an infinite loop.  This bug is now fixed (although the limitation of not being able to use output buffering inside output handlers still applies)

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 21:01:30 2024 UTC