Skip to content

Commit b425175

Browse files
committed
ext/dba: Fix dba_close() not closing a dba_popen() connection
dba_close() looked up the persistent entry with connection->info after dba_close_connection() had cleared it, so the entry was never removed and the database file and its lock stayed held. Count the connections on the entry's refcount again, as PHP 8.3 did, and close the handle when dba_close() releases the last one, unless another persistent handle to the same file shares its stream. Lock conflict checks now also see persistent handles still in use, since closing one of two connections with the same key removed the other's DBA_G(connections) entry.
1 parent 62a96e1 commit b425175

3 files changed

Lines changed: 115 additions & 4 deletions

File tree

‎NEWS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PHP NEWS
2222
- Curl:
2323
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
2424

25+
- DBA:
26+
. Fixed dba_close() not closing a database opened with dba_popen().
27+
(Ilia Alshanetsky)
28+
2529
- DOM:
2630
. Fixed bug GH-25268 (NULL php_libxml_ref_obj dereference in
2731
dom_objects_store_clone_obj when cloning Dom\Notation). (David Carlier)

‎ext/dba/dba.c‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,22 @@ static void dba_close_info(dba_info *info)
271271
info = NULL;
272272
}
273273

274+
static bool dba_info_shares_stream(const dba_info *info)
275+
{
276+
ZEND_HASH_FOREACH_PTR(&EG(persistent_list), zend_resource *le) {
277+
if (le->type != le_pdb || le->ptr == NULL || le->ptr == info) {
278+
continue;
279+
}
280+
const dba_info *other = le->ptr;
281+
if ((info->fp && (info->fp == other->fp || info->fp == other->lock.fp))
282+
|| (info->lock.fp && (info->lock.fp == other->fp || info->lock.fp == other->lock.fp))) {
283+
return true;
284+
}
285+
} ZEND_HASH_FOREACH_END();
286+
287+
return false;
288+
}
289+
274290
static int remove_pconnection_from_list(zval *zv, void *p)
275291
{
276292
zend_resource *le = Z_RES_P(zv);
@@ -297,6 +313,11 @@ static void dba_close_connection(dba_connection *connection)
297313

298314
if (!persistent) {
299315
dba_close_info(connection->info);
316+
} else if (connection->hash) {
317+
zend_resource *le = zend_hash_find_ptr(&EG(persistent_list), connection->hash);
318+
if (le) {
319+
GC_DELREF(le);
320+
}
300321
}
301322

302323
connection->info = NULL;
@@ -501,6 +522,15 @@ static dba_info *php_dba_find(const zend_string *path)
501522
}
502523
} ZEND_HASH_FOREACH_END();
503524

525+
ZEND_HASH_FOREACH_PTR(&EG(persistent_list), zend_resource *le) {
526+
if (le->type == le_pdb && GC_REFCOUNT(le) > 1) {
527+
dba_info *info = le->ptr;
528+
if (info && zend_string_equals(path, info->path)) {
529+
return info;
530+
}
531+
}
532+
} ZEND_HASH_FOREACH_END();
533+
504534
return NULL;
505535
}
506536
/* }}} */
@@ -596,6 +626,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
596626
efree(resource_key);
597627
RETURN_FALSE;
598628
}
629+
GC_ADDREF(le);
599630

600631
object_init_ex(return_value, dba_connection_ce);
601632
dba_connection *connection = Z_DBA_CONNECTION_P(return_value);
@@ -951,13 +982,15 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent)
951982
connection->info->hnd = hptr;
952983

