[php-src] master: Merge branch 'PHP-8.6'

From: Date: Tue, 10 Nov 2026 13:49:49 +0000
Subject: [php-src] master: Merge branch 'PHP-8.6'
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-143534@lists.php.net to get a copy of this message
Author: Ilia Alshanetsky (iliaal) Date: 2026-11-10T08:38:27-05:00 Commit: https://github.com/php/php-src/commit/e0e2095a0d4afa15896ea7dbb37723a386fdb4b8 Raw diff: https://github.com/php/php-src/commit/e0e2095a0d4afa15896ea7dbb37723a386fdb4b8.diff Merge branch 'PHP-8.6' * PHP-8.6: ext/dba: Fix dba_close() not closing a dba_popen() connection Changed paths: A ext/dba/tests/dba_popen_close.phpt M ext/dba/dba.c Diff: diff --git a/ext/dba/dba.c b/ext/dba/dba.c index af9c62d2cfd6..b7f6d0d49c0e 100644 --- a/ext/dba/dba.c +++ b/ext/dba/dba.c @@ -269,6 +269,22 @@ static void dba_close_info(dba_info *info) info = NULL; } +static bool dba_info_shares_stream(const dba_info *info) +{ + ZEND_HASH_FOREACH_PTR(&EG(persistent_list), zend_resource *le) { + if (le->type != le_pdb || le->ptr == NULL || le->ptr == info) { + continue; + } + const dba_info *other = le->ptr; + if ((info->fp && (info->fp == other->fp || info->fp == other->lock.fp)) + || (info->lock.fp && (info->lock.fp == other->fp || info->lock.fp == other->lock.fp))) { + return true; + } + } ZEND_HASH_FOREACH_END(); + + return false; +} + static int remove_pconnection_from_list(zval *zv, void *p) { zend_resource *le = Z_RES_P(zv); @@ -295,6 +311,11 @@ static void dba_close_connection(dba_connection *connection) if (!persistent) { dba_close_info(connection->info); + } else if (connection->hash) { + zend_resource *le = zend_hash_find_ptr(&EG(persistent_list), connection->hash); + if (le) { + GC_DELREF(le); + } } connection->info = NULL; @@ -494,6 +515,15 @@ static dba_info *php_dba_find(const zend_string *path) } } ZEND_HASH_FOREACH_END(); + ZEND_HASH_FOREACH_PTR(&EG(persistent_list), zend_resource *le) { + if (le->type == le_pdb && GC_REFCOUNT(le) > 1) { + dba_info *info = le->ptr; + if (info && zend_string_equals(path, info->path)) { + return info; + } + } + } ZEND_HASH_FOREACH_END(); + return NULL; } /* }}} */ @@ -588,6 +618,7 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent) zend_string_release_ex(resource_key, /* persistent */ false); RETURN_FALSE; } + GC_ADDREF(le); object_init_ex(return_value, dba_connection_ce); dba_connection *connection = Z_DBA_CONNECTION_P(return_value); @@ -939,13 +970,15 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, bool persistent) connection->info->hnd = hptr; if (persistent) { - if (zend_register_persistent_resource_ex(connection->hash, connection->info, le_pdb) == NULL) { + zend_resource *le = zend_register_persistent_resource_ex(connection->hash, connection->info, le_pdb); + if (le == NULL) { php_error_docref(NULL, E_WARNING, "Could not register persistent resource"); zend_string_release_ex(resource_key, /* persistent */ false); dba_close_connection(connection); zval_ptr_dtor(return_value); RETURN_FALSE; } + GC_ADDREF(le); } zend_hash_add_new(&DBA_G(connections), connection->hash, return_value); @@ -988,12 +1021,16 @@ PHP_FUNCTION(dba_close) connection = Z_DBA_CONNECTION_P(id); CHECK_DBA_CONNECTION(connection->info); - bool persistent = connection->info->flags & DBA_PERSISTENT; + dba_info *info = connection->info; + zend_resource *le = NULL; + if ((info->flags & DBA_PERSISTENT) && connection->hash) { + le = zend_hash_find_ptr(&EG(persistent_list), connection->hash); + } dba_close_connection(connection); - if (persistent) { - zend_hash_apply_with_argument(&EG(persistent_list), remove_pconnection_from_list, (void *) connection->info); + if (le && GC_REFCOUNT(le) == 1 && !dba_info_shares_stream(info)) { + zend_hash_apply_with_argument(&EG(persistent_list), remove_pconnection_from_list, (void *) info); } } /* }}} */ diff --git a/ext/dba/tests/dba_popen_close.phpt b/ext/dba/tests/dba_popen_close.phpt new file mode 100644 index 000000000000..9e9b9cb78a19 --- /dev/null +++ b/ext/dba/tests/dba_popen_close.phpt @@ -0,0 +1,70 @@ +--TEST-- +dba_close() releases a persistent connection once no other connection uses it +--EXTENSIONS-- +dba +--SKIPIF-- +<?php +$handler = "flatfile"; +require_once(__DIR__ .'/skipif.inc'); +if (PHP_OS_FAMILY === 'Solaris') die("skip Solaris flock() emulation cannot detect a lock held by the same process"); +?> +--CONFLICTS-- +dba +--FILE-- +<?php +$handler = "flatfile"; +require_once(__DIR__ .'/test.inc'); + +function lock_free(string $file): bool { + $db = @dba_open($file, 'wlt', 'flatfile'); + if ($db === false) { + return false; + } + dba_close($db); + return true; +} + +$db1 = dba_popen($db_filename, 'cl', 'flatfile'); +dba_insert('key', 'value', $db1); +$db2 = dba_popen($db_filename, 'cl', 'flatfile'); + +dba_close($db2); +echo "first still open: ", dba_fetch('key', $db1), "\n"; +echo "lock free after closing one of two: "; var_dump(lock_free($db_filename)); + +dba_close($db1); +echo "lock free after closing both: "; var_dump(lock_free($db_filename)); + +$db3 = dba_popen($db_filename, 'cl', 'flatfile'); +unset($db3); +$db4 = dba_popen($db_filename, 'cl', 'flatfile'); +echo "reused after implicit release: ", dba_fetch('key', $db4), "\n"; +dba_close($db4); +echo "lock free after closing the reused connection: "; var_dump(lock_free($db_filename)); + +$db5 = dba_popen($db_filename, 'cl', 'flatfile'); +$db6 = dba_popen($db_filename, 'cl', 'flatfile'); +dba_close($db5); +var_dump(@dba_open($db_filename, 'wlt', 'flatfile')); +echo "conflict seen after closing the earlier connection: ", error_get_last()['message'], "\n"; +dba_close($db6); + +$reader1 = dba_popen($db_filename, 'rl', 'flatfile'); +$reader2 = dba_popen($db_filename, 'rd', 'flatfile'); +dba_close($reader1); +echo "connection sharing the stream still reads: ", dba_fetch('key', $reader2), "\n"; +dba_close($reader2); +?> +--CLEAN-- +<?php +require(__DIR__ .'/clean.inc'); +?> +--EXPECT-- +first still open: value +lock free after closing one of two: bool(false) +lock free after closing both: bool(true) +reused after implicit release: value +lock free after closing the reused connection: bool(true) +bool(false) +conflict seen after closing the earlier connection: dba_open(): Driver initialization failed for handler: flatfile: Unable to establish lock (database file already open) +connection sharing the stream still reads: value

« previous php.cvs (#143534) next »