From 2da97420a07339566dced28e4553eaae856da39e Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 16 Nov 2026 11:50:32 -0500 Subject: [PATCH 1/2] Avoid redundant tokenizer buffer checks --- src/Connection/ImapTokenizer.php | 8 +++- tests/Unit/Connection/ImapTokenizerTest.php | 46 +++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Connection/ImapTokenizer.php b/src/Connection/ImapTokenizer.php index 7d9c7b14..9d974b41 100644 --- a/src/Connection/ImapTokenizer.php +++ b/src/Connection/ImapTokenizer.php @@ -51,7 +51,9 @@ public function nextToken(): ?Token { $this->skipWhitespace(); - $this->ensureBuffer(1); + if (! isset($this->buffer[$this->position])) { + $this->ensureBuffer(1); + } $char = $this->currentChar(); @@ -139,7 +141,9 @@ public function nextToken(): ?Token protected function skipWhitespace(): void { while (true) { - $this->ensureBuffer(1); + if (! isset($this->buffer[$this->position])) { + $this->ensureBuffer(1); + } $length = strspn($this->buffer, " \t", $this->position); diff --git a/tests/Unit/Connection/ImapTokenizerTest.php b/tests/Unit/Connection/ImapTokenizerTest.php index ec46ae89..d5f69bd3 100644 --- a/tests/Unit/Connection/ImapTokenizerTest.php +++ b/tests/Unit/Connection/ImapTokenizerTest.php @@ -612,3 +612,49 @@ public function fgets(): string|false ['<>', new EmailAddress('')], ["{0}\r\n", new Literal('')], ]); + +test('tokenizer retries a temporary empty read before returning a token', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(['', "ATOM\r\n"]); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toEqual(new Atom('ATOM')); + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); +}); + +test('tokenizer retries a temporary empty read after consuming whitespace', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw([" \t", '', "ATOM\r\n"]); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toEqual(new Atom('ATOM')); + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); +}); + +test('tokenizer resumes after consecutive empty reads', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(['', '', "ATOM\r\n"]); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toBeNull(); + expect($tokenizer->nextToken())->toEqual(new Atom('ATOM')); + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); +}); + +test('tokenizer reads a fragmented carriage return and line feed', function () { + $stream = new FakeStream; + $stream->open(); + $stream->feedRaw(["\r", "\n", "ATOM\r\n"]); + + $tokenizer = new ImapTokenizer($stream); + + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); + expect($tokenizer->nextToken())->toEqual(new Atom('ATOM')); + expect($tokenizer->nextToken())->toBeInstanceOf(Crlf::class); +}); From a6ff9e73854ef456e9257a39115b8312b7fb959e Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Mon, 16 Nov 2026 11:53:35 -0500 Subject: [PATCH 2/2] Explain buffer retry after whitespace scanning --- src/Connection/ImapTokenizer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Connection/ImapTokenizer.php b/src/Connection/ImapTokenizer.php index 9d974b41..caa83eaa 100644 --- a/src/Connection/ImapTokenizer.php +++ b/src/Connection/ImapTokenizer.php @@ -51,6 +51,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); }