svn: /pecl/ingres/trunk/ CHANGELOG ingres.c ingres.h tests/bug_18004.phpt
| From: | Grant Croker | Date: | Wed, 11 Aug 2010 07:00:56 +0000 |
| Subject: | svn: /pecl/ingres/trunk/ CHANGELOG ingres.c ingres.h tests/bug_18004.phpt | ||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-14581@lists.php.net to get a copy of this message | ||
grantc Wed, 11 Aug 2010 07:00:56 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=302098
Log:
Fix bug 18004 - Unreferenced resources can cause ingres_query() to fail
Overview
--------
The following problem was discovered during testing of the Drupal Ingres port.
When executing a non-result returning statement using ingres_query(), without
assigning it to a value, causes the created resource to have a 0 reference
count. Since it's reference count is 0 the Zend engine will destroy/garbage
collect that result resource. This in turn closes the statement handle and
freeing any associated memory. In addition to the result resource the II_LINK
structure maintains a copy of the statement handle so that any previous
statements that caused an error can be cancelled/closed. When the Zend engine
calls php_close_ii_result() this statement handle in the associated link is not
cleaned up.
Upon execution of the next non-result returning SQL statement the following
message is seen:
ingres_query(): Error: 7 - Unable to close non-result resource statement
This change cleans up the tear down code for a result resource when the Zend
engine calls the result destructor function, php_close_ii_result(). Previously
the driver would close the OpenAPI statement handle and free the memory without
clearing up the result list in II_LINK. Adding the attribute, result_id, to
II_RESULT, allows php_close_ii_result() to make use of php_ii_result_remove()
to remove any stale information contained within the connection resource as
well as resetting the statement handle, II_LINK->stmtHandle.
Bug: http://pecl.php.net/bugs/18004 (unknown)
Changed paths:
U pecl/ingres/trunk/CHANGELOG
U pecl/ingres/trunk/ingres.c
U pecl/ingres/trunk/ingres.h
A pecl/ingres/trunk/tests/bug_18004.phpt
Modified: pecl/ingres/trunk/CHANGELOG
===================================================================
--- pecl/ingres/trunk/CHANGELOG 2010-08-11 04:44:22 UTC (rev 302097)
+++ pecl/ingres/trunk/CHANGELOG 2010-08-11 07:00:56 UTC (rev 302098)
@@ -1,4 +1,5 @@
SVN
+- Fix bug 18004 - An unreferenced result resources can cause ingres_query() to fail
- Fix bug 17621 - Improve the handling of C style comments within an SQL query
2.2.2
Modified: pecl/ingres/trunk/ingres.c
===================================================================
--- pecl/ingres/trunk/ingres.c 2010-08-11 04:44:22 UTC (rev 302097)
+++ pecl/ingres/trunk/ingres.c 2010-08-11 07:00:56 UTC (rev 302098)
@@ -769,13 +769,15 @@
}
if (ii_link->result_list_ptr->result_id)
{
+ /* Clean up the result resource */
zend_list_delete(ii_link->result_list_ptr->result_id);
- // set the result_id 0 to indicate it has been released
- ii_link->result_list_ptr->result_id = 0;
}
result_entry = ii_link->result_list_ptr;
- ii_link->result_list_ptr = (ii_result_entry
*)ii_link->result_list_ptr->next_result_ptr;
+ if (ii_link->result_list_ptr)
+ {
+ ii_link->result_list_ptr = (ii_result_entry
*)ii_link->result_list_ptr->next_result_ptr;
+ }
/* free memory associated with the top entry in the list */
free(result_entry);
@@ -802,7 +804,28 @@
static void php_close_ii_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
II_RESULT *ii_result = (II_RESULT *) rsrc->ptr;
+ char *resource = NULL;
+ char *type_name = NULL;
+ int type;
+ ii_result_entry *result_entry;
+ /* Check to see we have a valid link_id */
+ if ( ii_result->link_id != -1 )
+ {
+ resource = zend_list_find(ii_result->link_id, &type);
+ type_name = zend_rsrc_list_get_rsrc_type(ii_result->link_id TSRMLS_CC);
+ /* Is it an "ingres (persistent) connection" ? */
+ if ((strcmp("ingres connection",type_name) == 0 ) || (strcmp("ingres
persistent connection",type_name) == 0 ))
+ {
+ if (((II_LINK *)resource)->stmtHandle == ii_result->stmtHandle)
+ {
+ ((II_LINK *)resource)->stmtHandle = NULL;
+ }
+ }
+ }
+ /* Remove this result resource from the associated ii_link resource */
+ php_ii_result_remove (ii_result, ii_result->result_id TSRMLS_CC);
+
_close_statement(ii_result TSRMLS_CC);
_ii_free_ii_result(ii_result);
@@ -1257,6 +1280,8 @@
ii_result->buffered = FALSE;
ii_result->prepared = FALSE;
ii_result->executed = FALSE;
+ ii_result->result_id = 0;
+
}
/* }}} */
@@ -2413,6 +2438,9 @@
ZEND_REGISTER_RESOURCE(return_value, ii_result, le_ii_result)
+ /* store the result_id so we can use it in php_close_ii_result() */
+ ii_result->result_id = Z_LVAL_P(return_value); /* resource id */
+
/* Add details of the II_RESULT resource to the II_LINK resource for later clean up */
result_entry = (ii_result_entry *)malloc(sizeof(ii_result_entry));
@@ -2689,6 +2717,9 @@
ZEND_REGISTER_RESOURCE(return_value, ii_result, le_ii_result)
+ /* store the result_id so we can use it in php_close_ii_result() */
+ ii_result->result_id = Z_LVAL_P(return_value); /* resource id */
+
/* Add details of the II_RESULT resource to the II_LINK resource for later clean up */
result_entry = (ii_result_entry *)malloc(sizeof(ii_result_entry));
@@ -3466,7 +3497,7 @@
resource = zend_list_find(ii_result->link_id, &type);
type_name = zend_rsrc_list_get_rsrc_type(ii_result->link_id TSRMLS_CC);
/* Is it an "ingres (persistent) connection" ? */
- if ((strcmp("ingres connection\0",type_name) == 0 ) || (strcmp("ingres
persistent connection\0",type_name) == 0 ))
+ if ((strcmp("ingres connection",type_name) == 0 ) || (strcmp("ingres
persistent connection",type_name) == 0 ))
{
if (((II_LINK *)resource)->stmtHandle == ii_result->stmtHandle)
{
@@ -3474,7 +3505,10 @@
}
this_ptr = (char *)((II_LINK *)resource)->result_list_ptr;
result_entry = ((II_LINK *)resource)->result_list_ptr;
- next_ptr = result_entry->next_result_ptr;
+ if (result_entry)
+ {
+ next_ptr = result_entry->next_result_ptr;
+ }
/* scan the link's resource list */
while (result_entry && result_entry->result_id != result_id)
{
@@ -3485,10 +3519,6 @@
{
next_ptr = (char *)((ii_result_entry *)this_ptr)->next_result_ptr;
}
- else
- {
- php_error_docref(NULL TSRMLS_CC, E_ERROR, "php_ii_result_remove : An
attempt was made to access a NULL pointer");
- }
}
if (this_ptr == (char *)((II_LINK *)resource)->result_list_ptr) /* head of list */
Modified: pecl/ingres/trunk/ingres.h
===================================================================
--- pecl/ingres/trunk/ingres.h 2010-08-11 04:44:22 UTC (rev 302097)
+++ pecl/ingres/trunk/ingres.h 2010-08-11 07:00:56 UTC (rev 302098)
@@ -79,6 +79,8 @@
II_BOOL buffered; /* Is this a buffered query or not */
II_BOOL prepared; /* Is this a prepared query or not */
II_BOOL executed; /* Has this query been through ingres_execute() already? */
+ int result_id; /* Resource ID of this result resource used to match with
result
+ entries in the result entry list */
} II_RESULT;
/* The following was added to allow the extension to build on Windows using */
Added: pecl/ingres/trunk/tests/bug_18004.phpt
===================================================================
--- pecl/ingres/trunk/tests/bug_18004.phpt (rev 0)
+++ pecl/ingres/trunk/tests/bug_18004.phpt 2010-08-11 07:00:56 UTC (rev 302098)
@@ -0,0 +1,39 @@
+--TEST--
+Ingres: Bug #18004 - test non-associated calls to ingres_query()
+--SKIPIF--
+<?php require_once('skipif.inc'); ?>
+--ENV--
+II_SYSTEM=/opt/Ingres/II
+--FILE--
+<?php
+
+require_once('connection.inc');
+
+$conn = ingres_connect($database,$user,$password);
+
+if ($conn) {
+ ingres_query($conn, "set lockmode session where readlock=nolock");
+ if (ingres_errno())
+ {
+ echo ingres_errno() . " - " . ingres_error()."\n";
+ }
+ ingres_query($conn, "set session read write, isolation level read uncommitted");
+ if (ingres_errno())
+ {
+ echo ingres_errno() . " - " . ingres_error()."\n";
+ }
+ $rc=ingres_query($conn, "select dbmsinfo('cursor_limit') as cursor_limit");
+ if (ingres_errno())
+ {
+ echo ingres_errno() . " - " . ingres_error()."\n";
+ }
+ $result = ingres_fetch_object($rc);
+ echo "Cursor limit is - " . $result->cursor_limit;
+ ingres_close($conn);
+} else {
+ echo "Connection failed.";
+}
+
+?>
+--EXPECTF--
+Cursor limit is - %d