Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race condition in _FileManagerImpl.currentDirectoryPath #1034

Closed
ahoppen opened this issue Nov 5, 2024 · 0 comments · Fixed by #1035
Closed

Race condition in _FileManagerImpl.currentDirectoryPath #1034

ahoppen opened this issue Nov 5, 2024 · 0 comments · Fixed by #1035
Assignees

Comments

@ahoppen
Copy link
Member

ahoppen commented Nov 5, 2024

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants