Skip to content

Commit 40059be

Browse files
committed
Merge branch 'PHP-7.0' into PHP-7.1
* PHP-7.0: Add NEWS Fix ASSERT logic Bugfix 72791: fix memory leak in PDO persistent connections
2 parents 2fbfa7f + bb955ec commit 40059be

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ PHP NEWS
2424
- PDO:
2525
. Fixed bug #72788 (Invalid memory access when using persistent PDO
2626
connection). (Keyur)
27+
. Fixed bug #72791 (Memory leak in PDO persistent connection handling). (Keyur)
2728

2829
- Reflection:
2930
. Implemented request #38992 (invoke() and invokeArgs() static method calls

ext/pdo/pdo_dbh.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ static PHP_METHOD(PDO, dbh_constructor)
297297
/* is the connection still alive ? */
298298
if (pdbh->methods->check_liveness && FAILURE == (pdbh->methods->check_liveness)(pdbh)) {
299299
/* nope... need to kill it */
300-
/*??? memory leak */
300+
pdbh->refcount--;
301301
zend_list_close(le);
302302
pdbh = NULL;
303303
}
@@ -310,6 +310,7 @@ static PHP_METHOD(PDO, dbh_constructor)
310310
/* need a brand new pdbh */
311311
pdbh = pecalloc(1, sizeof(*pdbh), 1);
312312

313+
pdbh->refcount = 1;
313314
pdbh->is_persistent = 1;
314315
pdbh->persistent_id = pemalloc(plen + 1, 1);
315316
memcpy((char *)pdbh->persistent_id, hashkey, plen+1);
@@ -322,6 +323,7 @@ static PHP_METHOD(PDO, dbh_constructor)
322323
efree(dbh);
323324
/* switch over to the persistent one */
324325
Z_PDO_OBJECT_P(object)->inner = pdbh;
326+
pdbh->refcount++;
325327
dbh = pdbh;
326328
}
327329

@@ -1504,8 +1506,13 @@ static void dbh_free(pdo_dbh_t *dbh, zend_bool free_persistent)
15041506
dbh->query_stmt = NULL;
15051507
}
15061508

1507-
if (dbh->is_persistent && !free_persistent) {
1508-
return;
1509+
if (dbh->is_persistent) {
1510+
#if ZEND_DEBUG
1511+
ZEND_ASSERT(!free_persistent || (dbh->refcount == 1));
1512+
#endif
1513+
if (!free_persistent && (--dbh->refcount)) {
1514+
return;
1515+
}
15091516
}
15101517

15111518
if (dbh->methods) {

0 commit comments

Comments
 (0)