diff --git a/src/Connection/ImapTokenizer.php b/src/Connection/ImapTokenizer.php index caa83eaa..e802764d 100644 --- a/src/Connection/ImapTokenizer.php +++ b/src/Connection/ImapTokenizer.php @@ -52,9 +52,7 @@ public function nextToken(): ?Token $this->skipWhitespace(); // Retry if whitespace scanning stopped after an empty read. - if (! isset($this->buffer[$this->position])) { - $this->ensureBuffer(1); - } + $this->ensureBuffer(1); $char = $this->currentChar(); @@ -142,9 +140,7 @@ public function nextToken(): ?Token protected function skipWhitespace(): void { while (true) { - if (! isset($this->buffer[$this->position])) { - $this->ensureBuffer(1); - } + $this->ensureBuffer(1); $length = strspn($this->buffer, " \t", $this->position); @@ -430,7 +426,11 @@ protected function readEmailAddress(): ?EmailAddress */ protected function ensureBuffer(int $length): void { - // If we have enough data in the buffer, return early. + if (isset($this->buffer[$this->position + $length - 1])) { + return; + } + + // Read until the requested number of bytes is available. while ((strlen($this->buffer) - $this->position) < $length) { $data = $this->stream->fgets(); diff --git a/tests/Unit/Connection/ImapTokenizerTest.php b/tests/Unit/Connection/ImapTokenizerTest.php index d5f69bd3..c45eee7f 100644 --- a/tests/Unit/Connection/ImapTokenizerTest.php +++ b/tests/Unit/Connection/ImapTokenizerTest.php @@ -658,3 +658,48 @@ public function fgets(): string|false expect($tokenizer->nextToken())->toEqual(new Atom('ATOM')); expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); }); + +test('tokenizer reads buffered values followed by another token', function (string $input, Token $expected, int $chunkSize) { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(str_split($input.' NEXT', $chunkSize)); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toEqual($expected); + expect($tokenizer->nextToken())->toEqual(new Atom('NEXT')); + expect($tokenizer->nextToken())->toBeNull(); +})->with([ + ['"hello \\"world\\""', new QuotedString('hello "world"')], + ['', new EmailAddress('user@example.com')], + ['example', new Atom('example')], + ["{5}\r\nhello", new Literal('hello')], +])->with(range(1, 24)); + +test('tokenizer preserves reader errors after an empty stream read', function (array $chunks, string $message) { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw($chunks); + + $tokenizer = new ImapTokenizer($stream); + + expect(fn () => $tokenizer->nextToken()) + ->toThrow(ImapParserException::class, $message); +})->with([ + [['"hello', '', '"'], 'Unterminated quoted string at buffer offset 0. Buffer: ""'], + [['"hello\\', '', '"'], 'Unterminated escape sequence in quoted string'], + [[''], 'Unterminated email address, expected ">"'], + [['{12', '', "}\r\n"], 'Unterminated literal specifier'], +]); + +test('tokenizer finishes an atom at an empty stream read', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(['hello', '', 'world ']); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toEqual(new Atom('hello')); + expect($tokenizer->nextToken())->toEqual(new Atom('world')); + expect($tokenizer->nextToken())->toBeNull(); +});