php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54583 Segfault when trying to reexecute statement after exception with libmysql
Submitted: 2011-04-21 08:28 UTC Modified: 2014-01-01 12:44 UTC
Votes:6
Avg. Score:5.0 ± 0.0
Reproduced:4 of 5 (80.0%)
Same Version:4 (100.0%)
Same OS:4 (100.0%)
From: an0nym at narod dot ru Assigned: johannes
Status: Assigned Package: PDO MySQL
PHP Version: 5.3.6 OS: CentOS 5.5 x86_64
Private report: No CVE-ID:
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: an0nym at narod dot ru
New email:
PHP Version: OS:

 

 [2011-04-21 08:28 UTC] an0nym at narod dot ru
Description:
------------
PHP segfaults when prepared statement that threw an exception is reexecuted. pdo_mysql, libmysql. Haven't tested with mysqlnd yet. 

Test script:
---------------
<?php
$DB = new PDO("mysql:dbname=test;host=localhost", "root", "",
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_EMULATE_PREPARES => false));
$DB->exec("CREATE TEMPORARY TABLE t(f VARCHAR(1))");
$stmt = $DB->prepare("INSERT INTO t VALUES(:value)");
$value = "aa";
$stmt->bindParam(":value", $value);
try {
    $stmt->execute();
} catch (PDOException $e) {}
$stmt->execute();


Expected result:
----------------
Script successfully executed

Actual result:
--------------
Segmentation fault

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-21 08:29 UTC] an0nym at narod dot ru
There was a similar problem that was patched in PHP 5.3.6. http://bugs.php.net/53551
 [2011-04-21 11:28 UTC] johannes@php.net
-Status: Open +Status: Feedback
 [2011-04-21 11:28 UTC] johannes@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.

Works for me with libmysql and mysqlnd. Please provide a stacktrace and the version of libmysql you are using.
 [2011-04-21 11:54 UTC] an0nym at narod dot ru
It seems you don't have strict mode enabled. 
Try this.

<?php
$DB = new PDO("mysql:dbname=test;host=localhost", "root", "",
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_EMULATE_PREPARES => false,
          PDO::MYSQL_ATTR_INIT_COMMAND => "SET SQL_MODE = 'STRICT_ALL_TABLES'"));
$DB->exec("CREATE TEMPORARY TABLE t(f VARCHAR(1))");
$stmt = $DB->prepare("INSERT INTO t VALUES(:value)");
$value = "aa";
$stmt->bindParam(":value", $value);
try {
    $stmt->execute();
} catch (PDOException $e) {}
$stmt->execute();
 [2011-04-21 11:56 UTC] an0nym at narod dot ru
-Status: Feedback +Status: Open
 [2011-04-21 11:56 UTC] an0nym at narod dot ru
Try new test code, please. I will submit backtrace when I manage to generate it.
 [2011-04-22 17:23 UTC] an0nym at narod dot ru
Everything works fine with php5.3.5 + mysqlnd @ winsrv2008r2x64. So it seems that 
the error is with libmysql only.
 [2011-05-10 10:15 UTC] uw@php.net
-Status: Open +Status: Feedback
 [2011-05-10 10:15 UTC] uw@php.net
Which version of libmysql is used, what's the server version?
 [2011-05-10 12:36 UTC] an0nym at narod dot ru
-Status: Feedback +Status: Open
 [2011-05-10 12:36 UTC] an0nym at narod dot ru
Sorry, CentOS is with mysqlnd now. Testing on Fedora:

root@test # uname -a
Linux test 2.6.35.11-83.fc14.x86_64 #1 SMP Mon Feb 7 07:06:44 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux
06:32:59 ~
root@test # php -v
PHP 5.3.6 (cli) (built: Mar 17 2011 20:56:13)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
06:33:02 ~
root@test # find / | grep libmysql
/usr/lib64/mysql/libmysqlclient.so.16.0.0
/usr/lib64/mysql/libmysqlclient_r.so.16.0.0
/usr/lib64/mysql/libmysqlclient_r.so.16
/usr/lib64/mysql/libmysqlclient.so.16
06:33:06 ~
root@test # cat test.php
<?php
var_dump(function_exists("mysqli_fetch_all"));
$DB = new PDO("mysql:dbname=test;host=localhost", "root", "",
    array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::ATTR_EMULATE_PREPARES => false,
          PDO::MYSQL_ATTR_INIT_COMMAND => "SET SQL_MODE = 'STRICT_ALL_TABLES'"));
$DB->exec("CREATE TEMPORARY TABLE t(f VARCHAR(1))");
$stmt = $DB->prepare("INSERT INTO t VALUES(:value)");
$value = "aa";
$stmt->bindParam(":value", $value);
try {
    $stmt->execute();
} catch (PDOException $e) {}
$stmt->execute();
06:33:17 ~
root@test # php test.php
bool(false)
Segmentation fault
06:33:24 ~
root@test # mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.1.56 Source distribution

Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye
06:34:43 ~
root@test #
 [2011-05-10 13:47 UTC] uw@php.net
-Assigned To: +Assigned To: johannes
 [2011-05-10 13:47 UTC] uw@php.net
Affects libmysql only. No issue when using mysqlnd.

Johannes, please review and apply patch:

nixnutz@linux-fuxh:~/php/php-src/branches/PHP_5_3> svn diff ext/pdo_mysql/
Index: ext/pdo_mysql/mysql_statement.c
===================================================================
--- ext/pdo_mysql/mysql_statement.c     (Revision 310880)
+++ ext/pdo_mysql/mysql_statement.c     (Arbeitskopie)
@@ -136,6 +136,7 @@
 {
        pdo_mysql_stmt *S = stmt->driver_data;
        pdo_mysql_db_handle *H = S->H;
+       int paramno;

        PDO_DBG_ENTER("pdo_mysql_stmt_execute_prepared_libmysql");

@@ -143,6 +144,10 @@
        if (mysql_stmt_bind_param(S->stmt, S->params) || mysql_stmt_execute(S->stmt)) {
                if (S->params) {
                        memset(S->params, 0, S->num_params * sizeof(MYSQL_BIND));
+                       for (paramno = 0; paramno < S->num_params; paramno++) {
+                         S->params[paramno].is_null = &S->in_null[paramno];
+                         S->params[paramno].length = &S->in_length[paramno];
+                       }
                }
                pdo_mysql_error_stmt(stmt);
                if (mysql_stmt_errno(S->stmt) == 2057) {
 [2011-05-10 17:46 UTC] uw@php.net
There are more pointers in MYSQL_BIND. I might have missed some in the patch.
 [2014-01-01 12:44 UTC] felipe@php.net
-Package: PDO related +Package: PDO MySQL
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Feb 26 19:01:37 2017 UTC