Skip to content

Commit a442e29

Browse files
committed
Fix GH-9155: dba_open("non-existing", "c-", "flatfile") segfaults
We must not assume that the lock file has been opened. Closes GH-9156.
1 parent 35fd97c commit a442e29

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ PHP NEWS
44

55
- DBA:
66
. Fixed LMDB driver memory leak on DB creation failure (Girgias)
7+
. Fixed bug GH-9155 (dba_open("non-existing", "c-", "flatfile") segfaults).
8+
(cmb)
79

810
- OPcache:
911
. Fixed bug GH-9033 (Loading blacklist file can fail due to negative length).

ext/dba/dba.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -860,11 +860,10 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
860860
fcntl(info->fd, F_SETFL, flags & ~O_APPEND);
861861
#elif defined(PHP_WIN32)
862862
} else if (modenr == DBA_CREAT && need_creation && !restarted) {
863-
zend_bool close_both;
864-
865-
close_both = (info->fp != info->lock.fp);
866-
php_stream_free(info->lock.fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
867-
if (close_both) {
863+
if (info->lock.fp != NULL) {
864+
php_stream_free(info->lock.fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
865+
}
866+
if (info->fp != info->lock.fp) {
868867
php_stream_free(info->fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
869868
}
870869
info->fp = NULL;

ext/dba/tests/gh9155.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug GH-9155 (dba_open("non-existing", "c-", "flatfile") segfaults)
3+
--SKIPIF--
4+
<?php
5+
$handler = "flatfile";
6+
require_once(__DIR__ .'/skipif.inc');
7+
?>
8+
--FILE--
9+
<?php
10+
11+
require_once(__DIR__ .'/test.inc');
12+
13+
$db = dba_open($db_filename, 'c-', 'flatfile');
14+
var_dump($db);
15+
?>
16+
--CLEAN--
17+
<?php
18+
require_once(__DIR__ .'/clean.inc');
19+
?>
20+
--EXPECTF--
21+
resource(%d) of type (dba)

0 commit comments

Comments
 (0)