php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35910 Crash possibly related to Class, PDO, or combination. Seems like memory error
Submitted: 2006-01-05 21:22 UTC Modified: 2006-09-01 01:00 UTC
From: Ryan dot Melena at gmail dot com Assigned: wez (profile)
Status: No Feedback Package: PDO related
PHP Version: 5.1CVS-2006-01-05 (snap) OS: Windows XP
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: Ryan dot Melena at gmail dot com
New email:
PHP Version: OS:

 

 [2006-01-05 21:22 UTC] Ryan dot Melena at gmail dot com
Description:
------------
Provided code causes PHP error or crash when modified as discussed in comment on line 169.

Reproduce code:
---------------
http://66.41.135.167/phpbug/test3.phps

Expected result:
----------------
IDs should list in nested order as seen when running provided code without change mentioned in comment on line 169.

Actual result:
--------------
Code ends prematurely with a fatal error and php crashes.

MAKE SURE TO MAKE CHANGES INDICATED IN COMMENT ON LINE 169 TO REPRODUCE ERROR.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-05 21:27 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.1-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.1-win32-latest.zip


 [2006-01-05 21:28 UTC] tony2001@php.net
If you're still able to reproduce it - please provide the backtrace (I can see the "Redhat" in the OS field, so I believe you should be able to use gdb for that).
 [2006-01-05 21:35 UTC] Ryan dot Melena at gmail dot com
Tried latest snapshot on Windows and experienced the same problem.  Unable to try newest snapshot on Redhat system as it is not under my control and is maintained via rpms.  Any way to retrieve backtrack on Windows system?
 [2006-01-05 21:41 UTC] tony2001@php.net
You don't have to install rpms, you can build the snapshot in your own home directory.
 [2006-01-05 21:57 UTC] tony2001@php.net
I can't reproduce it, btw.
What version of MySQL are you using?
 [2006-01-05 21:59 UTC] Ryan dot Melena at gmail dot com
