php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #5879 switch() malfunction under very specific conditions
Submitted: 2000-07-31 20:43 UTC Modified: 2000-08-11 18:11 UTC
From: dragonzap1 at aol dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.1pl2 OS: Linux 2.2.16
Private report: No CVE-ID: None
 [2000-07-31 20:43 UTC] dragonzap1 at aol dot com
Script:

<?php

$string = 'string';

echo 'There is ';
switch($string) {
default:
        echo 'NOT ';
        break;
}
echo 'a problem.';

someFunction(NULL);

$ec = new EmptyClass();
$somevar = $ec->someMethod(NULL);
$ec->someMethod();
$ec->someMethod();

someFunction();

function someFunction($var = '') {
}

class EmptyClass {
        function someMethod($data = '') {
        }
}

?>

Compile flags:
        --with-mysql=/usr/local/mysql
        --with-apache=/usr/local/src/apache
        --with-gd=/usr/local/src/gd
        --with-jpeg-dir=/usr/local/src/jpeg
        --with-xml
        --with-zlib
        --enable-versioning

Problem:
   For some reason, this program doesn't always print out "There is NOT a problem." This problem seems to be VERY specific, and doesn't always happen (but if the page is reloaded a bunch of times, it should eventually say 'There is a problem.' and on rare occasions may crash altogether) It will start working as expected, however, if any of the calls to someFunction() or someMethod() are removed, or the arguments are removed or changed to non-NULL values. Adding any other case to the case statement (whether you keep the 'default' or not) will make it function properly as well.

The someFunction/EmptyClass/someMethod definitions can be removed while keeping this odd behavior intact, as well as the "$ec = new EmptyClass()", and then adding an "exit;" statement below the "echo 'a problem.';" line but I wanted to try to keep it mostly 'legal' PHP.

I know that most people aren't going to do a switch() statement with just a default case (it could happen as a placeholder in the middle of developing something, though, which is how I ran into it), but it may be indicative of a larger problem.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-07-31 21:27 UTC] waldschrott@php.net
As I can figure out, that code snipplet can be expected to
print "There is NOT a problem.".
The switch statement does not contain any specific cases,
thus the default case is used *always*..
The code below "echo 'a problem.';" can be ignored...

 [2000-08-01 10:23 UTC] waldschrott@php.net
user feedback:

I know it can be expected to print out "There is NOT a problem." My point is that it doesn't always print out "There is NOT a problem." even though it should always print that out, and sometimes prints out "There is a problem."; bypassing the default case completely, even though it's the only case and it's the default case. (hence the text, and by extension, the problem.) And the code below "echo 'a problem';" can't be ignored, because the problem does not occur if that code is not there.

 [2000-08-01 10:25 UTC] waldschrott@php.net
I?ve tested thoroughly and it does not make sense for me, do you have the optimizer enabled?
 [2000-08-01 16:02 UTC] waldschrott@php.net
I?ve tested on Linux and Win32 now. No such anomalies.
Please verify that you didn?t ran into a strange sort of output buffering...
Replace you echo()?s with variable assignments and check this way if the default case is executed.

Anyway it?s not possible to verify it for me.
 [2000-08-01 17:28 UTC] dragonzap1 at aol dot com
An example of this bug in action can be found at:

http://www.smartpudding.com/phpbug/

switch-bug.php is the file, switch-bug.phps is a symbolic link to the same file.
 [2000-08-04 11:36 UTC] jalal@php.net
Definitely something strange going on with this one.
The following code works correctly on the first iteration, then incorrectly, and will throw a segfault after about 6-7 reloads. Both WinNT and Linux platforms.

<?php
switch("1"){
default:
        echo 'NOT ';
        break;
}

echo 'a problem.';
exit;

someFunction(NULL);
$ec = new EmptyClass();
$ec->someOtherMethod();
$somevar = $ec->someMethod(NULL);
?>

Comment out any of the last four lines, and the code functions as expected.
Add a case "0": statement and the code functions correctly.

Any ideas anyone?
(Yes, I realize the code is not realistic, but it does indicate some error in the parsing of it.)
 [2000-08-05 19:36 UTC] jalal@php.net
Cannot repeat bug in latest snapshot. 
Considered fixed.
 [2000-08-08 02:28 UTC] jalal@php.net
Reopened.
Further reports of malfunction received.

 [2000-08-11 04:46 UTC] andi@php.net
Should be fixed in latest CVS
 [2000-08-11 04:58 UTC] waldschrott@php.net
Andi, would you please add a few comments to this bug. When
could it expected to appear etc.
It?ll make lifer easer for QA having a few comments on such
strange bugs...
 [2000-08-11 18:11 UTC] stas@php.net
Well, this should be fixed in CVS (switch with only default). Please check.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Sep 28 19:01:26 2024 UTC