From 48bc421e8154d53c67aa2e60c250da8fa668ad4e Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Mon, 27 Jul 2026 11:23:19 +0200 Subject: [PATCH] Treat NUL bytes as empty in `Str::isEmpty()` NUL bytes are invisible but fell outside the accepted empty character set. This also caused combinations of NUL bytes and whitespace to be considered nonempty. --- src/Str.php | 6 +++--- tests/StrTest.php | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Str.php b/src/Str.php index c7ab22b..2e23fd8 100644 --- a/src/Str.php +++ b/src/Str.php @@ -96,8 +96,8 @@ public static function trimSplit(?string $subject, string $delimiter = ',', ?int /** * Check if the given string is empty * - * Null is considered empty, and strings consisting only of whitespace or visually - * empty characters are considered empty. + * Null is considered empty, and strings consisting only of whitespace, NUL bytes, + * or visually empty characters are considered empty. * * @param string|Stringable|null $subject * @@ -105,6 +105,6 @@ public static function trimSplit(?string $subject, string $delimiter = ',', ?int */ public static function isEmpty(string|Stringable|null $subject): bool { - return $subject === null || preg_match('/^[\s\x{3164}\x{1160}]*$/u', $subject) === 1; + return $subject === null || preg_match('/^[\s\x00\x{3164}\x{1160}]*$/u', $subject) === 1; } } diff --git a/tests/StrTest.php b/tests/StrTest.php index bcff7db..8347d12 100644 --- a/tests/StrTest.php +++ b/tests/StrTest.php @@ -67,6 +67,16 @@ public function testIsEmptyReturnsTrueForStringWithOnlyWhitespace() $this->assertTrue(Str::isEmpty("\t\n")); } + public function testIsEmptyReturnsTrueForNulByte() + { + $this->assertTrue(Str::isEmpty("\0")); + } + + public function testIsEmptyReturnsTrueForNulByteCombinedWithWhitespace() + { + $this->assertTrue(Str::isEmpty("\0 ")); + } + public function testIsEmptyReturnsFalseForZero() { $this->assertFalse(Str::isEmpty('0'));