Skip to content

Commit

Permalink
Fix GH-9155: dba_open("non-existing", "c-", "flatfile") segfaults
Browse files Browse the repository at this point in the history
We must not assume that the lock file has been opened.

Closes GH-9156.
  • Loading branch information
cmb69 committed Jul 27, 2022
1 parent 35fd97c commit a442e29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -4,6 +4,8 @@ PHP NEWS

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

- OPcache:
. Fixed bug GH-9033 (Loading blacklist file can fail due to negative length).
Expand Down
9 changes: 4 additions & 5 deletions ext/dba/dba.c
Expand Up @@ -860,11 +860,10 @@ static void php_dba_open(INTERNAL_FUNCTION_PARAMETERS, int persistent)
fcntl(info->fd, F_SETFL, flags & ~O_APPEND);
#elif defined(PHP_WIN32)
} else if (modenr == DBA_CREAT && need_creation && !restarted) {
zend_bool close_both;

close_both = (info->fp != info->lock.fp);
php_stream_free(info->lock.fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
if (close_both) {
if (info->lock.fp != NULL) {
php_stream_free(info->lock.fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
}
if (info->fp != info->lock.fp) {
php_stream_free(info->fp, persistent ? PHP_STREAM_FREE_CLOSE_PERSISTENT : PHP_STREAM_FREE_CLOSE);
}
info->fp = NULL;
Expand Down
21 changes: 21 additions & 0 deletions ext/dba/tests/gh9155.phpt
@@ -0,0 +1,21 @@
--TEST--
Bug GH-9155 (dba_open("non-existing", "c-", "flatfile") segfaults)
--SKIPIF--
<?php
$handler = "flatfile";
require_once(__DIR__ .'/skipif.inc');
?>
--FILE--
<?php

require_once(__DIR__ .'/test.inc');

$db = dba_open($db_filename, 'c-', 'flatfile');
var_dump($db);
?>
--CLEAN--
<?php
require_once(__DIR__ .'/clean.inc');
?>
--EXPECTF--
resource(%d) of type (dba)

0 comments on commit a442e29

Please sign in to comment.