953984
if (persistent) {
954-
if (zend_register_persistent_resource(resource_key, resource_key_len, connection->info, le_pdb) == NULL) {
985+
zend_resource *le = zend_register_persistent_resource(resource_key, resource_key_len, connection->info, le_pdb);
986+
if (le == NULL) {
955987
php_error_docref(NULL, E_WARNING, "Could not register persistent resource");
956988
efree(resource_key);
957989
dba_close_connection(connection);
958990
zval_ptr_dtor(return_value);
959991
RETURN_FALSE;
960992
}
993+
GC_ADDREF(le);
961994
}
962995

963996
zend_hash_add_new(&DBA_G(connections), connection->hash, return_value);
@@ -1000,12 +1033,16 @@ PHP_FUNCTION(dba_close)
10001033
connection = Z_DBA_CONNECTION_P(id);
10011034
CHECK_DBA_CONNECTION(connection->info);
10021035

1003-
bool persistent = connection->info->flags & DBA_PERSISTENT;
1036+
dba_info *info = connection->info;
1037+
zend_resource *le = NULL;
1038+
if ((info->flags & DBA_PERSISTENT) && connection->hash) {
1039+
le = zend_hash_find_ptr(&EG(persistent_list), connection->hash);
1040+
}
10041041

10051042
dba_close_connection(connection);
10061043

1007-
if (persistent) {
1008-
zend_hash_apply_with_argument(&EG(persistent_list), remove_pconnection_from_list, (void *) connection->info);
1044+
if (le && GC_REFCOUNT(le) == 1 && !dba_info_shares_stream(info)) {
1045+
zend_hash_apply_with_argument(&EG(persistent_list), remove_pconnection_from_list, (void *) info);
10091046
}
10101047
}
10111048
/* }}} */

‎ext/dba/tests/dba_popen_close.phpt‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
--TEST--
2+
dba_close() releases a persistent connection once no other connection uses it
3+
--EXTENSIONS--
4+
dba
5+
--SKIPIF--
6+
<?php
7+
$handler = "flatfile";
8+
require_once(__DIR__ .'/skipif.inc');
9+
if (PHP_OS_FAMILY === 'Solaris') die("skip Solaris flock() emulation cannot detect a lock held by the same process");
10+
?>
11+
--CONFLICTS--
12+
dba
13+
--FILE--
14+
<?php
15+
$handler = "flatfile";
16+
require_once(__DIR__ .'/test.inc');
17+
18+
function lock_free(string $file): bool {
19+
$db = @dba_open($file, 'wlt', 'flatfile');
20+
if ($db === false) {
21+
return false;
22+
}
23+
dba_close($db);
24+
return true;
25+
}
26+
27+
$db1 = dba_popen($db_filename, 'cl', 'flatfile');
28+
dba_insert('key', 'value', $db1);
29+
$db2 = dba_popen($db_filename, 'cl', 'flatfile');
30+
31+
dba_close($db2);
32+
echo "first still open: ", dba_fetch('key', $db1), "\n";
33+
echo "lock free after closing one of two: "; var_dump(lock_free($db_filename));
34+
35+
dba_close($db1);
36+
echo "lock free after closing both: "; var_dump(lock_free($db_filename));
37+
38+
$db3 = dba_popen($db_filename, 'cl', 'flatfile');
39+
unset($db3);
40+
$db4 = dba_popen($db_filename, 'cl', 'flatfile');
41+
echo "reused after implicit release: ", dba_fetch('key', $db4), "\n";
42+
dba_close($db4);
43+
echo "lock free after closing the reused connection: "; var_dump(lock_free($db_filename));
44+
45+
$db5 = dba_popen($db_filename, 'cl', 'flatfile');
46+
$db6 = dba_popen($db_filename, 'cl', 'flatfile');
47+
dba_close($db5);
48+
var_dump(@dba_open($db_filename, 'wlt', 'flatfile'));
49+
echo "conflict seen after closing the earlier connection: ", error_get_last()['message'], "\n";
50+
dba_close($db6);
51+
52+
$reader1 = dba_popen($db_filename, 'rl', 'flatfile');
53+
$reader2 = dba_popen($db_filename, 'rd', 'flatfile');
54+
dba_close($reader1);
55+
echo "connection sharing the stream still reads: ", dba_fetch('key', $reader2), "\n";
56+
dba_close($reader2);
57+
?>
58+
--CLEAN--
59+
<?php
60+
require(__DIR__ .'/clean.inc');
61+
?>
62+
--EXPECT--
63+
first still open: value
64+
lock free after closing one of two: bool(false)
65+
lock free after closing both: bool(true)
66+
reused after implicit release: value
67+
lock free after closing the reused connection: bool(true)
68+
bool(false)
69+
conflict seen after closing the earlier connection: dba_open(): Driver initialization failed for handler: flatfile: Unable to establish lock (database file already open)
70+
connection sharing the stream still reads: value

0 commit comments

Comments
 (0)