Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: 5f14e32e576011c24bb941454bbd6463caf12533
      
https://github.com/WebKit/WebKit/commit/5f14e32e576011c24bb941454bbd6463caf12533
  Author: Chris Dumez <[email protected]>
  Date:   2026-09-08 (Tue, 08 Sep 2026)

  Changed paths:
    M Source/JavaScriptCore/jsc.cpp
    M Source/WTF/wtf/FileSystem.cpp
    M Source/WTF/wtf/FileSystem.h
    M Source/WTF/wtf/Forward.h
    M Source/WTF/wtf/PrintStream.cpp
    M Source/WTF/wtf/posix/FileSystemPOSIX.cpp
    M Source/WTF/wtf/text/CString.cpp
    M Source/WTF/wtf/text/CString.h
    M Source/WTF/wtf/text/StringImpl.cpp
    M Source/WTF/wtf/text/StringImpl.h
    M Source/WTF/wtf/text/StringView.cpp
    M Source/WTF/wtf/text/StringView.h
    M Source/WTF/wtf/text/TextStream.h
    M Source/WTF/wtf/text/WTFString.cpp
    M Source/WTF/wtf/text/WTFString.h
    M Source/WebCore/loader/SubresourceLoader.cpp
    M Source/WebCore/loader/cache/CachedResource.cpp
    M Source/WebCore/loader/cache/CachedResourceLoader.cpp
    M Source/WebCore/loader/cache/MemoryCache.cpp
    M Source/WebCore/page/MemoryRelease.cpp
    M Source/WebCore/platform/network/curl/CurlContext.cpp
    M Source/WebCore/platform/network/curl/CurlContext.h
    M Source/WebCore/platform/sql/SQLiteDatabase.cpp
    M Source/WebCore/rendering/adwaita/RenderThemeAdwaita.cpp
    M Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp
    M Source/WebDriver/glib/SessionHostGlib.cpp
    M Source/WebKit/NetworkProcess/cache/NetworkCache.cpp
    M Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.cpp
    M 
Source/WebKit/NetworkProcess/webtransport/cocoa/NetworkTransportSessionCocoa.mm
    M Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp
    M Source/WebKit/WebProcess/Network/WebResourceLoader.cpp
    M Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm
    M Tools/TestWebKitAPI/Helpers/GraphicsTestUtilities.cpp
    M Tools/TestWebKitAPI/Tests/WTF/CString.cpp
    M Tools/WebKitTestRunner/InjectedBundle/InjectedBundle.cpp

  Log Message:
  -----------
  Introduce CStringWithEncoding so that a CString can remember its encoding
https://bugs.webkit.org/show_bug.cgi?id=323408

Reviewed by Darin Adler.

CString is a ref-counted, null-terminated, copy-on-write char buffer, and as 
its own
header comment says, it "does not know its encoding". In practice its bytes are 
almost
always UTF-8 (String::utf8()) or Latin-1 (String::latin1()), but the 
distinction is lost
at construction: CString(std::span<const char8_t>) and CString(std::span<const
Latin1Character>) both byteCast into the same char storage.

That is not just a documentation problem. StringConcatenate.h auto-adapts any 
class with
a span() member into a StringTypeAdapter, keyed on the span's element type: a
span<const char8_t> is decoded as real UTF-8 via Unicode::checkUTF8, while any 
other
one-byte type is reinterpreted as Latin-1. Because CString::span() returns
span<const char>, makeString(string.utf8()) silently mojibakes every non-ASCII 
string.

This adds CStringWithEncoding<CharacterType>, which carries the encoding in the 
type, and
three aliases following the existing WTF convention that the byte type is the 
encoding:

    using UTF8CString = CStringWithEncoding<char8_t>;
    using Latin1CString = CStringWithEncoding<Latin1Character>;
    using ASCIICString = CStringWithEncoding<char>;

char spells ASCII here to match ASCIILiteral, whose characters() returns const 
char* and
whose span() returns span<const char>.