Sorry, I'm not very experienced as a Linux administrator.  I think compiling PHP from scratch and installing/learning GDB is a bit over my head.  PHP takes down apache in Windows and with VS I'm able to view the assembly which points to a dword move operation.  "00648F4A  mov  dword ptr [edx],eax"  I'm happy to help any way I can but I just don't have the enviornment set up to get a backtrace (and I'm not adept enough with linux to set it up).
 [2006-01-05 22:00 UTC] tony2001@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a backtrace to see what is happening behind the scenes. To
find out how to generate a backtrace, please read
http://bugs.php.net/bugs-generating-backtrace.php for *NIX and
http://bugs.php.net/bugs-generating-backtrace-win32.php for Win32

Once you have generated a backtrace, please submit it to this bug
report and change the status back to "Open". Thank you for helping
us make PHP better.


 [2006-01-05 22:01 UTC] Ryan dot Melena at gmail dot com
MySQL version is: 

5.0.15 on XP.
5.0.16 on Linux.
 [2006-01-05 22:07 UTC] tony2001@php.net
Yeah, that's what I think is causing it.
Could you try to install MySQL 4.x and see if you can reproduce the problem?
Also read this:
http://bugs.php.net/bugs-generating-backtrace-win32.php
http://bugs.php.net/bugs-generating-backtrace.php
 [2006-01-05 22:18 UTC] Ryan dot Melena at gmail dot com
Hope this is right... From Visual Studio:

>	php5ts.dll!_efree(void * ptr=0x00ed982c)  Line 303 + 0x1a bytes	C
 	php5ts.dll!zend_hash_del_key_or_index(_hashtable * ht=0x00eb4fe0, char * arKey=0x00ed9840, unsigned int nKeyLength=15, unsigned long h=0, int flag=0)  Line 490 + 0x6 bytes	C
 	php5ts.dll!zend_hash_reverse_apply(_hashtable * ht=0x00eb4fe0, int (void *, void * * *)* apply_func=0x10096c10, void * * * tsrm_ls=0x003225b8)  Line 738 + 0xf bytes	C
 	php5ts.dll!shutdown_executor(void * * * tsrm_ls=0x003225b8)  Line 268	C
 	php5ts.dll!zend_deactivate(void * * * tsrm_ls=0x003225b8)  Line 848	C
 	php5ts.dll!php_request_shutdown(void * dummy=0x00000000)  Line 1287	C
 	php.exe!main(int argc=2, char * * argv=0x00323f90)  Line 1231	C
 	php.exe!_mainCRTStartup()  + 0xe3 bytes	
 	kernel32.dll!7c816d4f() 	
 	[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]	
 	kernel32.dll!7c8399f3()
 [2006-01-05 22:24 UTC] tony2001@php.net
Yes, this is right, but it doesn't add any understanding.
Assigned to the PDO maintainer.
 [2006-04-09 07:44 UTC] wez@php.net
Please paste the script into the bug report.
I can't reach the URL you posted.

 [2006-04-09 14:53 UTC] Ryan dot Melena at gmail dot com
<?php

/* SQL

CREATE TABLE IF NOT EXISTS `section` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `parentSectionID` int(10) unsigned NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

INSERT INTO `section` VALUES (1, 1);
INSERT INTO `section` VALUES (2, 1);
INSERT INTO `section` VALUES (3, 2);
INSERT INTO `section` VALUES (4, 1);
INSERT INTO `section` VALUES (5, 5);
INSERT INTO `section` VALUES (6, 6);
INSERT INTO `section` VALUES (7, 6);

*/

?>

<?php

global $db_host, $db_name, $db_username, $db_password;

$db_host = 'localhost';
$db_name = 'test';
$db_username = 'root';
$db_password = 'superw00t';

class section
{
    private $id = null;
    
    private $parentSectionID = null;
    
    private $childSections = array();
    
    private $status = array('consistentWithDb' => false, 'childSectionsLoaded' => false);
    
    function __construct($id)
    {
        $db = resourceHelper::getDbConn();
        
        // Validate $id and load section data
        if(!is_int($id))
        {
            throw new Exception('Invalid argument for section::loadFromDb(), $id must be an int.  Value sent was [' . $id . '] of type [' . gettype($id) . '].');
        }
        
        $this->id = $id;
        
        $query =    'SELECT parentSectionID FROM section WHERE id = :id';

        $stmt = $db->prepare($query);
        
        $stmt->bindValue(':id', $this->id , PDO::PARAM_INT);
        
        $stmt->execute();
        
        $stmt->bindColumn('parentSectionID',    $this->parentSectionID,    PDO::PARAM_INT);
        
        $stmt->fetch(PDO::FETCH_BOUND);

        $stmt = null;
        $db = null;
    }
    
     function __get($varName)
    {
        switch($varName)
        {
            case 'id':
                return (int)$this->id;
            case 'childSections':
                if(!$this->status['childSectionsLoaded'])
                {
                    $this->loadChildSections();
                }
                return $this->childSections;
            default:
                throw new Exception('Variable [' . $varName . '] is not a public member of section.');
                break;
        }
    }
        
    private function loadChildSections()
    {
        $sectionID = null;
        
        $db = resourceHelper::getDbConn();
        
        $query = 'SELECT id FROM section WHERE parentSectionID = :id AND parentSectionID != id';
        
        $stmt = $db->prepare($query);
        
        $stmt->bindValue(':id',    $this->id,    PDO::PARAM_INT);
        
        $stmt->execute();
        
        $stmt->bindColumn('id',    $sectionID,    PDO::PARAM_INT);
        
        while($stmt->fetch(PDO::FETCH_BOUND))
        {
            $this->childSections[] = new section($sectionID);
        }
        
        $this->status['childSectionsLoaded'] = true;
    }
}


class resourceHelper
{
    public static function getDbConn()
    {
        global $db_host, $db_name, $db_username, $db_password;
    
        // Create and Validate database connection
        try
        {
            $db = new PDO('mysql:host=' . $db_host . ';dbname=' . $db_name, $db_username, $db_password);
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(Exception $e)
        {
            throw new Exception('Unable to connect to database [' . $config['db']['name'] . '] using configuration file values.' . $e->getMessage());
        }
        
        return $db;
    }
}


$db = resourceHelper::getDbConn();
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sectionID = null;

$query = 'SELECT id FROM section WHERE id = parentSectionID';

$stmt = $db->prepare($query);

$stmt->execute();

$stmt->bindColumn('id',    $sectionID,    PDO::PARAM_INT);

while($stmt->fetch(PDO::FETCH_BOUND))
{
    $section = new section($sectionID);
    buildSectionRow($section);
}

$stmt = null;
$db = null;




function buildSectionRow(section $section, $indent = 0)
{

    echo $section->id . '<br />';

    $indent++;
    
    /*
    To reproduce error, comment out $test definition then 
    replace $test in foreach loop with $section->childSections
    */

    $test = $section->childSections;

    foreach($test as $childSection)
    {
        buildSectionRow($childSection, $indent);
    }
}
?>
 [2006-04-11 03:38 UTC] wez@php.net
I can reproduce this on linux.

It appears to be less of a problem if you avoid binding to variables that are members of an object.

I haven't been able to pinpoint exactly what the cause is, as it appears to change subtly as the script is modified.

I remember that once-upon-a-time, referencing $this inside a constructor would lead to problems, perhaps this is a manifestation of that issue.
 [2006-08-24 16:17 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip


 [2006-09-01 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 19:01:29 2024 UTC