-
Notifications
You must be signed in to change notification settings - Fork 201
Closed
Description
Reading the following code, I noticed a race condition that causes us to return nil
for the currentDirectoryPath
even though there’s always a current directory path on Windows.
swift-foundation/Sources/FoundationEssentials/FileManager/FileManager+Directories.swift
Lines 467 to 476 in a0147ac
var currentDirectoryPath: String? { | |
#if os(Windows) | |
let dwLength: DWORD = GetCurrentDirectoryW(0, nil) | |
return withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) { | |
if GetCurrentDirectoryW(dwLength, $0.baseAddress) == dwLength - 1 { | |
return String(decodingCString: $0.baseAddress!, as: UTF16.self) | |
} | |
return nil | |
} | |
#else |
Race:
- We execute
let dwLength: DWORD = GetCurrentDirectoryW(0, nil)
and allocate a buffer of the lengthdwLength
- The current directory path gets changed to be longer
- The temporary allocation is not sufficient to fit the new (longer) directory path
- The second
GetCurrentDirectoryW
call returns a non-zero value _Filemanager.currentDirectoryPath
returnsnil
even though a current directory existed.
Metadata
Metadata
Assignees
Labels
No labels