CStringWithEncoding derives publicly from CString, so that adoption can be 
incremental:
a typed string decays implicitly to const CString&, which keeps safePrintfType,
printInternal, TextStream, the IPC and persistence coders, the generated 
CString log
signatures in LogMessages.in, and every existing const CString& parameter 
working
unchanged as producers are migrated one at a time. Slicing erases the encoding 
back to
"unknown", which is a widening to CString's documented semantics rather than a 
loss of
correctness. The derived class is final, adds no members and has no vtable, so 
sizeof is
unchanged. It is built entirely on CString's public API; the only change to 
CString
itself is dropping the final specifier.

data(), span() and mutableSpan() are hidden with tightly typed versions so the 
encoding
survives into the pointer and span types. characters() is the escape hatch that 
returns
const char* for external C functions and printf-style formatting.

Comparing Latin-1 bytes against UTF-8 bytes is meaningless, so those 
combinations are
deleted. The deleted overloads are exact matches and therefore beat the CString
comparison, which would need a derived-to-base conversion. ASCII is a subset of 
both, so
those combinations deliberately fall through to the byte comparison on CString.
Comparison against a plain CString stays available: CString means "unknown 
encoding", so
it is the deliberate escape hatch.

The following conversions are migrated in this patch:

    String::ascii()                     -> ASCIICString
    String::latin1()                    -> Latin1CString
    String::tryGetUTF8()                -> std::expected<UTF8CString, ...>
    StringView::tryGetUTF8()            -> std::expected<UTF8CString, ...>
    StringImpl::tryGetUTF8()            -> std::expected<UTF8CString, ...>
    StringImpl::utf8ForCharacters()     -> std::expected<UTF8CString, ...>
    convertToASCIILowercase/Uppercase() -> UTF8CString

utf8() on String, StringView and StringImpl still returns CString and is left 
with a
FIXME; retyping it means touching roughly 2900 call sites, most of which pass
utf8().data() to a %s and would need characters() instead. FIXMEs are also left 
on
CString's span<const char8_t> and span<const Latin1Character> constructors, 
which are the
exact ambiguity this type exists to remove, and on TextStream::operator<<(const 
CString&),
which appends bytes as Latin-1 and so will mis-render a sliced UTF8CString.

This patch is types only; there is no behavior change. Nothing in the tree 
feeds a
tryGetUTF8() result into makeString or StringBuilder::append, which is the only 
place the
new char8_t span typing would switch Latin-1 reinterpretation to real UTF-8 
decoding.

Also update to call `URL::string().utf8()` instead of `URL::string().latin1()` 
for logging.
URLs are only guaranteed to be ASCII when they are valid. When parsing fails, 
URL::string()
may return an invalid URL string which may not be ASCII (or even latin1).

Test: Tools/TestWebKitAPI/Tests/WTF/CString.cpp

