Skip to content

Commit 8b6bee9

Browse files
committed
Address safer CPP failures in platform/network/cocoa
https://bugs.webkit.org/show_bug.cgi?id=290642 Reviewed by Timothy Hatcher, Geoffrey Garen, and Darin Adler. * Source/WTF/wtf/RetainPtr.h: (WTF::lazyInitialize): * Source/WebCore/platform/network/NetworkStorageSession.h: * Source/WebCore/platform/network/cocoa/CookieCocoa.mm: (WebCore::portVectorFromList): (WebCore::portStringFromVector): (WebCore::cookieCreated): (WebCore::cookieExpiry): (WebCore::Cookie::operator NSHTTPCookie * _Nullable const): * Source/WebCore/platform/network/cocoa/CookieStorageObserver.mm: (WebCore::CookieStorageObserver::startObserving): * Source/WebCore/platform/network/cocoa/CredentialCocoa.h: (WebCore::Credential::encodingRequiresPlatformData const): * Source/WebCore/platform/network/cocoa/CredentialCocoa.mm: (WebCore::Credential::Credential): * Source/WebCore/platform/network/cocoa/NetworkLoadMetrics.mm: (WebCore::copyTimingData): * Source/WebCore/platform/network/cocoa/NetworkStorageSessionCocoa.mm: (WebCore::NetworkStorageSession::setAllCookiesToSameSiteStrict): (WebCore::policyProperties): (WebCore::NetworkStorageSession::capExpiryOfPersistentCookie): (WebCore::NetworkStorageSession::cookiesForSessionAsVector const): (WebCore::adjustScriptWrittenCookie): (WebCore::parseDOMCookie): (WebCore::NetworkStorageSession::setCookiesFromDOM const): (WebCore::NetworkStorageSession::deleteCookie const): (WebCore::NetworkStorageSession::getHostnamesWithCookies): (WebCore::NetworkStorageSession::deleteAllCookies): (WebCore::NetworkStorageSession::domCookiesForHost): * Source/WebCore/platform/network/cocoa/ProtectionSpaceCocoa.mm: (WebCore::type): (WebCore::scheme): (WebCore::ProtectionSpace::ProtectionSpace): (WebCore::ProtectionSpace::nsSpace const): * Source/WebCore/platform/network/cocoa/RangeResponseGenerator.mm: (WebCore::RangeResponseGenerator::Data::shutdownResource): (WebCore::RangeResponseGenerator::removeTask): (WebCore::RangeResponseGenerator::willSynthesizeRangeResponses): * Source/WebCore/platform/network/cocoa/ResourceRequestCocoa.mm: (WebCore::ResourceRequest::doUpdateResourceRequest): (WebCore::siteForCookies): (WebCore::ResourceRequest::doUpdatePlatformRequest): (WebCore::ResourceRequest::doUpdatePlatformHTTPBody): * Source/WebCore/platform/network/cocoa/ResourceResponseCocoa.mm: (WebCore::ResourceResponse::initNSURLResponse const): (WebCore::ResourceResponse::platformCertificateInfo const): * Source/WebCore/platform/network/cocoa/WebCoreNSURLSession.mm: (-[WebCoreNSURLSession finishTasksAndInvalidate]): (-[WebCoreNSURLSessionDataTask initWithSession:identifier:request:targetDispatcher:]): (-[WebCoreNSURLSessionDataTask resource:receivedResponse:completionHandler:]): (-[WebCoreNSURLSessionDataTask resource:receivedData:]): (-[WebCoreNSURLSessionDataTask resource:receivedRedirect:request:completionHandler:]): (-[WebCoreNSURLSessionDataTask _resource:loadFinishedWithError:metrics:]): * Source/WebKit/NetworkProcess/cocoa/NetworkTaskCocoa.mm: (WebKit::cookiesByCappingExpiry): Canonical link: https://commits.webkit.org/292902@main
1 parent 6fcde9f commit 8b6bee9

54 files changed

Lines changed: 233 additions & 225 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Source/JavaScriptCore/API/JSAPIGlobalObject.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@
264264
if ([moduleLoaderDelegate respondsToSelector:@selector(willEvaluateModule:)] || [moduleLoaderDelegate respondsToSelector:@selector(didEvaluateModule:)]) {
265265
String moduleKey = key.toWTFString(globalObject);
266266
RETURN_IF_EXCEPTION(scope, { });
267-
url = [NSURL URLWithString:static_cast<NSString *>(moduleKey)];
267+
url = [NSURL URLWithString:moduleKey.createNSString().get()];
268268
}
269269

270270
if ([moduleLoaderDelegate respondsToSelector:@selector(willEvaluateModule:)])

Source/JavaScriptCore/API/JSScript.mm

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,33 +77,33 @@ static bool validateBytecodeCachePath(NSURL* cachePath, NSError** error)
7777

7878
URL cachePathURL([cachePath absoluteURL]);
7979
if (!cachePathURL.protocolIsFile()) {
80-
createError([NSString stringWithFormat:@"Cache path `%@` is not a local file", static_cast<NSURL *>(cachePathURL)], error);
80+
createError([NSString stringWithFormat:@"Cache path `%@` is not a local file", cachePathURL.createNSURL().get()], error);
8181
return false;
8282
}
8383

8484
String systemPath = cachePathURL.fileSystemPath();
8585

8686
if (auto fileType = FileSystem::fileType(systemPath)) {
8787
if (*fileType != FileSystem::FileType::Regular) {
88-
createError([NSString stringWithFormat:@"Cache path `%@` already exists and is not a file", static_cast<NSString *>(systemPath)], error);
88+
createError([NSString stringWithFormat:@"Cache path `%@` already exists and is not a file", systemPath.createNSString().get()], error);
8989
return false;
9090
}
9191
}
9292

9393
String directory = FileSystem::parentPath(systemPath);
9494
if (directory.isNull()) {
95-
createError([NSString stringWithFormat:@"Cache path `%@` does not contain in a valid directory", static_cast<NSString *>(systemPath)], error);
95+
createError([NSString stringWithFormat:@"Cache path `%@` does not contain in a valid directory", systemPath.createNSString().get()], error);
9696
return false;
9797
}
9898

9999
if (FileSystem::fileType(directory) != FileSystem::FileType::Directory) {
100-
createError([NSString stringWithFormat:@"Cache directory `%@` is not a directory or does not exist", static_cast<NSString *>(directory)], error);
100+
createError([NSString stringWithFormat:@"Cache directory `%@` is not a directory or does not exist", directory.createNSString().get()], error);
101101
return false;
102102
}
103103

104104
#if USE(APPLE_INTERNAL_SDK)
105105
if (rootless_check_datavault_flag(FileSystem::fileSystemRepresentation(directory).data(), nullptr)) {
106-
createError([NSString stringWithFormat:@"Cache directory `%@` is not a data vault", static_cast<NSString *>(directory)], error);
106+
createError([NSString stringWithFormat:@"Cache directory `%@` is not a data vault", directory.createNSString().get()], error);
107107
return false;
108108
}
109109
#endif
@@ -133,15 +133,15 @@ + (instancetype)scriptOfType:(JSScriptType)type memoryMappedFromASCIIFile:(NSURL
133133

134134
URL filePathURL([filePath absoluteURL]);
135135
if (!filePathURL.protocolIsFile())
136-
return createError([NSString stringWithFormat:@"File path %@ is not a local file", static_cast<NSURL *>(filePathURL)], error);
136+
return createError([NSString stringWithFormat:@"File path %@ is not a local file", filePathURL.createNSURL().get()], error);
137137

138138
String systemPath = filePathURL.fileSystemPath();
139139
auto fileData = FileSystem::mapFile(systemPath, FileSystem::MappedFileMode::Shared);
140140
if (!fileData)
141-
return createError([NSString stringWithFormat:@"File at path %@ could not be mapped.", static_cast<NSString *>(systemPath)], error);
141+
return createError([NSString stringWithFormat:@"File at path %@ could not be mapped.", systemPath.createNSString().get()], error);
142142

143143
if (!charactersAreAllASCII(fileData->span()))
144-
return createError([NSString stringWithFormat:@"Not all characters in file at %@ are ASCII.", static_cast<NSString *>(systemPath)], error);
144+
return createError([NSString stringWithFormat:@"Not all characters in file at %@ are ASCII.", systemPath.createNSString().get()], error);
145145

146146
auto result = adoptNS([[JSScript alloc] init]);
147147
result->m_virtualMachine = vm;

Source/JavaScriptCore/inspector/remote/cocoa/RemoteInspectorCocoa.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static bool canAccessWebInspectorMachPort()
243243
if (!targetConnection)
244244
return;
245245

246-
NSData *messageData = [static_cast<NSString *>(message) dataUsingEncoding:NSUTF8StringEncoding];
246+
NSData *messageData = [message.createNSString() dataUsingEncoding:NSUTF8StringEncoding];
247247
NSUInteger messageLength = messageData.length;
248248
const NSUInteger maxChunkSize = 2 * 1024 * 1024; // 2 Mebibytes
249249

Source/WTF/wtf/RetainPtr.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,18 @@ inline CFHashCode safeCFHash(CFTypeRef a)
368368
return a ? CFHash(a) : 0;
369369
}
370370

371+
template<typename T, typename U>
372+
ALWAYS_INLINE void lazyInitialize(const RetainPtr<T>& ptr, RetainPtr<U>&& obj)
373+
{
374+
RELEASE_ASSERT(!ptr);
375+
const_cast<RetainPtr<T>&>(ptr) = std::move(obj);
376+
}
377+
371378
} // namespace WTF
372379

373380
using WTF::RetainPtr;
374381
using WTF::adoptCF;
382+
using WTF::lazyInitialize;
375383
using WTF::retainPtr;
376384
using WTF::safeCFEqual;
377385
using WTF::safeCFHash;

Source/WTF/wtf/URL.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ class URL {
235235
#if USE(FOUNDATION)
236236
WTF_EXPORT_PRIVATE URL(NSURL *);
237237
WTF_EXPORT_PRIVATE operator NSURL *() const;
238+
WTF_EXPORT_PRIVATE RetainPtr<NSURL> createNSURL() const;
239+
WTF_EXPORT_PRIVATE static NSURL *emptyNSURL();
238240
#endif
239241

240242
#if USE(GLIB)
@@ -265,7 +267,7 @@ class URL {
265267
friend WTF_EXPORT_PRIVATE bool protocolHostAndPortAreEqual(const URL&, const URL&);
266268

267269
#if USE(CF)
268-
static RetainPtr<CFURLRef> emptyCFURL();
270+
static CFURLRef emptyCFURL();
269271
#endif
270272

271273
String m_string;

Source/WTF/wtf/cocoa/LanguageCocoa.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
size_t indexOfBestMatchingLanguageInList(const String& language, const Vector<String>& languageList, bool& exactMatch)
4141
{
42-
auto matchedLanguages = retainPtr([NSLocale matchedLanguagesFromAvailableLanguages:createNSArray(languageList).get() forPreferredLanguages:@[ static_cast<NSString *>(language) ]]);
42+
auto matchedLanguages = retainPtr([NSLocale matchedLanguagesFromAvailableLanguages:createNSArray(languageList).get() forPreferredLanguages:@[ language.createNSString().get() ]]);
4343
if (![matchedLanguages count]) {
4444
exactMatch = false;
4545
return notFound;

Source/WTF/wtf/cocoa/URLCocoa.mm

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,22 @@ - (BOOL)_web_looksLikeIPAddress;
5050
return createCFURL().bridgingAutorelease();
5151
}
5252

53-
RetainPtr<CFURLRef> URL::emptyCFURL()
53+
RetainPtr<NSURL> URL::createNSURL() const
54+
{
55+
return bridge_cast(createCFURL());
56+
}
57+
58+
CFURLRef URL::emptyCFURL()
5459
{
5560
// We use the toll-free bridge to create an empty value that is distinct from null that no CFURL function can create.
5661
// FIXME: When we originally wrote this, we thought that creating empty CF URLs was valuable; can we do without it now?
57-
return bridge_cast(adoptNS([[NSURL alloc] initWithString:@""]));
62+
return bridge_cast(emptyNSURL());
63+
}
64+
65+
NSURL *URL::emptyNSURL()
66+
{
67+
static const NeverDestroyed<RetainPtr<NSURL>> emptyURL = adoptNS([[NSURL alloc] initWithString:@""]);
68+
return emptyURL.get().get();
5869
}
5970

6071
bool URL::hostIsIPAddress(StringView host)

Source/WTF/wtf/text/StringImpl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ class StringImpl : private StringImplShape {
511511

512512
#ifdef __OBJC__
513513
WTF_EXPORT_PRIVATE operator NSString *();
514+
WTF_EXPORT_PRIVATE RetainPtr<NSString> createNSString();
514515
#endif
515516

516517
#if STRING_STATS

Source/WTF/wtf/text/WTFString.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ class String final {
249249
// Given Cocoa idioms, this is a more useful default. Clients that need to preserve the
250250
// null string can check isNull explicitly.
251251
operator NSString *() const;
252-
WTF_EXPORT_PRIVATE RetainPtr<NSString> protectedNSString() const;
252+
WTF_EXPORT_PRIVATE RetainPtr<NSString> createNSString() const;
253253
#endif
254254

255255
#if OS(WINDOWS)

Source/WTF/wtf/text/cocoa/StringCocoa.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626

2727
namespace WTF {
2828

29-
RetainPtr<NSString> String::protectedNSString() const
29+
RetainPtr<NSString> String::createNSString() const
3030
{
31-
if (!m_impl)
32-
return @"";
33-
SUPPRESS_UNCOUNTED_ARG return static_cast<NSString *>(*m_impl);
31+
if (RefPtr impl = m_impl)
32+
return impl->createNSString();
33+
return @"";
3434
}
3535

3636
RetainPtr<id> makeNSArrayElement(const String& vectorElement)

0 commit comments

Comments
 (0)