Skip to content

Race condition in _FileManagerImpl.currentDirectoryPath #1034

@ahoppen

Description

@ahoppen

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.

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 length dwLength
  • 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 returns nil even though a current directory existed.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions