From 2e923877b5b351aece973bb2b280081b34440dfd Mon Sep 17 00:00:00 2001 From: Alexander Reifinger Date: Wed, 12 Jul 2017 14:28:11 +0200 Subject: [PATCH] Fix endless loop on Spamassessin crash When handling large messages, SpamAssassin sometimes crashes during analysing. The fgets() function than returns false, which is not caught. Templateria is then caught in an endless loop. This patch will fix that. --- src/Spamassassin/Client.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Spamassassin/Client.php b/src/Spamassassin/Client.php index 57549e2..e084159 100644 --- a/src/Spamassassin/Client.php +++ b/src/Spamassassin/Client.php @@ -142,6 +142,7 @@ protected function write($socket, $data) * @param resource $socket Socket connection created by getSocket() * * @return array Array containing output headers and message + * @throws \Exception In case the server goes away */ protected function read($socket) { @@ -150,6 +151,9 @@ protected function read($socket) while (true) { $buffer = fgets($socket, 128); + if($buffer === false) { + throw new \Exception('No response from SpamAssassin Server'); + } $headers .= $buffer; if ($buffer == "\r\n" || feof($socket)) { break; @@ -158,6 +162,9 @@ protected function read($socket) while (!feof($socket)) { $message .= fgets($socket, 128); + if($message === false) { + throw new \Exception('No response from SpamAssassin Server'); + } } fclose($socket); @@ -261,7 +268,7 @@ protected function parseOutput($header, $message) /** * Pings the server to check the connection - * + * * @return bool */ public function ping() @@ -282,7 +289,7 @@ public function ping() * Returns a detailed report if the message is spam or null if it's ham * * @param string $message Email message - * + * * @return Result Detailed spam report */ public function getSpamReport($message) @@ -416,9 +423,9 @@ public function learn($message, $learnType = self::LEARN_SPAM) /** * Report message as spam, both local and remote. - * + * * @param string $message Raw email message - * + * * @return bool */ public function report($message) @@ -433,9 +440,9 @@ public function report($message) /** * Revokes a message previously reported as spam. - * + * * @param string $message Raw email message - * + * * @return bool */ public function revoke($message)