Fix invalid instantiation and possibly unsafe accesses of array in class base_uint<BITS>#10530
Conversation
There was a problem hiding this comment.
As I see it, this code can cause an out-of-bounds access only in the case of WIDTH=0, which would be pointless.
Or am I missing something?
I do agree that this change makes the code somewhat more readable.
There was a problem hiding this comment.
Yes, the out-of-bounds access occurs in the edge case of WIDTH == 0.
This template class can, although it probably won't, be instantiated with BITS < 32, something that would lead to WIDTH == 0.
I was mainly interested in ensuring the out-of-bounds-check before array access regardless of such assumptions.
Making it easier to follow the code logic is an extra gain.
There was a problem hiding this comment.
This template class can, although it probably won't, be instantiated with BITS < 32
Indeed - as the code throughout the entire class is implemented, it can only be instantiated for a positive multiple of 32. Might make sense to add a compile-time assertion for that.
There was a problem hiding this comment.
Do you think adding:
static_assert(BITS/32 > 0 && BITS%32 == 0, "Template parameter BITS must be a positive multiple of 32.");
in the class constructor would be fine?
There was a problem hiding this comment.
I have added the assertions, is there something more I can do for this PR?
The implementation of base_uint::operator++(int) and base_uint::operator--(int) is now safer. Array pn is accessed via index i after bounds checking has been performed on the index, rather than before. The logic of the while loops has also been made more clear. A compile time assertion has been added in the class constructors to ensure that BITS is a positive multiple of 32.
|
Reopened with addition of static_assert for BITS value. |
|
utACK |
…of array in class base_uint<BITS> e5c6168 Fix instantiation and array accesses in class base_uint<BITS> (Pavlos Antoniou) Tree-SHA512: e4d39510d776c5ae8814cd5fb5c5d183cd8da937e339bff95caff68a84492fbec68bf513c5a6267446a564d39093e0c7fc703c645b511caab80f7baf7955b804
…cesses of array in class base_uint<BITS> e5c6168 Fix instantiation and array accesses in class base_uint<BITS> (Pavlos Antoniou) Tree-SHA512: e4d39510d776c5ae8814cd5fb5c5d183cd8da937e339bff95caff68a84492fbec68bf513c5a6267446a564d39093e0c7fc703c645b511caab80f7baf7955b804
…of array in class base_uint<BITS> Summary: e5c6168 Fix instantiation and array accesses in class base_uint<BITS> (Pavlos Antoniou) Tree-SHA512: e4d39510d776c5ae8814cd5fb5c5d183cd8da937e339bff95caff68a84492fbec68bf513c5a6267446a564d39093e0c7fc703c645b511caab80f7baf7955b804 Backport of Core PR10530 bitcoin/bitcoin#10530 Test Plan: make check test_runner.py Reviewers: deadalnix, Fabien, jasonbcox, markblundeberg, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc Differential Revision: https://reviews.bitcoinabc.org/D3569
…of array in class base_uint<BITS> Summary: e5c6168 Fix instantiation and array accesses in class base_uint<BITS> (Pavlos Antoniou) Tree-SHA512: e4d39510d776c5ae8814cd5fb5c5d183cd8da937e339bff95caff68a84492fbec68bf513c5a6267446a564d39093e0c7fc703c645b511caab80f7baf7955b804 Backport of Core PR10530 bitcoin/bitcoin#10530 Test Plan: make check test_runner.py Reviewers: deadalnix, Fabien, jasonbcox, markblundeberg, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc Differential Revision: https://reviews.bitcoinabc.org/D3569
…of array in class base_uint<BITS> Summary: e5c6168 Fix instantiation and array accesses in class base_uint<BITS> (Pavlos Antoniou) Tree-SHA512: e4d39510d776c5ae8814cd5fb5c5d183cd8da937e339bff95caff68a84492fbec68bf513c5a6267446a564d39093e0c7fc703c645b511caab80f7baf7955b804 Backport of Core PR10530 bitcoin/bitcoin#10530 Test Plan: make check test_runner.py Reviewers: deadalnix, Fabien, jasonbcox, markblundeberg, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc Differential Revision: https://reviews.bitcoinabc.org/D3569
…of array in class base_uint<BITS> Summary: e5c6168 Fix instantiation and array accesses in class base_uint<BITS> (Pavlos Antoniou) Tree-SHA512: e4d39510d776c5ae8814cd5fb5c5d183cd8da937e339bff95caff68a84492fbec68bf513c5a6267446a564d39093e0c7fc703c645b511caab80f7baf7955b804 Backport of Core PR10530 bitcoin/bitcoin#10530 Test Plan: make check test_runner.py Reviewers: deadalnix, Fabien, jasonbcox, markblundeberg, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc Differential Revision: https://reviews.bitcoinabc.org/D3569
The implementation of base_uint::operator++(int) and base_uint::operator--(int) is now safer.
Array pn is accessed via index i after bounds checking has been performed on the index, rather than before.
The logic of the while loops has also been made more clear.