From ab50417b74b590bd1eef7a61216a4a5c21a8e5d4 Mon Sep 17 00:00:00 2001 From: Arne Blankerts Date: Tue, 19 Mar 2019 23:27:46 +0100 Subject: [PATCH] Catch potential NULL return value from preg_replace_callback For reference: issue #356 --- src/collector/backend/SourceFileException.php | 2 +- src/collector/project/SourceFile.php | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/collector/backend/SourceFileException.php b/src/collector/backend/SourceFileException.php index 2baa4ab4..63220328 100644 --- a/src/collector/backend/SourceFileException.php +++ b/src/collector/backend/SourceFileException.php @@ -3,6 +3,6 @@ class SourceFileException extends \Exception { public const BadEncoding = 1; - public const InvalidDataBytes = 2; + public const RegexError = 3; } diff --git a/src/collector/project/SourceFile.php b/src/collector/project/SourceFile.php index c81f1a1c..418b7620 100644 --- a/src/collector/project/SourceFile.php +++ b/src/collector/project/SourceFile.php @@ -43,7 +43,7 @@ public function getSource(): string { $source = \file_get_contents($this->getPathname()); - if ($source == '') { + if ($source === '') { $this->src = ''; return ''; @@ -71,6 +71,13 @@ function (array $matches) { $source ); + if ($this->src === null) { + throw new SourceFileException( + sprintf('Error %d while executing regular expression callback', \preg_last_error()), + SourceFileException::RegexError + ); + } + return $this->src; }