Issues

ZF-9486: zend_mail_transport_sendmail::_sendMail() missing restore_error_handler() on exit path

Issue Type: Patch Created: 2010-03-19T23:50:19.000+0000 Last Updated: 2010-03-22T05:53:31.000+0000 Status: Resolved Fix version(s): - 1.10.3 (01/Apr/10)

Reporter: John Crenshaw (bugslayer) Assignee: Satoru Yoshida (satoruyoshida) Tags: - Zend_Mail

Related issues: Attachments:

Description

In Zend_Mail_Transport_Sendmail::_sendMail() there is a minor bug. If the first error is thrown, restore_error_handler() is never called, therefore 'leaking' the previously set '_handleMailErrors' handler, and corrupting the handler stack. Solution is to add a call to restore_error_handler() before throwing, or else refactor the code to prevent the unexpected exit path.

See code below for a sample fix:

<pre class="highlight">
    public function _sendMail()
    {
        set_error_handler(array($this, '_handleMailErrors'));
        if ($this->parameters === null) {
            $result = mail(
                $this->recipients,
                $this->_mail->getSubject(),
                $this->body,
                $this->header);
        } else {
            if(!is_string($this->parameters)) {
                /**
                 * @see Zend_Mail_Transport_Exception
                 * 
                 * Exception is thrown here because
                 * $parameters is a public property
                 */

                // FIX: set_error_handler was called earlier, we need to
                // reverse that before returning.
                restore_error_handler();

                require_once 'Zend/Mail/Transport/Exception.php';
                throw new Zend_Mail_Transport_Exception(
                    'Parameters were set but are not a string'
                );
            }

            $result = mail(
                $this->recipients,
                $this->_mail->getSubject(),
                $this->body,
                $this->header,
                $this->parameters);
        }
        restore_error_handler();

        if ($this->_errstr !== null || !$result) {
            /**
             * @see Zend_Mail_Transport_Exception
             */
            require_once 'Zend/Mail/Transport/Exception.php';
            throw new Zend_Mail_Transport_Exception('Unable to send mail. ' . $this->_errstr);
        }
    }

Comments

Posted by Satoru Yoshida (satoruyoshida) on 2010-03-22T05:53:31.000+0000

Thank You for report, John. Solved at SVN r21603

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts