Skip to content

[stdlib] Dictionary.merge: Don’t leave storage in an inconsistent state when closure throws #19638

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

Merged
merged 1 commit into from
Oct 1, 2018

Conversation

lorentey
Copy link
Member

@lorentey lorentey commented Oct 1, 2018

The new merge implementation leaves behind an uninitialized value when the uniquing closure throws.

@lorentey
Copy link
Member Author

lorentey commented Oct 1, 2018

@swift-ci please test and merge

@swift-ci swift-ci merged commit f2048ce into swiftlang:master Oct 1, 2018
@@ -613,9 +613,8 @@ extension _NativeDictionary { // High-level operations
isUnique = true
if found {
do {
let v = (_values + bucket.offset).move()
let newValue = try combine(v, value)
(_values + bucket.offset).initialize(to: newValue)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can optimize the happy path by moving back in the failure case:

let v = (_values + bucket.offset).move()
do {
  let newValue = try combine(v, value)
  (_values + bucket.offset).initialize(to: newValue)
} catch _MergeError.keyCollision {
  fatalError("Duplicate values for key: '\(key)'")
} catch {
  // Put the value back.
  (_values + bucket.offset).initialize(to: v)
  throw error
}

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 this pull request may close these issues.

3 participants