From 0295f2e3aef6f96786ac6af20451dc85b2e5c8eb Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Mon, 21 Sep 2020 16:11:18 +0100 Subject: [PATCH 1/6] Refactor imap_mail()'s internal implementation to use zend_strings --- ext/imap/php_imap.c | 47 +++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c index 27ff2058c7a3b..7cd5bb199b03a 100644 --- a/ext/imap/php_imap.c +++ b/ext/imap/php_imap.c @@ -3411,7 +3411,8 @@ PHP_FUNCTION(imap_mail_compose) /* }}} */ /* {{{ _php_imap_mail */ -int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *cc, char *bcc, char* rpath) +bool _php_imap_mail(zend_string *to, zend_string *subject, zend_string *message, zend_string *headers, + zend_string *cc, zend_string *bcc, zend_string* rpath) { #ifdef PHP_WIN32 int tsm_err; @@ -3429,13 +3430,13 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char * size_t bt_len; if (headers) { - bufferLen += strlen(headers); + bufferLen += ZSTR_LEN(headers); } if (to) { - bufferLen += strlen(to) + 6; + bufferLen += ZSTR_LEN(to) + 6; } if (cc) { - bufferLen += strlen(cc) + 6; + bufferLen += ZSTR_LEN(cc) + 6; } #define PHP_IMAP_CLEAN if (bufferTo) efree(bufferTo); if (bufferCc) efree(bufferCc); if (bufferBcc) efree(bufferBcc); if (bufferHeader) efree(bufferHeader); @@ -3445,10 +3446,10 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char * memset(bufferHeader, 0, bufferLen); if (to && *to) { strlcat(bufferHeader, "To: ", bufferLen + 1); - strlcat(bufferHeader, to, bufferLen + 1); + strlcat(bufferHeader, ZSTR_VAL(to), bufferLen + 1); strlcat(bufferHeader, "\r\n", bufferLen + 1); - tempMailTo = estrdup(to); - bt_len = strlen(to); + tempMailTo = estrdup(ZSTR_VAL(to)); + bt_len = ZSTR_LEN(to); bufferTo = (char *)safe_emalloc(bt_len, 1, 1); bt_len++; offset = 0; @@ -3474,10 +3475,10 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char * if (cc && *cc) { strlcat(bufferHeader, "Cc: ", bufferLen + 1); - strlcat(bufferHeader, cc, bufferLen + 1); + strlcat(bufferHeader, ZSTR_VAL(cc), bufferLen + 1); strlcat(bufferHeader, "\r\n", bufferLen + 1); - tempMailTo = estrdup(cc); - bt_len = strlen(cc); + tempMailTo = estrdup(ZSTR_VAL(cc)); + bt_len = ZSTR_LEN(cc); bufferCc = (char *)safe_emalloc(bt_len, 1, 1); bt_len++; offset = 0; @@ -3502,8 +3503,8 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char * } if (bcc && *bcc) { - tempMailTo = estrdup(bcc); - bt_len = strlen(bcc); + tempMailTo = estrdup(ZSTR_VAL(bcc)); + bt_len = ZSTR_LEN(bcc); bufferBcc = (char *)safe_emalloc(bt_len, 1, 1); bt_len++; offset = 0; @@ -3528,10 +3529,11 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char * } if (headers && *headers) { - strlcat(bufferHeader, headers, bufferLen + 1); + strlcat(bufferHeader, ZSTR_VAL(headers), bufferLen + 1); } - if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, bufferHeader, subject, bufferTo, message, bufferCc, bufferBcc, rpath) != SUCCESS) { + if (TSendMail(INI_STR("SMTP"), &tsm_err, &tsm_errmsg, bufferHeader, ZSTR_VAL(subject), + bufferTo, ZSTR_VAL(message), bufferCc, bufferBcc, ZSTR_VAL(rpath)) != SUCCESS) { if (tsm_errmsg) { php_error_docref(NULL, E_WARNING, "%s", tsm_errmsg); efree(tsm_errmsg); @@ -3548,15 +3550,15 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char * } sendmail = popen(INI_STR("sendmail_path"), "w"); if (sendmail) { - if (rpath && rpath[0]) fprintf(sendmail, "From: %s\n", rpath); - fprintf(sendmail, "To: %s\n", to); - if (cc && cc[0]) fprintf(sendmail, "Cc: %s\n", cc); - if (bcc && bcc[0]) fprintf(sendmail, "Bcc: %s\n", bcc); - fprintf(sendmail, "Subject: %s\n", subject); + if (ZSTR_LEN(rpath) != 0) fprintf(sendmail, "From: %s\n", ZSTR_VAL(rpath)); + fprintf(sendmail, "To: %s\n", ZSTR_VAL(to)); + if (ZSTR_LEN(cc) != 0) fprintf(sendmail, "Cc: %s\n", ZSTR_VAL(cc)); + if (ZSTR_LEN(bcc) != 0) fprintf(sendmail, "Bcc: %s\n", ZSTR_VAL(bcc)); + fprintf(sendmail, "Subject: %s\n", ZSTR_VAL(subject)); if (headers != NULL) { - fprintf(sendmail, "%s\n", headers); + fprintf(sendmail, "%s\n", ZSTR_VAL(headers)); } - fprintf(sendmail, "\n%s\n", message); + fprintf(sendmail, "\n%s\n", ZSTR_VAL(message)); ret = pclose(sendmail); return ret != -1; @@ -3598,8 +3600,7 @@ PHP_FUNCTION(imap_mail) php_error_docref(NULL, E_WARNING, "No message string in mail command"); } - if (_php_imap_mail(ZSTR_VAL(to), ZSTR_VAL(subject), ZSTR_VAL(message), headers?ZSTR_VAL(headers):NULL, cc?ZSTR_VAL(cc):NULL, - bcc?ZSTR_VAL(bcc):NULL, rpath?ZSTR_VAL(rpath):NULL)) { + if (_php_imap_mail(to, subject, message, headers, cc, bcc, rpath)) { RETURN_TRUE; } else { RETURN_FALSE; From e77e90dd89b3b1717e449192d2c2594823d733b1 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Mon, 21 Sep 2020 16:12:46 +0100 Subject: [PATCH 2/6] Ensure strings passed to imap_mail() don't contain null bytes --- ext/imap/php_imap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c index 7cd5bb199b03a..2125d2976a451 100644 --- a/ext/imap/php_imap.c +++ b/ext/imap/php_imap.c @@ -3577,7 +3577,7 @@ PHP_FUNCTION(imap_mail) zend_string *to=NULL, *message=NULL, *headers=NULL, *subject=NULL, *cc=NULL, *bcc=NULL, *rpath=NULL; int argc = ZEND_NUM_ARGS(); - if (zend_parse_parameters(argc, "SSS|SSSS", &to, &subject, &message, + if (zend_parse_parameters(argc, "PPP|PPPP", &to, &subject, &message, &headers, &cc, &bcc, &rpath) == FAILURE) { RETURN_THROWS(); } From b63564c24ec853d5ff738e53829524ed4568583b Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Mon, 21 Sep 2020 16:19:10 +0100 Subject: [PATCH 3/6] Use some proper null defaults in IMAP --- ext/imap/php_imap.c | 10 +++++----- ext/imap/php_imap.stub.php | 6 +++--- ext/imap/php_imap_arginfo.h | 18 +++++++++--------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c index 2125d2976a451..7b25f37b52565 100644 --- a/ext/imap/php_imap.c +++ b/ext/imap/php_imap.c @@ -866,7 +866,7 @@ PHP_FUNCTION(imap_append) pils *imap_le_struct; STRING st; - if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSS|SS", &streamind, &folder, &message, &flags, &internal_date) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS(), "rSS|S!S!", &streamind, &folder, &message, &flags, &internal_date) == FAILURE) { RETURN_THROWS(); } @@ -2647,7 +2647,7 @@ PHP_FUNCTION(imap_sort) SEARCHPGM *spg=NIL; int argc = ZEND_NUM_ARGS(); - if (zend_parse_parameters(argc, "rll|lSS", &streamind, &pgm, &rev, &flags, &criteria, &charset) == FAILURE) { + if (zend_parse_parameters(argc, "rll|lS!S!", &streamind, &pgm, &rev, &flags, &criteria, &charset) == FAILURE) { RETURN_THROWS(); } @@ -2665,7 +2665,7 @@ PHP_FUNCTION(imap_sort) RETURN_FALSE; } } - if (argc >= 5) { + if (criteria) { search_criteria = estrndup(ZSTR_VAL(criteria), ZSTR_LEN(criteria)); spg = mail_criteria(search_criteria); efree(search_criteria); @@ -2678,7 +2678,7 @@ PHP_FUNCTION(imap_sort) mypgm->function = (short) pgm; mypgm->next = NIL; - slst = mail_sort(imap_le_struct->imap_stream, (argc == 6 ? ZSTR_VAL(charset) : NIL), spg, mypgm, (argc >= 4 ? flags : NIL)); + slst = mail_sort(imap_le_struct->imap_stream, (charset ? ZSTR_VAL(charset) : NIL), spg, mypgm, (argc >= 4 ? flags : NIL)); if (spg && !(flags & SE_FREE)) { mail_free_searchpgm(&spg); @@ -3577,7 +3577,7 @@ PHP_FUNCTION(imap_mail) zend_string *to=NULL, *message=NULL, *headers=NULL, *subject=NULL, *cc=NULL, *bcc=NULL, *rpath=NULL; int argc = ZEND_NUM_ARGS(); - if (zend_parse_parameters(argc, "PPP|PPPP", &to, &subject, &message, + if (zend_parse_parameters(argc, "PPP|P!P!P!P!", &to, &subject, &message, &headers, &cc, &bcc, &rpath) == FAILURE) { RETURN_THROWS(); } diff --git a/ext/imap/php_imap.stub.php b/ext/imap/php_imap.stub.php index 9d2e1082c1a7c..88ded45db5325 100644 --- a/ext/imap/php_imap.stub.php +++ b/ext/imap/php_imap.stub.php @@ -148,7 +148,7 @@ function imap_subscribe($stream_id, string $mailbox): bool {} function imap_unsubscribe($stream_id, string $mailbox): bool {} /** @param resource $stream_id */ -function imap_append($stream_id, string $folder, string $message, string $options = UNKNOWN, string $internal_date = UNKNOWN): bool {} +function imap_append($stream_id, string $folder, string $message, ?string $options = null, ?string $internal_date = null): bool {} /** @param resource $stream_id */ function imap_ping($stream_id): bool {} @@ -180,7 +180,7 @@ function imap_setflag_full($stream_id, string $sequence, string $flag, int $opti function imap_clearflag_full($stream_id, string $sequence, string $flag, int $options = 0): bool {} /** @param resource $stream_id */ -function imap_sort($stream_id, int $criteria, int $reverse, int $options = 0, string $search_criteria = UNKNOWN, string $charset = UNKNOWN): array|false {} +function imap_sort($stream_id, int $criteria, int $reverse, int $options = 0, ?string $search_criteria = null, ?string $charset = null): array|false {} /** @param resource $stream_id */ function imap_uid($stream_id, int $msg_no): int|false {} @@ -258,4 +258,4 @@ function imap_setacl($stream_id, string $mailbox, string $id, string $rights): b function imap_getacl($stream_id, string $mailbox): array|false {} #endif -function imap_mail(string $to, string $subject, string $message, string $additional_headers = UNKNOWN, string $cc = UNKNOWN, string $bcc = UNKNOWN, string $rpath = UNKNOWN): bool {} +function imap_mail(string $to, string $subject, string $message, ?string $additional_headers = null, ?string $cc = null, ?string $bcc = null, ?string $rpath = null): bool {} diff --git a/ext/imap/php_imap_arginfo.h b/ext/imap/php_imap_arginfo.h index 69fe25e4d0f4c..6b545f24f8554 100644 --- a/ext/imap/php_imap_arginfo.h +++ b/ext/imap/php_imap_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 4991f82d78672ab23044864fa6dd75851952965c */ + * Stub hash: a1b1cb1bb36085cb43254b9d708585352d3cf72a */ ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_open, 0, 0, 3) ZEND_ARG_TYPE_INFO(0, mailbox, IS_STRING, 0) @@ -173,8 +173,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imap_append, 0, 3, _IS_BOOL, 0) ZEND_ARG_INFO(0, stream_id) ZEND_ARG_TYPE_INFO(0, folder, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0) - ZEND_ARG_TYPE_INFO(0, options, IS_STRING, 0) - ZEND_ARG_TYPE_INFO(0, internal_date, IS_STRING, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_STRING, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, internal_date, IS_STRING, 1, "null") ZEND_END_ARG_INFO() #define arginfo_imap_ping arginfo_imap_expunge @@ -217,8 +217,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_imap_sort, 0, 3, MAY_BE_ARRAY|MA ZEND_ARG_TYPE_INFO(0, criteria, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, reverse, IS_LONG, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_LONG, 0, "0") - ZEND_ARG_TYPE_INFO(0, search_criteria, IS_STRING, 0) - ZEND_ARG_TYPE_INFO(0, charset, IS_STRING, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, search_criteria, IS_STRING, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, charset, IS_STRING, 1, "null") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_imap_uid, 0, 2, MAY_BE_LONG|MAY_BE_FALSE) @@ -342,10 +342,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imap_mail, 0, 3, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, to, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, subject, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, message, IS_STRING, 0) - ZEND_ARG_TYPE_INFO(0, additional_headers, IS_STRING, 0) - ZEND_ARG_TYPE_INFO(0, cc, IS_STRING, 0) - ZEND_ARG_TYPE_INFO(0, bcc, IS_STRING, 0) - ZEND_ARG_TYPE_INFO(0, rpath, IS_STRING, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, additional_headers, IS_STRING, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, cc, IS_STRING, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, bcc, IS_STRING, 1, "null") + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, rpath, IS_STRING, 1, "null") ZEND_END_ARG_INFO() From d61bd516d41725d0b3423461fd6bfe6cc9899ddf Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Mon, 21 Sep 2020 16:24:21 +0100 Subject: [PATCH 4/6] Use a proper empty array default value for imap_open() --- ext/imap/php_imap.c | 6 +++--- ext/imap/php_imap.stub.php | 2 +- ext/imap/php_imap_arginfo.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c index 7b25f37b52565..a7e68f4a6d5a1 100644 --- a/ext/imap/php_imap.c +++ b/ext/imap/php_imap.c @@ -702,10 +702,10 @@ static void php_imap_do_open(INTERNAL_FUNCTION_PARAMETERS, int persistent) zend_long retries = 0, flags = NIL, cl_flags = NIL; MAILSTREAM *imap_stream; pils *imap_le_struct; - zval *params = NULL; + HashTable *params = NULL; int argc = ZEND_NUM_ARGS(); - if (zend_parse_parameters(argc, "PSS|lla", &mailbox, &user, + if (zend_parse_parameters(argc, "PSS|llh", &mailbox, &user, &passwd, &flags, &retries, ¶ms) == FAILURE) { RETURN_THROWS(); } @@ -723,7 +723,7 @@ static void php_imap_do_open(INTERNAL_FUNCTION_PARAMETERS, int persistent) if (params) { zval *disabled_auth_method; - if ((disabled_auth_method = zend_hash_str_find(Z_ARRVAL_P(params), "DISABLE_AUTHENTICATOR", sizeof("DISABLE_AUTHENTICATOR") - 1)) != NULL) { + if ((disabled_auth_method = zend_hash_str_find(params, "DISABLE_AUTHENTICATOR", sizeof("DISABLE_AUTHENTICATOR") - 1)) != NULL) { switch (Z_TYPE_P(disabled_auth_method)) { case IS_STRING: if (Z_STRLEN_P(disabled_auth_method) > 1) { diff --git a/ext/imap/php_imap.stub.php b/ext/imap/php_imap.stub.php index 88ded45db5325..7f4cfe2ef797a 100644 --- a/ext/imap/php_imap.stub.php +++ b/ext/imap/php_imap.stub.php @@ -5,7 +5,7 @@ /** * @return resource|false */ -function imap_open(string $mailbox, string $user, string $password, int $options = 0, int $n_retries = 0, array $params = UNKNOWN) {} +function imap_open(string $mailbox, string $user, string $password, int $options = 0, int $n_retries = 0, array $params = []) {} /** * @param resource $stream_id diff --git a/ext/imap/php_imap_arginfo.h b/ext/imap/php_imap_arginfo.h index 6b545f24f8554..52931df1a1916 100644 --- a/ext/imap/php_imap_arginfo.h +++ b/ext/imap/php_imap_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: a1b1cb1bb36085cb43254b9d708585352d3cf72a */ + * Stub hash: fe7adff9478c5e0e2ad9df6f34dc8a862a537cb3 */ ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_open, 0, 0, 3) ZEND_ARG_TYPE_INFO(0, mailbox, IS_STRING, 0) @@ -7,7 +7,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_open, 0, 0, 3) ZEND_ARG_TYPE_INFO(0, password, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_LONG, 0, "0") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, n_retries, IS_LONG, 0, "0") - ZEND_ARG_TYPE_INFO(0, params, IS_ARRAY, 0) + ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, params, IS_ARRAY, 0, "[]") ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_imap_reopen, 0, 2, _IS_BOOL, 0) From 5ba4dca77d31b6dce9c7269335a7cb50376099b1 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Mon, 21 Sep 2020 16:26:35 +0100 Subject: [PATCH 5/6] Remove unused default_host argument from imap_headerinfo() --- ext/imap/php_imap.c | 3 +-- ext/imap/php_imap.stub.php | 8 +------- ext/imap/php_imap_arginfo.h | 6 +----- 3 files changed, 3 insertions(+), 14 deletions(-) diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c index a7e68f4a6d5a1..db07099ea5b3a 100644 --- a/ext/imap/php_imap.c +++ b/ext/imap/php_imap.c @@ -1603,7 +1603,6 @@ PHP_FUNCTION(imap_undelete) PHP_FUNCTION(imap_headerinfo) { zval *streamind; - zend_string *defaulthost = NULL; int argc = ZEND_NUM_ARGS(); zend_long msgno, fromlength, subjectlength; pils *imap_le_struct; @@ -1611,7 +1610,7 @@ PHP_FUNCTION(imap_headerinfo) ENVELOPE *en; char dummy[2000], fulladdress[MAILTMPLEN + 1]; - if (zend_parse_parameters(argc, "rl|llS", &streamind, &msgno, &fromlength, &subjectlength, &defaulthost) == FAILURE) { + if (zend_parse_parameters(argc, "rl|ll", &streamind, &msgno, &fromlength, &subjectlength) == FAILURE) { RETURN_THROWS(); } diff --git a/ext/imap/php_imap.stub.php b/ext/imap/php_imap.stub.php index 7f4cfe2ef797a..0a7adc2736977 100644 --- a/ext/imap/php_imap.stub.php +++ b/ext/imap/php_imap.stub.php @@ -27,13 +27,7 @@ function imap_num_recent($stream_id): int|false {} function imap_headers($stream_id): array|false {} /** @param resource $stream_id */ -function imap_headerinfo($stream_id, int $msg_no, int $from_length = 0, int $subject_length = 0, string $default_host = UNKNOWN): stdClass|false {} - -/** - * @param resource $stream_id - * @alias imap_headerinfo - */ -function imap_header($stream_id, int $msg_no, int $from_length = 0, int $subject_length = 0, string $default_host = UNKNOWN): stdClass|false {} +function imap_headerinfo($stream_id, int $msg_no, int $from_length = 0, int $subject_length = 0): stdClass|false {} function imap_rfc822_parse_headers(string $headers, string $default_host = "UNKNOWN"): stdClass {} diff --git a/ext/imap/php_imap_arginfo.h b/ext/imap/php_imap_arginfo.h index 52931df1a1916..ffb60a034f890 100644 --- a/ext/imap/php_imap_arginfo.h +++ b/ext/imap/php_imap_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: fe7adff9478c5e0e2ad9df6f34dc8a862a537cb3 */ + * Stub hash: e501d6869d721ad720a1a7c8b597b96e9591d5ed */ ZEND_BEGIN_ARG_INFO_EX(arginfo_imap_open, 0, 0, 3) ZEND_ARG_TYPE_INFO(0, mailbox, IS_STRING, 0) @@ -37,11 +37,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_imap_headerinfo, 0, 2, stdCl ZEND_ARG_TYPE_INFO(0, msg_no, IS_LONG, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, from_length, IS_LONG, 0, "0") ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, subject_length, IS_LONG, 0, "0") - ZEND_ARG_TYPE_INFO(0, default_host, IS_STRING, 0) ZEND_END_ARG_INFO() -#define arginfo_imap_header arginfo_imap_headerinfo - ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_imap_rfc822_parse_headers, 0, 1, stdClass, 0) ZEND_ARG_TYPE_INFO(0, headers, IS_STRING, 0) ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, default_host, IS_STRING, 0, "\"UNKNOWN\"") @@ -440,7 +437,6 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(imap_num_recent, arginfo_imap_num_recent) ZEND_FE(imap_headers, arginfo_imap_headers) ZEND_FE(imap_headerinfo, arginfo_imap_headerinfo) - ZEND_FALIAS(imap_header, imap_headerinfo, arginfo_imap_header) ZEND_FE(imap_rfc822_parse_headers, arginfo_imap_rfc822_parse_headers) ZEND_FE(imap_rfc822_write_address, arginfo_imap_rfc822_write_address) ZEND_FE(imap_rfc822_parse_adrlist, arginfo_imap_rfc822_parse_adrlist) From 7e0ab1c242e7a0992d8e38a054364af562801415 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Tue, 22 Sep 2020 14:04:51 +0100 Subject: [PATCH 6/6] Fix imap_mail() refactoring on Windows --- ext/imap/php_imap.c | 66 +++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/ext/imap/php_imap.c b/ext/imap/php_imap.c index db07099ea5b3a..9e9ca677256db 100644 --- a/ext/imap/php_imap.c +++ b/ext/imap/php_imap.c @@ -3420,6 +3420,9 @@ bool _php_imap_mail(zend_string *to, zend_string *subject, zend_string *message, int ret; #endif + ZEND_ASSERT(to && ZSTR_LEN(to) != 0); + ZEND_ASSERT(subject && ZSTR_LEN(subject) != 0); + #ifdef PHP_WIN32 char *tempMailTo; char *tsm_errmsg = NULL; @@ -3428,12 +3431,11 @@ bool _php_imap_mail(zend_string *to, zend_string *subject, zend_string *message, size_t offset, bufferLen = 0; size_t bt_len; + /* Add "To" field's necessary buffer length */ + bufferLen += ZSTR_LEN(to) + 6; if (headers) { bufferLen += ZSTR_LEN(headers); } - if (to) { - bufferLen += ZSTR_LEN(to) + 6; - } if (cc) { bufferLen += ZSTR_LEN(cc) + 6; } @@ -3443,36 +3445,36 @@ bool _php_imap_mail(zend_string *to, zend_string *subject, zend_string *message, bufferHeader = (char *)safe_emalloc(bufferLen, 1, 1); memset(bufferHeader, 0, bufferLen); - if (to && *to) { - strlcat(bufferHeader, "To: ", bufferLen + 1); - strlcat(bufferHeader, ZSTR_VAL(to), bufferLen + 1); - strlcat(bufferHeader, "\r\n", bufferLen + 1); - tempMailTo = estrdup(ZSTR_VAL(to)); - bt_len = ZSTR_LEN(to); - bufferTo = (char *)safe_emalloc(bt_len, 1, 1); - bt_len++; - offset = 0; - addr = NULL; - rfc822_parse_adrlist(&addr, tempMailTo, "NO HOST"); - while (addr) { - if (addr->host == NULL || strcmp(addr->host, ERRHOST) == 0) { - PHP_IMAP_BAD_DEST; - } else { - bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->mailbox)); - bt_len += strlen(addr->mailbox); - bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->host)); - bt_len += strlen(addr->host); - offset += slprintf(bufferTo + offset, bt_len - offset, "%s@%s,", addr->mailbox, addr->host); - } - addr = addr->next; - } - efree(tempMailTo); - if (offset>0) { - bufferTo[offset-1] = 0; + + /* Handle "To" Field */ + strlcat(bufferHeader, "To: ", bufferLen + 1); + strlcat(bufferHeader, ZSTR_VAL(to), bufferLen + 1); + strlcat(bufferHeader, "\r\n", bufferLen + 1); + tempMailTo = estrdup(ZSTR_VAL(to)); + bt_len = ZSTR_LEN(to); + bufferTo = (char *)safe_emalloc(bt_len, 1, 1); + bt_len++; + offset = 0; + addr = NULL; + rfc822_parse_adrlist(&addr, tempMailTo, "NO HOST"); + while (addr) { + if (addr->host == NULL || strcmp(addr->host, ERRHOST) == 0) { + PHP_IMAP_BAD_DEST; + } else { + bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->mailbox)); + bt_len += strlen(addr->mailbox); + bufferTo = safe_erealloc(bufferTo, bt_len, 1, strlen(addr->host)); + bt_len += strlen(addr->host); + offset += slprintf(bufferTo + offset, bt_len - offset, "%s@%s,", addr->mailbox, addr->host); } + addr = addr->next; + } + efree(tempMailTo); + if (offset>0) { + bufferTo[offset-1] = 0; } - if (cc && *cc) { + if (cc && ZSTR_LEN(cc) != 0) { strlcat(bufferHeader, "Cc: ", bufferLen + 1); strlcat(bufferHeader, ZSTR_VAL(cc), bufferLen + 1); strlcat(bufferHeader, "\r\n", bufferLen + 1); @@ -3501,7 +3503,7 @@ bool _php_imap_mail(zend_string *to, zend_string *subject, zend_string *message, } } - if (bcc && *bcc) { + if (bcc && ZSTR_LEN(bcc)) { tempMailTo = estrdup(ZSTR_VAL(bcc)); bt_len = ZSTR_LEN(bcc); bufferBcc = (char *)safe_emalloc(bt_len, 1, 1); @@ -3527,7 +3529,7 @@ bool _php_imap_mail(zend_string *to, zend_string *subject, zend_string *message, } } - if (headers && *headers) { + if (headers && ZSTR_LEN(headers)) { strlcat(bufferHeader, ZSTR_VAL(headers), bufferLen + 1); }