From 5d7ca0075904c7ba8d534b4f59f5a40b00e89733 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Mon, 27 May 2024 16:15:26 +0545 Subject: [PATCH] throw ParseException when null input is provided --- lib/Parser/MimeDir.php | 6 ++++++ tests/VObject/Parser/MimeDirTest.php | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/Parser/MimeDir.php b/lib/Parser/MimeDir.php index ddac9642b..be5d87baf 100644 --- a/lib/Parser/MimeDir.php +++ b/lib/Parser/MimeDir.php @@ -79,6 +79,12 @@ public function parse($input = null, int $options = 0): ?Document $this->setInput($input); } + if (!\is_resource($this->input)) { + // Null was passed as input, but there was no existing input buffer + // There is nothing to parse. + throw new ParseException('No input provided to parse'); + } + if (0 !== $options) { $this->options = $options; } diff --git a/tests/VObject/Parser/MimeDirTest.php b/tests/VObject/Parser/MimeDirTest.php index 24656f886..db5735b0c 100644 --- a/tests/VObject/Parser/MimeDirTest.php +++ b/tests/VObject/Parser/MimeDirTest.php @@ -101,6 +101,25 @@ public function testDecodeUnsupportedInlineCharset(): void $mimeDir->parse($vcard); } + public function provideEmptyParserInput(): array + { + return [ + [null, 'No input provided to parse'], + ['', 'End of document reached prematurely'], + ]; + } + + /** + * @dataProvider provideEmptyParserInput + */ + public function testParseEmpty($input, $expectedExceptionMessage): void + { + $this->expectException(ParseException::class); + $this->expectExceptionMessage($expectedExceptionMessage); + $mimeDir = new MimeDir(); + $mimeDir->parse($input); + } + public function testDecodeWindows1252(): void { $vcard = <<