Fix a segfault when using a mysqli object after unsuccesssful connect, the
authorAndrey Hristov <andrey@php.net>
Wed, 24 Mar 2010 15:17:57 +0000 (15:17 +0000)
committerAndrey Hristov <andrey@php.net>
Wed, 24 Mar 2010 15:17:57 +0000 (15:17 +0000)
handle should have been allocated with mysqli_init().

ext/mysqli/mysqli_nonapi.c
ext/mysqlnd/mysqlnd_wireprotocol.h

index b3b0dded08538075195e9e3d476d4986405d59b7..34fec47d409c2bae62c0eb21351b930f57ccfe86 100644 (file)
@@ -69,6 +69,7 @@ void mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_real_conne
        zend_bool                       new_connection = FALSE;
        zend_rsrc_list_entry    *le;
        mysqli_plist_entry *plist = NULL;
+       zend_bool                       self_alloced = 0;
 
 
 #if !defined(MYSQL_USE_MYSQLND)
@@ -99,6 +100,7 @@ void mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_real_conne
                }
                if (!mysql) {
                        mysql = (MY_MYSQL *) ecalloc(1, sizeof(MY_MYSQL));
+                       self_alloced = 1;
                }
                flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
        } else {
@@ -243,6 +245,7 @@ void mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS, zend_bool is_real_conne
                if (!is_real_connect) {
                        /* free mysql structure */
                        mysqli_close(mysql->mysql, MYSQLI_CLOSE_DISCONNECTED);
+                       mysql->mysql = NULL;
                }
                goto err;
        }
@@ -292,7 +295,7 @@ err:
                mysql->hash_key = NULL;
                mysql->persistent = FALSE;
        }
-       if (!is_real_connect) {
+       if (!is_real_connect && self_alloced) {
                efree(mysql);
        }
        RETVAL_FALSE;
index 2b2a7cae03436448529360c7e2a3f080467b02a3..8e36b1fac9a11d6cdbac9621f3b975a15e7c0eb3 100644 (file)
@@ -268,7 +268,7 @@ void php_mysqlnd_rowp_read_text_protocol(MYSQLND_MEMORY_POOL_CHUNK * row_buffer,
 
 
 PHPAPI MYSQLND_PROTOCOL * mysqlnd_protocol_init(zend_bool persistent TSRMLS_DC);
-PHPAPI void mysqlnd_protocol_free(MYSQLND_PROTOCOL * const protocol TSRMLS_DC)
+PHPAPI void mysqlnd_protocol_free(MYSQLND_PROTOCOL * const protocol TSRMLS_DC);
 
 
 #endif /* MYSQLND_WIREPROTOCOL_H */