Skip to content

Commit

Permalink
Fix GH-7875: mails are sent even if failure to log throws exception
Browse files Browse the repository at this point in the history
We explicitly check for an exception after the logging attempt, and
bail out in that case.

Co-authored-by: Tim Düsterhus <timwolla@googlemail.com>

Closes GH-7878.
  • Loading branch information
cmb69 committed Jan 17, 2022
1 parent ee8f9d7 commit 478edcd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ PHP NEWS
- Sockets:
. Fixed ext/sockets build on Haiku. (David Carlier)

- Standard:
. Fixed bug GH-7875 (mails are sent even if failure to log throws exception).
(cmb)

20 Jan 2022, PHP 8.0.15

- Core:
Expand Down
4 changes: 4 additions & 0 deletions ext/standard/mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ PHPAPI int php_mail(const char *to, const char *subject, const char *message, co
efree(logline);
}

if (EG(exception)) {
MAIL_RET(0);
}

if (PG(mail_x_header)) {
const char *tmp = zend_get_executed_filename();
zend_string *f;
Expand Down
35 changes: 35 additions & 0 deletions ext/standard/tests/mail/gh7875.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
GH-7875 (mails are sent even if failure to log throws exception)
--INI--
sendmail_path={MAIL:{PWD}/gh7875.mail.out}
mail.log={PWD}/gh7875.mail.log
--FILE--
<?php
function exception_error_handler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
return;
}
throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");

touch(__DIR__ . "/gh7875.mail.log");
chmod(__DIR__ . "/gh7875.mail.log", 0444);

try {
mail('recipient@example.com', 'Subject', 'Body', []);
echo 'Not Reached';
} catch (\Exception $e) {
echo $e->getMessage(), PHP_EOL;
var_dump(file_exists(__DIR__ . "/gh7875.mail.out"));
}
?>
--CLEAN--
<?php
@chmod(__DIR__ . "/gh7875.mail.log", 0644);
@unlink(__DIR__ . "/gh7875.mail.log");
@unlink(__DIR__ . "/gh7875.mail.out");
?>
--EXPECTF--
mail(%s): Failed to open stream: Permission denied
bool(false)

0 comments on commit 478edcd

Please sign in to comment.