Skip to content

Commit 7f3bdda

Browse files
committed
Properly fix #80220
The original fix for that bug[1] broke the formerly working composition of message/rfc822 messages, which results in a segfault when freeing the message body now. While `imap_mail_compose()` does not really support composition of meaningful message/rfc822 messages (although libc-client appears to support that), some code may still use this to compose partial messages, and using string manipulation to create the final message. The point is that libc-client expects `TYPEMESSAGE` with an explicit subtype of `RFC822` to have a `nested.msg` (otherwise there will be a segfault during free), but not to have any `contents.text.data` (this will leak otherwise). [1] <http://git.php.net/?p=php-src.git;a=commit;h=0d022ddf03c5fabaaa22e486d1e4a367ed9170a7> Closes GH-6343.
1 parent 7b5f232 commit 7f3bdda

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PHP NEWS
55
- IMAP:
66
. Fixed bug #64076 (imap_sort() does not return FALSE on failure). (cmb)
77
. Fixed bug #80239 (imap_rfc822_write_address() leaks memory). (cmb)
8+
. Fixed minor regression caused by fixing bug #80220. (cmb)
89

910
29 Oct 2020, PHP 7.3.24
1011

ext/imap/php_imap.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3706,15 +3706,19 @@ PHP_FUNCTION(imap_mail_compose)
37063706
bod->disposition.parameter = disp_param;
37073707
}
37083708
}
3709-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(data), "contents.data", sizeof("contents.data") - 1)) != NULL) {
3710-
convert_to_string_ex(pvalue);
3711-
bod->contents.text.data = fs_get(Z_STRLEN_P(pvalue) + 1);
3712-
memcpy(bod->contents.text.data, Z_STRVAL_P(pvalue), Z_STRLEN_P(pvalue)+1);
3713-
bod->contents.text.size = Z_STRLEN_P(pvalue);
3709+
if (bod->type == TYPEMESSAGE && bod->subtype && !strcmp(bod->subtype, "RFC822")) {
3710+
bod->nested.msg = mail_newmsg();
37143711
} else {
3715-
bod->contents.text.data = fs_get(1);
3716-
memcpy(bod->contents.text.data, "", 1);
3717-
bod->contents.text.size = 0;
3712+
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(data), "contents.data", sizeof("contents.data") - 1)) != NULL) {
3713+
convert_to_string_ex(pvalue);
3714+
bod->contents.text.data = fs_get(Z_STRLEN_P(pvalue) + 1);
3715+
memcpy(bod->contents.text.data, Z_STRVAL_P(pvalue), Z_STRLEN_P(pvalue)+1);
3716+
bod->contents.text.size = Z_STRLEN_P(pvalue);
3717+
} else {
3718+
bod->contents.text.data = fs_get(1);
3719+
memcpy(bod->contents.text.data, "", 1);
3720+
bod->contents.text.size = 0;
3721+
}
37183722
}
37193723
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(data), "lines", sizeof("lines") - 1)) != NULL) {
37203724
bod->size.lines = zval_get_long(pvalue);
@@ -3933,7 +3937,7 @@ PHP_FUNCTION(imap_mail_compose)
39333937
efree(mystring);
39343938
mystring=tempstring;
39353939
} else if (bod) {
3936-
spprintf(&tempstring, 0, "%s%s%s", mystring, bod->contents.text.data, CRLF);
3940+
spprintf(&tempstring, 0, "%s%s%s", mystring, bod->contents.text.data ? bod->contents.text.data : "", CRLF);
39373941
efree(mystring);
39383942
mystring=tempstring;
39393943
} else {

ext/imap/tests/bug80220.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Bug #80220 (imap_mail_compose() may leak memory) - message/rfc822 regression
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('imap')) die('skip imap extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$bodies = [[
10+
'type' => TYPEMESSAGE,
11+
'subtype' => 'RFC822',
12+
], [
13+
'contents.data' => 'asd',
14+
]];
15+
var_dump(imap_mail_compose([], $bodies));
16+
17+
$bodies = [[
18+
'type' => TYPEMESSAGE,
19+
], [
20+
'contents.data' => 'asd',
21+
]];
22+
var_dump(imap_mail_compose([], $bodies));
23+
?>
24+
--EXPECT--
25+
string(53) "MIME-Version: 1.0
26+
Content-Type: MESSAGE/RFC822
27+
28+
29+
"
30+
string(53) "MIME-Version: 1.0
31+
Content-Type: MESSAGE/RFC822
32+
33+
34+
"

0 commit comments

Comments
 (0)