From c40812f4772b8524c4e46155591690e0b9fbb644 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 26 Jul 2022 16:12:21 +0200 Subject: [PATCH] Fix GH-9155: dba_open("non-existing", "c-", "flatfile") segfaults We must not assume that the lock file has been opened. --- ext/dba/dba.c | 9 ++++----- ext/dba/tests/gh9155.phpt | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 ext/dba/tests/gh9155.phpt diff --git a/ext/dba/dba.c b/ext/dba/dba.c index 7e8ef11b03d0f..1042226a46c50 100644 --- a/ext/dba/dba.c +++ b/ext/dba/dba.c @@ -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; diff --git a/ext/dba/tests/gh9155.phpt b/ext/dba/tests/gh9155.phpt new file mode 100644 index 0000000000000..d8057517ec0c2 --- /dev/null +++ b/ext/dba/tests/gh9155.phpt @@ -0,0 +1,21 @@ +--TEST-- +Bug GH-9155 (dba_open("non-existing", "c-", "flatfile") segfaults) +--SKIPIF-- + +--FILE-- + +--CLEAN-- + +--EXPECTF-- +resource(%d) of type (dba)