* Source/JavaScriptCore/jsc.cpp:
(fillBufferWithContentsOfFile):
(toCString):
(dumpException):
(fetchModuleFromLocalFileSystem):
* Source/WTF/wtf/Forward.h:
* Source/WTF/wtf/PrintStream.cpp:
(WTF::printExpectedCStringHelper):
* Source/WTF/wtf/text/CString.cpp:
(WTF::convertASCIICase):
(WTF::convertToASCIILowercase):
(WTF::convertToASCIIUppercase):
* Source/WTF/wtf/text/CString.h:
(WTF::operator==):
(WTF::operator<):
(WTF::CStringWithEncodingHash::hash):
(WTF::CStringWithEncodingHash::equal):
* Source/WTF/wtf/text/StringImpl.cpp:
(WTF::StringImpl::utf8ForCharacters):
(WTF::StringImpl::tryGetUTF8 const):
* Source/WTF/wtf/text/StringImpl.h:
* Source/WTF/wtf/text/StringView.cpp:
(WTF::StringView::tryGetUTF8 const):
* Source/WTF/wtf/text/StringView.h:
* Source/WTF/wtf/text/TextStream.h:
* Source/WTF/wtf/text/WTFString.cpp:
(WTF::String::ascii const):
(WTF::String::latin1 const):
(WTF::String::tryGetUTF8 const):
(WTF::String::utf8 const):
* Source/WTF/wtf/text/WTFString.h:
* Source/WebCore/loader/SubresourceLoader.cpp:
(WebCore::SubresourceLoader::didFinishLoading):
(WebCore::SubresourceLoader::didFail):
(WebCore::SubresourceLoader::willCancel):
* Source/WebCore/loader/cache/CachedResource.cpp:
(WebCore::CachedResource::failBeforeStarting):
* Source/WebCore/loader/cache/CachedResourceLoader.cpp:
(WebCore::CachedResourceLoader::requestResource):
(WebCore::CachedResourceLoader::loadResource):
* Source/WebCore/loader/cache/MemoryCache.cpp:
(WebCore::MemoryCache::add):
(WebCore::MemoryCache::remove):
* Source/WebCore/page/MemoryRelease.cpp:
(WebCore::logMemoryStatistics):
* Source/WebCore/platform/network/curl/CurlContext.cpp:
(WebCore::CurlHandle::setURL):
* Source/WebCore/platform/network/curl/CurlContext.h:
(WebCore::CurlSList::append):
* Source/WebCore/platform/sql/SQLiteDatabase.cpp:
(WebCore::SQLiteDatabase::open):
* Source/WebCore/rendering/adwaita/RenderThemeAdwaita.cpp:
(WebCore::RenderThemeAdwaita::mediaControlsImageDataForIconNameAndType):
(WebCore::RenderThemeAdwaita::mediaControlsBase64StringForIconNameAndType):
* Source/WebCore/xml/parser/XMLDocumentParserLibxml2.cpp:
(WebCore::xmlDocPtrForString):
* Source/WebDriver/glib/SessionHostGlib.cpp:
(WebDriver::SessionHost::launchBrowser):
* Source/WebKit/NetworkProcess/cache/NetworkCache.cpp:
(WebKit::NetworkCache::Cache::store):
(WebKit::NetworkCache::Cache::storeRedirect):
(WebKit::NetworkCache::Cache::update):
* 
Source/WebKit/NetworkProcess/webtransport/cocoa/NetworkTransportSessionCocoa.mm:
(WebKit::createParameters):
* Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp:
(WebKit::WebLoaderStrategy::scheduleLoadFromNetworkProcess):
* Source/WebKit/WebProcess/Network/WebResourceLoader.cpp:
(WebKit::WebResourceLoader::willSendRequest):
(WebKit::WebResourceLoader::didReceiveResponse):
(WebKit::WebResourceLoader::didReceiveData):
(WebKit::WebResourceLoader::didFinishResourceLoad):
(WebKit::WebResourceLoader::didFailResourceLoad):
(WebKit::WebResourceLoader::didBlockAuthenticationChallenge):
(WebKit::WebResourceLoader::stopLoadingAfterXFrameOptionsOrContentSecurityPolicyDenied):
(WebKit::WebResourceLoader::didReceiveResource):
* Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm:
(WebKit::WebProcess::accessibilityFocusedUIElement):
* Tools/TestWebKitAPI/Helpers/GraphicsTestUtilities.cpp:
(TestWebKitAPI::imageBufferPixelIs):
* Tools/TestWebKitAPI/Tests/WTF/CString.cpp:
(requires):
(TEST(WTF, CStringWithEncodingConstruction)):
(TEST(WTF, CStringWithEncodingNewUninitialized)):
(TEST(WTF, CStringWithEncodingComparison)):
(TEST(WTF, CStringWithEncodingHashing)):
(TEST(WTF, CStringWithEncodingMakeString)):
* Tools/WebKitTestRunner/InjectedBundle/InjectedBundle.cpp:
(WTR::InjectedBundle::outputText):
* Source/WTF/wtf/FileSystem.h:
(WTF::FileSystemImpl::statFile):
* Source/WTF/wtf/posix/FileSystemPOSIX.cpp:
(WTF::FileSystemImpl::statFile):
(WTF::FileSystemImpl::fileCreationTime):
(WTF::FileSystemImpl::getFileDeviceId):
* Source/WebKit/NetworkProcess/cache/NetworkCacheFileSystem.cpp:
(WebKit::NetworkCache::fileTimes):

Canonical link: https://commits.webkit.org/320652@main



To unsubscribe from these emails, change your notification settings at 
https://github.com/WebKit/WebKit/settings/notifications

Reply via email to