-
Notifications
You must be signed in to change notification settings - Fork 196
Open
Labels
enhancementNew feature or requestNew feature or request
Description
In FoundationEssentials' Data class, there's a commented use of mktemp.
swift-foundation/Sources/FoundationEssentials/Data/Data+Writing.swift
Lines 168 to 182 in 5af94ab
// The warning diligently tells us we shouldn't be using mktemp() because blindly opening the returned path opens us up to a TOCTOU race. However, in this case, we're being careful by doing O_CREAT|O_EXCL and repeating, just like the implementation of mkstemp. | |
// Furthermore, we can't compatibly switch to mkstemp() until we have the ability to set fchmod correctly, which requires the ability to query the current umask, which we don't have. (22033100) | |
#if os(Windows) | |
guard _mktemp_s(templateFileSystemRep, strlen(templateFileSystemRep) + 1) == 0 else { | |
throw CocoaError.errorWithFilePath(inPath, errno: errno, reading: false, variant: variant) | |
} | |
let fd = try String(cString: templateFileSystemRep).withNTPathRepresentation { | |
openFileDescriptorProtected(path: $0, flags: _O_BINARY | _O_CREAT | _O_EXCL | _O_RDWR, options: options) | |
} | |
#else | |
guard mktemp(templateFileSystemRep) != nil else { | |
throw CocoaError.errorWithFilePath(inPath, errno: errno, reading: false, variant: variant) | |
} | |
let fd = openFileDescriptorProtected(path: templateFileSystemRep, flags: O_CREAT | O_EXCL | O_RDWR, options: options) | |
#endif |
As the comment describes, this use is "fine" ™️. It also describes a reason that the preferred mkstemp
method cannot be used.
It seems to reference an Apple-internal bug ID, so here's a public one :)
The behavior I'm seeing in my own project is that use of -static-stdlib
forces this 'bad' API usage to show up in my own build logs when linking a shared library on Linux:
/home/andrew/.local/share/swiftly/toolchains/main-snapshot-2025-06-03/usr/lib/swift_static/linux/libFoundationEssentials.a(Data+Writing.swift.o):
_ThreadLocal.swift.o:function $s20FoundationEssentials19createTemporaryFile33_FC9EC52B075D2ACCFF86F1C9F84293BELL2at6inPath6prefix7options7variants5Int32V_SStSS_AA0Q5OrURLOSSAA4DataV14WritingOptionsVSSSgtKFAJ_SStSgSpys4Int8VGSgKXEfU_:
(.text+0xac9): warning: the use of `mktemp' is dangerous, better use `mkstemp' or `mkdtemp'
$ swiftc -version
Swift version 6.2-dev (LLVM b5d039be1fbae13, Swift 4fb4945ab972c85)
Target: x86_64-unknown-linux-gnu
Build config: +assertions
As the error suggests, this is swiftly main-snapshot-06-03.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request