Skip to content

Commit 08892a5

Browse files
committed
Hold a reference to the statement in a LOB stream
Otherwise, the statement can be freed before the LOB can be used. The strategy used is like how pdo_oci8 does it (hold ref to handle) and not like pdo_pgsql does it (hold a hashmap of handles). Related to GH-18
1 parent ecef0d2 commit 08892a5

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Diff for: ibm_statement.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct lob_stream_data
4646
stmt_handle *stmt_res;
4747
pdo_stmt_t *stmt;
4848
int colno;
49+
zval dbh; /* for holding a ref to the statement */
4950
};
5051

5152
#ifdef PASE
@@ -201,6 +202,13 @@ int lob_stream_flush(php_stream *stream)
201202
int lob_stream_close(php_stream *stream, int close_handle)
202203
{
203204
struct lob_stream_data *data = stream->abstract;
205+
pdo_stmt_t *stmt = data->stmt;
206+
zend_object *obj = &stmt->std;
207+
208+
if (Z_ISREF(data->dbh)) {
209+
zval_ptr_dtor(&data->dbh); /* should decref */
210+
GC_DELREF(obj);
211+
}
204212
efree(data);
205213
return 0;
206214
}
@@ -227,14 +235,23 @@ php_stream* create_lob_stream( pdo_stmt_t *stmt , stmt_handle *stmt_res , int co
227235
data->stmt_res = stmt_res;
228236
data->stmt = stmt;
229237
data->colno = colno;
238+
230239
col_res = &data->stmt_res->columns[data->colno];
231240
retval = (php_stream *) php_stream_alloc(&lob_stream_ops, data, NULL, "r");
232241
/* Find out if the column contains NULL data */
233242
if (lob_stream_read(retval, NULL, 0) == SQL_NULL_DATA) {
243+
/* Throw away this stream immediately */
244+
ZVAL_NULL(&data->dbh);
234245
php_stream_close(retval);
235246
return NULL;
236-
} else
247+
} else {
248+
/* Hold a ref to the PDOStatement, or it gets deallocated */
249+
ZVAL_COPY_VALUE(&data->dbh, &stmt->database_object_handle);
250+
Z_ADDREF(data->dbh);
251+
zend_object *obj = &stmt->std;
252+
GC_ADDREF(obj);
237253
return retval;
254+
}
238255
}
239256

240257
/*

0 commit comments

Comments
 (0)