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

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:


    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

Thank You for report, John. Solved at SVN r21603