Skip to content

Commit 71c22ef

Browse files
committed
Fix GH-9309: Segfault when connection is used after imap_close()
We actually need to check whether `php_imap_object.imap_stream` is `NULL` to detect that the connection has already been closed. Closes GH-9313.
1 parent 7908aae commit 71c22ef

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ PHP NEWS
2222
. Fixed bug GH-9155 (dba_open("non-existing", "c-", "flatfile") segfaults)
2323
(cmb)
2424

25+
- IMAP:
26+
. Fixed bug GH-9309 (Segfault when connection is used after imap_close()).
27+
(cmb)
28+
2529
- MBString:
2630
. Fixed bug GH-9008 (mb_detect_encoding(): wrong results with null $encodings).
2731
(cmb)

ext/imap/php_imap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ static void imap_object_destroy(zend_object *zobj) {
198198

199199
#define GET_IMAP_STREAM(imap_conn_struct, zval_imap_obj) \
200200
imap_conn_struct = imap_object_from_zend_object(Z_OBJ_P(zval_imap_obj)); \
201-
if (!imap_conn_struct) { \
201+
if (imap_conn_struct->imap_stream == NULL) { \
202202
zend_throw_exception(zend_ce_value_error, "IMAP\\Connection is already closed", 0); \
203203
RETURN_THROWS(); \
204204
}

ext/imap/tests/gh9309.phpt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Bug GH-9309 (Segfault when connection is used after imap_close())
3+
--EXTENSIONS--
4+
imap
5+
--SKIPIF--
6+
<?php
7+
require_once(__DIR__.'/setup/skipif.inc');
8+
?>
9+
--FILE--
10+
<?php
11+
require_once(__DIR__.'/setup/imap_include.inc');
12+
$stream_id = setup_test_mailbox('gh9309', 0, $mailbox);
13+
imap_close($stream_id);
14+
try {
15+
imap_headers($stream_id);
16+
} catch (ValueError $ex) {
17+
echo $ex->getMessage(), PHP_EOL;
18+
}
19+
?>
20+
--CLEAN--
21+
<?php
22+
$mailbox_suffix = 'gh9309';
23+
require_once(__DIR__.'/setup/clean.inc');
24+
?>
25+
--EXPECT--
26+
Create a temporary mailbox and add 0 msgs
27+
New mailbox created
28+
IMAP\Connection is already closed

0 commit comments

Comments
 (0)