Skip to content

Commit 77582ec

Browse files
committed
Merge branch 'PHP-8.0'
* PHP-8.0: Throw Value/TypeError for invalid $bodies in imap_mail_compose()
2 parents ac1ccce + 158d308 commit 77582ec

File tree

4 files changed

+49
-34
lines changed

4 files changed

+49
-34
lines changed

ext/imap/php_imap.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,7 +3093,7 @@ PHP_FUNCTION(imap_fetch_overview)
30933093
/* {{{ Create a MIME message based on given envelope and body sections */
30943094
PHP_FUNCTION(imap_mail_compose)
30953095
{
3096-
zval *envelope, *body;
3096+
HashTable *envelope, *body;
30973097
zend_string *key;
30983098
zval *data, *pvalue, *disp_data, *env_data;
30993099
char *cookie = NIL;
@@ -3105,62 +3105,66 @@ PHP_FUNCTION(imap_mail_compose)
31053105
int toppart = 0;
31063106
int first;
31073107

3108-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a/a/", &envelope, &body) == FAILURE) {
3108+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "h/h/", &envelope, &body) == FAILURE) {
31093109
RETURN_THROWS();
31103110
}
31113111

3112+
if (zend_hash_num_elements(body) == 0) {
3113+
zend_argument_value_error(2, "cannot be empty");
3114+
}
3115+
31123116
#define PHP_RFC822_PARSE_ADRLIST(target, value) \
31133117
str_copy = estrndup(Z_STRVAL_P(value), Z_STRLEN_P(value)); \
31143118
rfc822_parse_adrlist(target, str_copy, "NO HOST"); \
31153119
efree(str_copy);
31163120

