Skip to content

Conversation

practicalswift
Copy link
Contributor

Avoid triggering undefined behaviour in base_uint<BITS>::bits().

1 << 31 is undefined behaviour in C++11.

Given the reasonable assumption of sizeof(int) * CHAR_BIT == 32.

@fanquake fanquake requested a review from sipa October 18, 2018 09:30
@@ -176,7 +176,7 @@ unsigned int base_uint<BITS>::bits() const
for (int pos = WIDTH - 1; pos >= 0; pos--) {
if (pn[pos]) {
for (int nbits = 31; nbits > 0; nbits--) {
if (pn[pos] & 1 << nbits)
if (pn[pos] & 1U << nbits)
Copy link
Member

Choose a reason for hiding this comment

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

pn[...] is uint32_t, how is this ever undefined behavior?
I don't think C ever "promotes" uint32_t to int.

Copy link
Member

@sipa sipa Oct 18, 2018

Choose a reason for hiding this comment

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

@laanwj With parenthesis it's pn[pos] & (1 << nbits); I believe the type of (1 << nbits) is indeed int, which in case of nbits == 31 is exceeding the size of the type.

Copy link
Member

@laanwj laanwj Oct 18, 2018

Choose a reason for hiding this comment

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

so let's make the counter an 'unsigned' then?
edit: oh, never mind, it's counting down, that wouldn't really work either...

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test that fails without this fix?

Copy link
Member

Choose a reason for hiding this comment

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

no, he can't—the whole problem, the whole shitshow with C and C++ is that you can't predict what will happen with undefined behavior—it can just work with one compiler or version and commit war crimes the next one

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@promag How would one test for the presence of undefined behaviour? :-)

If you are referring to the sanitisers there are no sanitisers flagging this UB AFAIK.

@laanwj
Copy link
Member

laanwj commented Oct 18, 2018

utACK 96f6dc9

@gmaxwell
Copy link
Contributor

This should also get fixed src/primitives/transaction.h: static const uint32_t SEQUENCE_LOCKTIME_DISABLE_FLAG = (1 << 31);

utACK

@laanwj laanwj merged commit 96f6dc9 into bitcoin:master Oct 18, 2018
laanwj added a commit that referenced this pull request Oct 18, 2018
…::bits()

96f6dc9 Avoid triggering undefined behaviour in base_uint<BITS>::bits() (practicalswift)

Pull request description:

  Avoid triggering undefined behaviour in `base_uint<BITS>::bits()`.

  `1 << 31` is undefined behaviour in C++11.

  Given the reasonable assumption of `sizeof(int) * CHAR_BIT == 32`.

Tree-SHA512: 995fa38e71c8921873139ecf1b7dd54178555219af3be60d07290f379439ddd8479e3963c6f3cae8178efb61063a0f9add6cba82a5578d13888597b5bcd54f22
sipa added a commit that referenced this pull request Oct 20, 2018
…DISABLE_FLAG

bc60c61 Avoid 1 << 31 (UB) in calculation of SEQUENCE_LOCKTIME_DISABLE_FLAG (practicalswift)

Pull request description:

  Avoid `1 << 31` (UB) in calculation of `SEQUENCE_LOCKTIME_DISABLE_FLAG`.

  Context: #14510 (comment)

Tree-SHA512: bdb4a913c6a82ff1a455ba67d3351f6408ff4116574329361644b483fea96b801fdc5c5659233856b591cd3a46ec669d3b5b438553e4240d7099c560eae2e2ae
jasonbcox pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request May 31, 2019
…DISABLE_FLAG

Summary:
PR14513 backport https://github.com/bitcoin/bitcoin/pull/14513/files
bc60c615a5 Avoid 1 << 31 (UB) in calculation of SEQUENCE_LOCKTIME_DISABLE_FLAG (practicalswift)

Pull request description:

  Avoid `1 << 31` (UB) in calculation of `SEQUENCE_LOCKTIME_DISABLE_FLAG`.

  Context: bitcoin/bitcoin#14510 (comment)

Test Plan: `make check`

Reviewers: Fabien, #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D3127
jtoomim pushed a commit to jtoomim/bitcoin-abc that referenced this pull request Jun 29, 2019
…DISABLE_FLAG

Summary:
PR14513 backport https://github.com/bitcoin/bitcoin/pull/14513/files
bc60c615a5 Avoid 1 << 31 (UB) in calculation of SEQUENCE_LOCKTIME_DISABLE_FLAG (practicalswift)

Pull request description:

  Avoid `1 << 31` (UB) in calculation of `SEQUENCE_LOCKTIME_DISABLE_FLAG`.

  Context: bitcoin/bitcoin#14510 (comment)

Test Plan: `make check`

Reviewers: Fabien, #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D3127
@practicalswift practicalswift deleted the 1<<31 branch April 10, 2021 19:36
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 27, 2021
…t<BITS>::bits()

96f6dc9 Avoid triggering undefined behaviour in base_uint<BITS>::bits() (practicalswift)

Pull request description:

  Avoid triggering undefined behaviour in `base_uint<BITS>::bits()`.

  `1 << 31` is undefined behaviour in C++11.

  Given the reasonable assumption of `sizeof(int) * CHAR_BIT == 32`.

Tree-SHA512: 995fa38e71c8921873139ecf1b7dd54178555219af3be60d07290f379439ddd8479e3963c6f3cae8178efb61063a0f9add6cba82a5578d13888597b5bcd54f22
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 27, 2021
…CKTIME_DISABLE_FLAG

bc60c61 Avoid 1 << 31 (UB) in calculation of SEQUENCE_LOCKTIME_DISABLE_FLAG (practicalswift)

Pull request description:

  Avoid `1 << 31` (UB) in calculation of `SEQUENCE_LOCKTIME_DISABLE_FLAG`.

  Context: bitcoin#14510 (comment)

Tree-SHA512: bdb4a913c6a82ff1a455ba67d3351f6408ff4116574329361644b483fea96b801fdc5c5659233856b591cd3a46ec669d3b5b438553e4240d7099c560eae2e2ae
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 28, 2021
…t<BITS>::bits()

96f6dc9 Avoid triggering undefined behaviour in base_uint<BITS>::bits() (practicalswift)

Pull request description:

  Avoid triggering undefined behaviour in `base_uint<BITS>::bits()`.

  `1 << 31` is undefined behaviour in C++11.

  Given the reasonable assumption of `sizeof(int) * CHAR_BIT == 32`.

Tree-SHA512: 995fa38e71c8921873139ecf1b7dd54178555219af3be60d07290f379439ddd8479e3963c6f3cae8178efb61063a0f9add6cba82a5578d13888597b5bcd54f22
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 28, 2021
…CKTIME_DISABLE_FLAG

bc60c61 Avoid 1 << 31 (UB) in calculation of SEQUENCE_LOCKTIME_DISABLE_FLAG (practicalswift)

Pull request description:

  Avoid `1 << 31` (UB) in calculation of `SEQUENCE_LOCKTIME_DISABLE_FLAG`.

  Context: bitcoin#14510 (comment)

Tree-SHA512: bdb4a913c6a82ff1a455ba67d3351f6408ff4116574329361644b483fea96b801fdc5c5659233856b591cd3a46ec669d3b5b438553e4240d7099c560eae2e2ae
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 29, 2021
…t<BITS>::bits()

96f6dc9 Avoid triggering undefined behaviour in base_uint<BITS>::bits() (practicalswift)

Pull request description:

  Avoid triggering undefined behaviour in `base_uint<BITS>::bits()`.

  `1 << 31` is undefined behaviour in C++11.

  Given the reasonable assumption of `sizeof(int) * CHAR_BIT == 32`.

Tree-SHA512: 995fa38e71c8921873139ecf1b7dd54178555219af3be60d07290f379439ddd8479e3963c6f3cae8178efb61063a0f9add6cba82a5578d13888597b5bcd54f22
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jun 29, 2021
…CKTIME_DISABLE_FLAG

bc60c61 Avoid 1 << 31 (UB) in calculation of SEQUENCE_LOCKTIME_DISABLE_FLAG (practicalswift)

Pull request description:

  Avoid `1 << 31` (UB) in calculation of `SEQUENCE_LOCKTIME_DISABLE_FLAG`.

  Context: bitcoin#14510 (comment)

Tree-SHA512: bdb4a913c6a82ff1a455ba67d3351f6408ff4116574329361644b483fea96b801fdc5c5659233856b591cd3a46ec669d3b5b438553e4240d7099c560eae2e2ae
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
…t<BITS>::bits()

96f6dc9 Avoid triggering undefined behaviour in base_uint<BITS>::bits() (practicalswift)

Pull request description:

  Avoid triggering undefined behaviour in `base_uint<BITS>::bits()`.

  `1 << 31` is undefined behaviour in C++11.

  Given the reasonable assumption of `sizeof(int) * CHAR_BIT == 32`.

Tree-SHA512: 995fa38e71c8921873139ecf1b7dd54178555219af3be60d07290f379439ddd8479e3963c6f3cae8178efb61063a0f9add6cba82a5578d13888597b5bcd54f22
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
…CKTIME_DISABLE_FLAG

bc60c61 Avoid 1 << 31 (UB) in calculation of SEQUENCE_LOCKTIME_DISABLE_FLAG (practicalswift)

Pull request description:

  Avoid `1 << 31` (UB) in calculation of `SEQUENCE_LOCKTIME_DISABLE_FLAG`.

  Context: bitcoin#14510 (comment)

Tree-SHA512: bdb4a913c6a82ff1a455ba67d3351f6408ff4116574329361644b483fea96b801fdc5c5659233856b591cd3a46ec669d3b5b438553e4240d7099c560eae2e2ae
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
…t<BITS>::bits()

96f6dc9 Avoid triggering undefined behaviour in base_uint<BITS>::bits() (practicalswift)

Pull request description:

  Avoid triggering undefined behaviour in `base_uint<BITS>::bits()`.

  `1 << 31` is undefined behaviour in C++11.

  Given the reasonable assumption of `sizeof(int) * CHAR_BIT == 32`.

Tree-SHA512: 995fa38e71c8921873139ecf1b7dd54178555219af3be60d07290f379439ddd8479e3963c6f3cae8178efb61063a0f9add6cba82a5578d13888597b5bcd54f22
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
…CKTIME_DISABLE_FLAG

bc60c61 Avoid 1 << 31 (UB) in calculation of SEQUENCE_LOCKTIME_DISABLE_FLAG (practicalswift)

Pull request description:

  Avoid `1 << 31` (UB) in calculation of `SEQUENCE_LOCKTIME_DISABLE_FLAG`.

  Context: bitcoin#14510 (comment)

Tree-SHA512: bdb4a913c6a82ff1a455ba67d3351f6408ff4116574329361644b483fea96b801fdc5c5659233856b591cd3a46ec669d3b5b438553e4240d7099c560eae2e2ae
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
…t<BITS>::bits()

96f6dc9 Avoid triggering undefined behaviour in base_uint<BITS>::bits() (practicalswift)

Pull request description:

  Avoid triggering undefined behaviour in `base_uint<BITS>::bits()`.

  `1 << 31` is undefined behaviour in C++11.

  Given the reasonable assumption of `sizeof(int) * CHAR_BIT == 32`.

Tree-SHA512: 995fa38e71c8921873139ecf1b7dd54178555219af3be60d07290f379439ddd8479e3963c6f3cae8178efb61063a0f9add6cba82a5578d13888597b5bcd54f22
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Jul 1, 2021
…CKTIME_DISABLE_FLAG

bc60c61 Avoid 1 << 31 (UB) in calculation of SEQUENCE_LOCKTIME_DISABLE_FLAG (practicalswift)

Pull request description:

  Avoid `1 << 31` (UB) in calculation of `SEQUENCE_LOCKTIME_DISABLE_FLAG`.

  Context: bitcoin#14510 (comment)

Tree-SHA512: bdb4a913c6a82ff1a455ba67d3351f6408ff4116574329361644b483fea96b801fdc5c5659233856b591cd3a46ec669d3b5b438553e4240d7099c560eae2e2ae
gades pushed a commit to cosanta/cosanta-core that referenced this pull request Apr 23, 2022
…t<BITS>::bits()

96f6dc9 Avoid triggering undefined behaviour in base_uint<BITS>::bits() (practicalswift)

Pull request description:

  Avoid triggering undefined behaviour in `base_uint<BITS>::bits()`.

  `1 << 31` is undefined behaviour in C++11.

  Given the reasonable assumption of `sizeof(int) * CHAR_BIT == 32`.

Tree-SHA512: 995fa38e71c8921873139ecf1b7dd54178555219af3be60d07290f379439ddd8479e3963c6f3cae8178efb61063a0f9add6cba82a5578d13888597b5bcd54f22
@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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants