Skip to content

Commit 876fb36

Browse files
committed
ext/standard/mail.c: use guard pattern to reduce nesting
1 parent d85b4f6 commit 876fb36

File tree

1 file changed

+86
-87
lines changed

1 file changed

+86
-87
lines changed

ext/standard/mail.c

Lines changed: 86 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -586,122 +586,121 @@ PHPAPI bool php_mail(const char *to, const char *subject, const zend_string *mes
586586
efree (sendmail_cmd);
587587
}
588588

589-
if (sendmail) {
590-
int ret;
591-
#ifndef PHP_WIN32
592-
if (EACCES == errno) {
593-
php_error_docref(NULL, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
594-
pclose(sendmail);
589+
if (UNEXPECTED(!sendmail)) {
590+
php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
595591
#if PHP_SIGCHILD
596-
/* Restore handler in case of error on Windows
597-
Not sure if this applicable on Win but just in case. */
598-
if (sig_handler) {
599-
signal(SIGCHLD, sig_handler);
600-
}
601-
#endif
602-
MAIL_RET(false);
592+
if (sig_handler) {
593+
signal(SIGCHLD, sig_handler);
603594
}
604595
#endif
605-
fprintf(sendmail, "To: %s%s", to, line_sep);
606-
fprintf(sendmail, "Subject: %s%s", subject, line_sep);
607-
if (headers && ZSTR_LEN(headers)) {
608-
fprintf(sendmail, "%s%s", ZSTR_VAL(headers), line_sep);
596+
MAIL_RET(false);
597+
}
598+
599+
#ifndef PHP_WIN32
600+
if (EACCES == errno) {
601+
php_error_docref(NULL, E_WARNING, "Permission denied: unable to execute shell to run mail delivery binary '%s'", sendmail_path);
602+
pclose(sendmail);
603+
#if PHP_SIGCHILD
604+
/* Restore handler in case of error on Windows
605+
Not sure if this applicable on Win but just in case. */
606+
if (sig_handler) {
607+
signal(SIGCHLD, sig_handler);
609608
}
609+
#endif
610+
MAIL_RET(false);
611+
}
612+
#endif
613+
fprintf(sendmail, "To: %s%s", to, line_sep);
614+
fprintf(sendmail, "Subject: %s%s", subject, line_sep);
615+
if (headers && ZSTR_LEN(headers)) {
616+
fprintf(sendmail, "%s%s", ZSTR_VAL(headers), line_sep);
617+
}
610618

611-
fprintf(sendmail, "%s", line_sep);
612-
613-
if (cr_lf_mode && zend_string_equals_literal(cr_lf_mode, "lf")) {
614-
char *converted_message = NULL;
615-
size_t msg_len = ZSTR_LEN(message);
616-
size_t new_len = 0;
617-
618-
if (msg_len > 0) {
619-
for (size_t i = 0; i < msg_len - 1; ++i) {
620-
if (ZSTR_VAL(message)[i] == '\r' && ZSTR_VAL(message)[i + 1] == '\n') {
621-
++new_len;
622-
}
619+
fprintf(sendmail, "%s", line_sep);
620+
621+
if (cr_lf_mode && zend_string_equals_literal(cr_lf_mode, "lf")) {
622+
char *converted_message = NULL;
623+
size_t msg_len = ZSTR_LEN(message);
624+
size_t new_len = 0;
625+
626+
if (msg_len > 0) {
627+
for (size_t i = 0; i < msg_len - 1; ++i) {
628+
if (ZSTR_VAL(message)[i] == '\r' && ZSTR_VAL(message)[i + 1] == '\n') {
629+
++new_len;
623630
}
631+
}
624632

625-
if (new_len == 0) {
626-
fprintf(sendmail, "%s", ZSTR_VAL(message));
627-
} else {
628-
converted_message = emalloc(msg_len - new_len + 1);
629-
size_t j = 0;
630-
for (size_t i = 0; i < msg_len; ++i) {
631-
if (i < msg_len - 1 && ZSTR_VAL(message)[i] == '\r' && ZSTR_VAL(message)[i + 1] == '\n') {
632-
converted_message[j++] = '\n';
633-
++i; /* skip LF part */
634-
} else {
635-
converted_message[j++] = ZSTR_VAL(message)[i];
636-
}
633+
if (new_len == 0) {
634+
fprintf(sendmail, "%s", ZSTR_VAL(message));
635+
} else {
636+
converted_message = emalloc(msg_len - new_len + 1);
637+
size_t j = 0;
638+
for (size_t i = 0; i < msg_len; ++i) {
639+
if (i < msg_len - 1 && ZSTR_VAL(message)[i] == '\r' && ZSTR_VAL(message)[i + 1] == '\n') {
640+
converted_message[j++] = '\n';
641+
++i; /* skip LF part */
642+
} else {
643+
converted_message[j++] = ZSTR_VAL(message)[i];
637644
}
638-
639-
converted_message[j] = '\0';
640-
fprintf(sendmail, "%s", converted_message);
641-
efree(converted_message);
642645
}
646+
647+
converted_message[j] = '\0';
648+
fprintf(sendmail, "%s", converted_message);
649+
efree(converted_message);
643650
}
644-
} else {
645-
fprintf(sendmail, "%s", ZSTR_VAL(message));
646651
}
652+
} else {
653+
fprintf(sendmail, "%s", ZSTR_VAL(message));
654+
}
655+
656+
fprintf(sendmail, "%s", line_sep);
647657

648-
fprintf(sendmail, "%s", line_sep);
658+
int ret;
649659
#ifdef PHP_WIN32
650-
ret = pclose(sendmail);
660+
ret = pclose(sendmail);
651661

652662
#if PHP_SIGCHILD
653-
if (sig_handler) {
654-
signal(SIGCHLD, sig_handler);
655-
}
663+
if (sig_handler) {
664+
signal(SIGCHLD, sig_handler);
665+
}
656666
#endif
657667
#else
658-
int wstatus = pclose(sendmail);
668+
int wstatus = pclose(sendmail);
659669
#if PHP_SIGCHILD
660-
if (sig_handler) {
661-
signal(SIGCHLD, sig_handler);
662-
}
670+
if (sig_handler) {
671+
signal(SIGCHLD, sig_handler);
672+
}
663673
#endif
664-
/* Determine the wait(2) exit status */
665-
if (wstatus == -1) {
666-
php_error_docref(NULL, E_WARNING, "Sendmail pclose failed %d (%s)", errno, strerror(errno));
667-
MAIL_RET(false);
668-
} else if (WIFSIGNALED(wstatus)) {
669-
php_error_docref(NULL, E_WARNING, "Sendmail killed by signal %d (%s)", WTERMSIG(wstatus), strsignal(WTERMSIG(wstatus)));
670-
MAIL_RET(false);
674+
/* Determine the wait(2) exit status */
675+
if (wstatus == -1) {
676+
php_error_docref(NULL, E_WARNING, "Sendmail pclose failed %d (%s)", errno, strerror(errno));
677+
MAIL_RET(false);
678+
} else if (WIFSIGNALED(wstatus)) {
679+
php_error_docref(NULL, E_WARNING, "Sendmail killed by signal %d (%s)", WTERMSIG(wstatus), strsignal(WTERMSIG(wstatus)));
680+
MAIL_RET(false);
681+
} else {
682+
if (WIFEXITED(wstatus)) {
683+
ret = WEXITSTATUS(wstatus);
671684
} else {
672-
if (WIFEXITED(wstatus)) {
673-
ret = WEXITSTATUS(wstatus);
674-
} else {
675-
php_error_docref(NULL, E_WARNING, "Sendmail did not exit");
676-
MAIL_RET(false);
677-
}
685+
php_error_docref(NULL, E_WARNING, "Sendmail did not exit");
686+
MAIL_RET(false);
678687
}
688+
}
679689
#endif
680690

681691
#if defined(EX_TEMPFAIL)
682-
if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
692+
if ((ret != EX_OK)&&(ret != EX_TEMPFAIL))
683693
#elif defined(EX_OK)
684-
if (ret != EX_OK)
694+
if (ret != EX_OK)
685695
#else
686-
if (ret != 0)
687-
#endif
688-
{
689-
php_error_docref(NULL, E_WARNING, "Sendmail exited with non-zero exit code %d", ret);
690-
MAIL_RET(false);
691-
} else {
692-
MAIL_RET(true);
693-
}
694-
} else {
695-
php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program '%s'", sendmail_path);
696-
#if PHP_SIGCHILD
697-
if (sig_handler) {
698-
signal(SIGCHLD, sig_handler);
699-
}
696+
if (ret != 0)
700697
#endif
698+
{
699+
php_error_docref(NULL, E_WARNING, "Sendmail exited with non-zero exit code %d", ret);
701700
MAIL_RET(false);
701+
} else {
702+
MAIL_RET(true);
702703
}
703-
704-
MAIL_RET(true); /* never reached */
705704
}
706705
/* }}} */
707706

0 commit comments

Comments
 (0)