Skip to content

[stdlib] Fix bug in Bit with-overflow arithmetic #680

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 5 commits into from
Dec 21, 2015

Conversation

PatrickPijnappel
Copy link
Contributor

The original code would crash on negative values.

  • Added a test for this case.
  • Patch passes build-script -R -t.

} else {
return (Bit(rawValue: v.0 % 2)!, true)
let bitRaw = intResult % 2 + (intResult < 0 ? 2 : 0)
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 this might be simpler, and yet produce the same result: let bitRaw = (intResult + 2) % 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, that would rely on the precondition that intResult >= -2. This is true for all current calls to the function, but the way it's written now trivially eliminates this precondition.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK.

@gribozavr
Copy link
Contributor

Thank you! Please wrap to 80 columns.

@PatrickPijnappel
Copy link
Contributor Author

@gribozavr Wrapped to 80 columns.

gribozavr added a commit that referenced this pull request Dec 21, 2015
[stdlib] Fix bug in Bit with-overflow arithmetic
@gribozavr gribozavr merged commit c486a3a into swiftlang:master Dec 21, 2015
@PatrickPijnappel PatrickPijnappel deleted the bit-fix branch December 21, 2015 03:10
freak4pc pushed a commit to freak4pc/swift that referenced this pull request Sep 28, 2022
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.

2 participants