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

mempool: Fix unintended unsigned integer wraparound in CTxMemPool::UpdateAncestorsOf(bool add, …) when add is false #14226

Conversation

practicalswift
Copy link
Contributor

@practicalswift practicalswift commented Sep 15, 2018

Fix unintended unsigned integer wraparound in CTxMemPool::UpdateAncestorsOf(bool add, …) when add is false.

txmempool.cpp:219:44: runtime error: unsigned integer overflow: 18446744073709551615 * 155 cannot be represented in type 'unsigned long' !=

@practicalswift practicalswift changed the title mempool: Fix unintended unsigned integer wraparound in CTxMemPool::UpdateAncestorsOf(…) when add=true mempool: Fix unintended unsigned integer wraparound in CTxMemPool::UpdateAncestorsOf(bool add, …) when add is true Sep 15, 2018
@practicalswift practicalswift changed the title mempool: Fix unintended unsigned integer wraparound in CTxMemPool::UpdateAncestorsOf(bool add, …) when add is true mempool: Fix unintended unsigned integer wraparound in CTxMemPool::UpdateAncestorsOf(bool add, …) when add is false Sep 15, 2018
@sdaftuar
Copy link
Member

Why did you decide to close this PR? After some digging I believe this change correctly fixes a bug, though I think the problem is not in the unsigned integer overflow (which is well-defined), but in casting the resulting unsigned integer to a signed type (which I have recently learned is not defined behavior, when the unsigned value would be negative).

So I think this change, which casts to int64_t first before multiplying by -1, is exactly correct.

@practicalswift
Copy link
Contributor Author

practicalswift commented Sep 24, 2018

@sdaftuar I received some feedback about my PR:s creating too much review work – that's the background to the close. But I'm happy to re-open :-)

@maflcko
Copy link
Member

maflcko commented Sep 24, 2018

I haven't reviewed the claim that @sdaftuar makes, but I checked that on my machine the bitcoind compiled with clang doesn't change at all, and for gcc the objdump is the same.

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 24, 2018

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #13804 (WIP: Transaction Pool Layer by MarcoFalke)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@sdaftuar
Copy link
Member

utACK

@ryanofsky pointed me to this: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf, from section 4.7.3

If the destination type is signed, the value is unchanged if it can be represented in the destination type (and bit-field width); otherwise, the value is implementation-defined.

@practicalswift
Copy link
Contributor Author

@sdaftuar @ryanofsky You might want to take a look at #11551 (Fix unsigned integer wrap-around in GetBlockProofEquivalentTime) and #11535 :-)

@DrahtBot
Copy link
Contributor

Coverage Change (pull 14226) Reference (master)
Lines -0.0552 % 87.0361 %
Functions -0.0309 % 84.1130 %
Branches -0.0464 % 51.5451 %

@practicalswift practicalswift force-pushed the fix-integer-wraparound-in-calculation-of-updateSize-in-UpdateAncestorsOf branch 2 times, most recently from a6de9e3 to 8b1b039 Compare November 7, 2018 08:59
@practicalswift practicalswift deleted the fix-integer-wraparound-in-calculation-of-updateSize-in-UpdateAncestorsOf branch April 10, 2021 19:36
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants