NSURLCache bug: Can't create a disk cache in a directory that has a space in it.

Number:rdar://FB9117600 Date Originated:25/5/2021
Status:Open Resolved:No
Product:CFNetwork.framework Product Version:1237
Classification:Bug Reproducible:Always
I believe I have discovered a bug in CFNetwork.framework version 1237 NSURLCache. I noticed that if the URL I supply to init(memoryCapacity:diskCapacity:directory:) contains a space then the cache fails because it is attempting to create the SQLite database to hold the cache at a path with %20 in it which is invalid, see debug output at the bottom.

I used Hopper to investigate this deeper (screenshot attached) and I believe there is a bug that this method is using:
Which results in the %20 instead of:
CFURLCopyFileSystemPath(dir, kCFURLPOSIXPathStyle);
Which returns the path correctly with spaces.

Here is a Stackoverflow post where a developer was facing the same problem of seeing %20 in paths:

I can work around this problem by using the old init that takes the path as a string however that is deprecated now so would prefer for this URL based init to be fixed, thanks.

2021-05-25 09:20:24.069335+0100 URLCacheTestApp[78502:8570943] [logging-persist] cannot open file at line 44514 of [02c344acea]
2021-05-25 09:20:24.069440+0100 URLCacheTestApp[78502:8570943] [logging-persist] os_unix.c:44515: (0) open(/Users/mh/Library/Group%20Containers/HUK6B7UUU2.com.malcolmhall.URLCacheTest/Library/Caches/Cache.db) - Undefined error: 0
2021-05-25 09:20:24.069526+0100 URLCacheTestApp[78502:8570943] NetworkStorageDB:_openDBReadConnections: failed to open read connection to DB @ /Users/mh/Library/Group%20Containers/HUK6B7UUU2.com.malcolmhall.URLCacheTest/Library/Caches/Cache.db.  Error=14. Cause=unable to open database file
2021-05-25 09:20:24.069585+0100 URLCacheTestApp[78502:8570943] The read-connection to the DB=/Users/mh/Library/Group%20Containers/HUK6B7UUU2.com.malcolmhall.URLCacheTest/Library/Caches/Cache.db is NOT valid.  Unable to determine schema version.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!