31173121
env = mail_newenvelope();
3118-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "remail", sizeof("remail") - 1)) != NULL) {
3122+
if ((pvalue = zend_hash_str_find(envelope, "remail", sizeof("remail") - 1)) != NULL) {
31193123
convert_to_string_ex(pvalue);
31203124
env->remail = cpystr(Z_STRVAL_P(pvalue));
31213125
}
3122-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "return_path", sizeof("return_path") - 1)) != NULL) {
3126+
if ((pvalue = zend_hash_str_find(envelope, "return_path", sizeof("return_path") - 1)) != NULL) {
31233127
convert_to_string_ex(pvalue);
31243128
PHP_RFC822_PARSE_ADRLIST(&env->return_path, pvalue);
31253129
}
3126-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "date", sizeof("date") - 1)) != NULL) {
3130+
if ((pvalue = zend_hash_str_find(envelope, "date", sizeof("date") - 1)) != NULL) {
31273131
convert_to_string_ex(pvalue);
31283132
env->date = (unsigned char*)cpystr(Z_STRVAL_P(pvalue));
31293133
}
3130-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "from", sizeof("from") - 1)) != NULL) {
3134+
if ((pvalue = zend_hash_str_find(envelope, "from", sizeof("from") - 1)) != NULL) {
31313135
convert_to_string_ex(pvalue);
31323136
PHP_RFC822_PARSE_ADRLIST(&env->from, pvalue);
31333137
}
3134-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "reply_to", sizeof("reply_to") - 1)) != NULL) {
3138+
if ((pvalue = zend_hash_str_find(envelope, "reply_to", sizeof("reply_to") - 1)) != NULL) {
31353139
convert_to_string_ex(pvalue);
31363140
PHP_RFC822_PARSE_ADRLIST(&env->reply_to, pvalue);
31373141
}
3138-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "in_reply_to", sizeof("in_reply_to") - 1)) != NULL) {
3142+
if ((pvalue = zend_hash_str_find(envelope, "in_reply_to", sizeof("in_reply_to") - 1)) != NULL) {
31393143
convert_to_string_ex(pvalue);
31403144
env->in_reply_to = cpystr(Z_STRVAL_P(pvalue));
31413145
}
3142-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "subject", sizeof("subject") - 1)) != NULL) {
3146+
if ((pvalue = zend_hash_str_find(envelope, "subject", sizeof("subject") - 1)) != NULL) {
31433147
convert_to_string_ex(pvalue);
31443148
env->subject = cpystr(Z_STRVAL_P(pvalue));
31453149
}
3146-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "to", sizeof("to") - 1)) != NULL) {
3150+
if ((pvalue = zend_hash_str_find(envelope, "to", sizeof("to") - 1)) != NULL) {
31473151
convert_to_string_ex(pvalue);
31483152
PHP_RFC822_PARSE_ADRLIST(&env->to, pvalue);
31493153
}
3150-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "cc", sizeof("cc") - 1)) != NULL) {
3154+
if ((pvalue = zend_hash_str_find(envelope, "cc", sizeof("cc") - 1)) != NULL) {
31513155
convert_to_string_ex(pvalue);
31523156
PHP_RFC822_PARSE_ADRLIST(&env->cc, pvalue);
31533157
}
3154-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "bcc", sizeof("bcc") - 1)) != NULL) {
3158+
if ((pvalue = zend_hash_str_find(envelope, "bcc", sizeof("bcc") - 1)) != NULL) {
31553159
convert_to_string_ex(pvalue);
31563160
PHP_RFC822_PARSE_ADRLIST(&env->bcc, pvalue);
31573161
}
3158-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "message_id", sizeof("message_id") - 1)) != NULL) {
3162+
if ((pvalue = zend_hash_str_find(envelope, "message_id", sizeof("message_id") - 1)) != NULL) {
31593163
convert_to_string_ex(pvalue);
31603164
env->message_id=cpystr(Z_STRVAL_P(pvalue));
31613165
}
31623166

3163-
if ((pvalue = zend_hash_str_find(Z_ARRVAL_P(envelope), "custom_headers", sizeof("custom_headers") - 1)) != NULL) {
3167+
if ((pvalue = zend_hash_str_find(envelope, "custom_headers", sizeof("custom_headers") - 1)) != NULL) {
31643168
if (Z_TYPE_P(pvalue) == IS_ARRAY) {
31653169
custom_headers_param = tmp_param = NULL;
31663170
SEPARATE_ARRAY(pvalue);
@@ -3177,14 +3181,17 @@ PHP_FUNCTION(imap_mail_compose)
31773181
}
31783182

31793183
first = 1;
3180-
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(body), data) {
3184+
ZEND_HASH_FOREACH_VAL(body, data) {
31813185
if (first) {
31823186
first = 0;
31833187

31843188
if (Z_TYPE_P(data) != IS_ARRAY) {
3185-
// TODO ValueError
3186-
php_error_docref(NULL, E_WARNING, "body parameter must be a non-empty array");
3187-
RETVAL_FALSE;
3189+
zend_argument_type_error(2, "individual body must be of type array, %s given",
3190+
zend_zval_type_name(data));
3191+
goto done;
3192+
}
3193+
if (zend_hash_num_elements(Z_ARRVAL_P(data)) == 0) {
3194+
zend_argument_value_error(2, "individual body cannot be empty");
31883195
goto done;
31893196
}
31903197
SEPARATE_ARRAY(data);
@@ -3402,13 +3409,6 @@ PHP_FUNCTION(imap_mail_compose)
34023409
}
34033410
} ZEND_HASH_FOREACH_END();
34043411

3405-
if (first) {
3406-
// TODO ValueError
3407-
php_error_docref(NULL, E_WARNING, "body parameter must be a non-empty array");
3408-
RETVAL_FALSE;
3409-
goto done;
3410-
}
3411-
34123412
if (bod && bod->type == TYPEMULTIPART && (!bod->nested.part || !bod->nested.part->next)) {
34133413
php_error_docref(NULL, E_WARNING, "Cannot generate multipart e-mail without components.");
34143414
RETVAL_FALSE;

ext/imap/tests/bug45705_2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $envelope = array('return_path' => 'John Doe <john@example.com>',
1818
);
1919

2020
var_dump($envelope);
21-
imap_mail_compose($envelope, array(1 => array()));
21+
imap_mail_compose($envelope, [1 => ['cc' => 'Steve Doe <steve@example.com>',]]);
2222
var_dump($envelope);
2323

2424
?>

ext/imap/tests/bug80223.phpt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@ if (!extension_loaded('imap')) die('skip imap extension not available');
66
?>
77
--FILE--
88
<?php
9-
imap_mail_compose([], []);
10-
imap_mail_compose([], [1]);
9+
try {
10+
imap_mail_compose([], []);
11+
} catch (\ValueError $e) {
12+
echo $e->getMessage(), \PHP_EOL;
13+
}
14+
try {
15+
imap_mail_compose([], [1]);
16+
} catch (\TypeError $e) {
17+
echo $e->getMessage(), \PHP_EOL;
18+
}
19+
try {
20+
imap_mail_compose([], [[]]);
21+
} catch (\ValueError $e) {
22+
echo $e->getMessage(), \PHP_EOL;
23+
}
1124
?>
12-
--EXPECTF--
13-
Warning: imap_mail_compose(): body parameter must be a non-empty array in %s on line %d
14-
15-
Warning: imap_mail_compose(): body parameter must be a non-empty array in %s on line %d
25+
--EXPECT--
26+
imap_mail_compose(): Argument #2 ($bodies) cannot be empty
27+
imap_mail_compose(): Argument #2 ($bodies) individual body must be of type array, int given
28+
imap_mail_compose(): Argument #2 ($bodies) individual body cannot be empty

ext/imap/tests/imap_body_basic.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ Create a new mailbox for test
3737
Create a temporary mailbox and add 1 msgs
3838
.. mailbox '%s' created
3939
Msg Count in new mailbox: 1
40-
string(%d) "1: this is a test message, please ignore%a"
41-
string(%d) "1: this is a test message, please ignore%a"
40+
string(%d) "1: this is a test message, please ignore
41+
"
42+
string(%d) "1: this is a test message, please ignore
43+
"

0 commit comments

Comments
 (0)