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

Unsafe Handling of DateFormatter #273

Closed
sidepelican opened this issue Nov 13, 2023 · 2 comments
Closed

Unsafe Handling of DateFormatter #273

sidepelican opened this issue Nov 13, 2023 · 2 comments

Comments

@sidepelican
Copy link
Contributor

Problem

The ISO8601DateFormatter in swift-corelibs-foundation is not thread-safe.
However, URLEncodedFormDecoder and URLEncodedFormEncoder use this class as a singleton. This can cause a runtime crash, such as a segmentation fault.

Reference to the code:

static var iso8601Formatter: ISO8601DateFormatter = {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = .withInternetDateTime
return formatter
}()

Steps to Reproduce

The following code, when run multiple times in a Linux Swift environment, sometimes causes a crash:

import Foundation

let iso8601Formatter: ISO8601DateFormatter = {
  let formatter = ISO8601DateFormatter()
  formatter.formatOptions = .withInternetDateTime
  return formatter
}()

let now = Date()
for i in 0..<1000 {
  DispatchQueue.global().async {
    let date = now.addingTimeInterval(Double(i) * 60 * 60 * 24)
    print(iso8601Formatter.string(from: date))
  }
}

sleep(1)

Tested Environment

  • Swift 5.9 (swift-5.9-RELEASE)
  • Target: x86_64-unknown-linux-gnu
@adam-fowler
Copy link
Member

I couldn't get it to crash, but I've still got rid of the global instance.
Fixed in #274
I thought this had been fixed in corelibs foundation, but looking through issues it doesn't look like it has. I doubt it will now and we'll have to wait for the new Foundation

@adam-fowler
Copy link
Member

Fix in v1.9.1

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

No branches or pull requests

2 participants