Skip to content

Commit baeb417

Browse files
committed
CString.isEmpty() for String.utf8() returns true when String has a single character
https://bugs.webkit.org/show_bug.cgi?id=303428 Reviewed by Darin Adler. isEmpty was accounting for a null terminator in the CStringBuffer length but it is not like that. Test: Tools/TestWebKitAPI/Tests/WTF/CString.cpp * Source/WTF/wtf/text/CString.h: * Tools/TestWebKitAPI/Tests/WTF/CString.cpp: (TEST(WTF, CStringEmptyRegularConstructor)): (TEST(WTF, CStringOnyByte)): Canonical link: https://commits.webkit.org/303840@main
1 parent b7cce06 commit baeb417

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

Source/WTF/wtf/text/CString.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class CString final {
9393
size_t length() const;
9494

9595
bool isNull() const { return !m_buffer; }
96-
bool isEmpty() const { return isNull() || m_buffer->length() <= 1; }
96+
bool isEmpty() const { return isNull() || !m_buffer->length(); }
9797
bool isSafeToSendToAnotherThread() const;
9898

9999
CStringBuffer* buffer() const LIFETIME_BOUND { return m_buffer.get(); }

Tools/TestWebKitAPI/Tests/WTF/CString.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,23 @@ TEST(WTF, CStringEmptyRegularConstructor)
8787
ASSERT_STREQ(referenceString, stringWithLength.data());
8888
}
8989

90+
TEST(WTF, CStringOneByte)
91+
{
92+
const char* referenceString = "W";
93+
94+
CString string(referenceString);
95+
ASSERT_FALSE(string.isNull());
96+
ASSERT_FALSE(string.isEmpty());
97+
ASSERT_EQ(string.length(), strlen(referenceString));
98+
ASSERT_STREQ(referenceString, string.data());
99+
100+
CString stringWithLength(std::span { referenceString, 1 });
101+
ASSERT_FALSE(stringWithLength.isNull());
102+
ASSERT_FALSE(stringWithLength.isEmpty());
103+
ASSERT_EQ(stringWithLength.length(), strlen(referenceString));
104+
ASSERT_STREQ(referenceString, stringWithLength.data());
105+
}
106+
90107
TEST(WTF, CStringUninitializedConstructor)
91108
{
92109
char* buffer;

0 commit comments

Comments
 (0)