8000 uint256: 1 is a constant by ajtowns · Pull Request #20016 · bitcoin/bitcoin · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

uint256: 1 is a constant #20016

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 3 commits into from
Sep 28, 2020
Merged

uint256: 1 is a constant #20016

merged 3 commits into from
Sep 28, 2020

Conversation

ajtowns
Copy link
Contributor
@ajtowns ajtowns commented Sep 25, 2020

UINT256_ONE() returns a reference to a global; mark it as const to be sure someone doesn't accidently modify it.

@ajtowns
Copy link
Contributor Author
ajtowns commented Sep 25, 2020

This was added in #17261 in which it was remarked "static initialization order result[ed] in a potential uninitialized object access." I have an alternative change at https://github.com/ajtowns/bitcoin/commits/202009-uint256_1_v2 which does a static init and hence avoids the functional call, and seems to pass travis fine, so might be worth considering that? cc @achow101 @kallewoof

@achow101
Copy link
Member

I have an alternative change at https://github.com/ajtowns/bitcoin/commits/202009-uint256_1_v2 which does a static init and hence avoids the functional call, and seems to pass travis fine, so might be worth considering that?

Concept ACK

@kallewoof
Copy link
Contributor

Clear improvement.

ACK 832109a

@ajtowns
Copy link
Contributor Author
ajtowns commented Sep 26, 2020

Updated to simplfy constructors and replace UINT256_ONE() call with uint256::ONE constant

@practicalswift
Copy link
Contributor

Concept ACK

Copy link
Contributor
@kristapsk kristapsk left a comment

Choose a reason for hiding this comment

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

ACK 915c1c2

@sipa
Copy link
Member
sipa commented Sep 26, 2020

utACK 915c1c2

1 is a constant

It is in tautologies that the ordering of society is centered.

Copy link
Member
@maflcko maflcko left a comment

Choose a reason for hiding this comment

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

Not sure how this could have led to memory access violations. Though, if the wallet or the script interpreter is running after the end of the main function, something else has gone wrong and should probably be fixed.

cr ACK 915c1c2 📺

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

cr ACK 915c1c25fd6d6bdc249b71be12db0744312729da 📺
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUhQlQwAqTJdOujLC6CD+i1XI8ISvOTG76CLhzgK5wg+63FYUVILodO1T0ig7NJH
D+RHnv0fdyO3OIFGH3C4ooSkHNqIiKec7Q0V2uemtU5TZX04jM6ocsP/frsCfSmU
p13UkuNxx5QSnrSlFLxrGrmuX/BkLxdubGLTXNQHNltx5rHz2p0H03ueep2/XrXM
BeMAdrBcBvjsQ3SW5XuUND8hza6YajWpAMOUSnS7rbT/hpxi3SakjYyA8E/fMQUo
tr7F/XVLX9KTYzRFvbQLRdUs2Uy3jIP4AhuJJqa7rt/1wv3Au7N0nL96wPJlBiKO
radqu65JIp2sNDl4XtXcq1gol+O184rASiaeDZlI1kAxUKoBvDKd9XtBnDWHywyl
vvulJWWkZLHN6q0z3JJ2GON8Uyr+q6ErUrOuOyLsuRvnrmawqkc6tBaUpkCefPuQ
6kKwWybrM5xSq0/OYiOQ0vFRJY34GLy7qNmyUOIQrGpbfn9AL7c9B5p9FueqMukP
/HlO1n43
=ZuQH
-----END PGP SIGNATURE-----

Timestamp of file with hash ed0494b57cec88e0ff8da75d55c543250f502605792f0e336b3c5d14e6a470b4 -

@maflcko
Copy link
Member
maflcko commented Sep 26, 2020

(1 is not a constant in python, but we don't use that here https://kate.io/blog/2017/08/22/weird-python-integers/)

Copy link
Contributor
@promag promag left a comment

Choose a reason for hiding this comment

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

ACK 915c1c2.

Copy link
Member
@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

ACK 915c1c2, I have reviewed the code and it looks OK, I agree it can be merged.

May I suggest a further step:

--- a/src/uint256.h
+++ b/src/uint256.h
@@ -21,7 +21,7 @@ protected:
     uint8_t m_data[WIDTH];
 public:
     /* construct 0 value by default */
-    base_blob() : m_data() {}
+    constexpr base_blob() : m_data() {}
 
     /* constructor for constants between 1 and 255 */
     explicit constexpr base_blob(uint8_t v) : m_data{v} {}
@@ -112,7 +112,7 @@ public:
  */
 class uint160 : public base_blob<160> {
 public:
-    uint160() {}
+    constexpr uint160() {}
     explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {}
 };
 
@@ -123,7 +123,7 @@ public:
  */
 class uint256 : public base_blob<256> {
 public:
-    uint256() {}
+    constexpr uint256() {}
     constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
     explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
     static const uint256 ONE;

?

Replace the memset with C++11 value/aggregate initialisation of
the m_data array, which still ensures the unspecified values end
up as zero-initialised.

This then allows changing UINT256_ONE() from dynamically allocating an
object, to a simpler referencing a static allocation.
-BEGIN VERIFY SCRIPT-
sed -i '/inline.* UINT256_ONE() {/,+1d' src/uint256.h
sed -i 's/UINT256_ONE()/uint256::ONE/' $(git grep -l UINT256_ONE)
-END VERIFY SCRIPT-
Copy link
Contributor
@kallewoof kallewoof left a comment

Choose a reason for hiding this comment

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

ACK 4cc7171

@promag
Copy link
Contributor
promag commented Sep 28, 2020

ACK 4cc7171

@maflcko
Copy link
Member
maflcko commented Sep 28, 2020

re ACK 4cc7171, only change is some constexpr shuffling 🛁

Show signature and timestamp

Signature:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

re ACK 4cc7171c98, only change is some constexpr shuffling 🛁
-----BEGIN PGP SIGNATURE-----

iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUhxqwwAxiSleGr6i0+dxWiXOWdI69VYKTpCJZx1eqemwQOuNA+YguYEgdIvm+78
2uFRqv3WgLDF7RvJuGVoX8x3BYMaNML0SsADXDNi0A/1FCHMdH1Mh9R+kJLFrYyK
+EsRdPGYnt4RNstY/lKkMiN6zKwEA889hcUFJbGHdnjO9+LsnfO2oCV2HSB5n6c+
uj//8F3NwvyIFwVVYdn0XnDML777HOY6qwsIQFtjNV8MCzPyEdO9qGAyrORWKLIs
rDNUFByTi+i8Eg0iOUeDHarOq9JRRXk9iw09Mo207PeplnjoIvFUbdDLBkpiaIf2
6xh2tIsnZRBcVWyLmwtNPA+4Hg59a6mujwV0HEgoq1gZ55fS6qxB9gHyVElWmqZX
beGvhIM6Oa4j6449UFbGhAMn5hl7m3xXZyGr+HuV8u1RYEpMEpkiNV3CMrMSPQbA
5LY4cIsOLWmuuI/RQJmecftKiKu+YkoknRNjqF7TIqdX5qVMP6V4buB6MeFvB6R/
vgDgxnQp
=JHzT
-----END PGP SIGNATURE-----

Timestamp of file with hash 7017836e4e8e4ce0003284c993c86597e80a695b740243eca5ab7c60803d2d5e -

@maflcko maflcko merged commit c95784e into bitcoin:master Sep 28, 2020
Copy link
Member
@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

re-ACK 4cc7171, only suggested changes since my previous review.

deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request May 5, 2021
Summary:
4cc7171c9887e88027642927c979e507d7b78dda wallet: no need for duplicate storage for ABANDON_HASH constant (Anthony Towns)
82cf4641f4a161834d07ce83c18982d9b143c040 scripted-diff: Replace UINT256_ONE() with uint256::ONE (Anthony Towns)
183f308fff4caad3e3ada654b6fdf597d262c4c1 uint256: Update constructors to c++11, make ONE static (Anthony Towns)

Pull request description:

  `UINT256_ONE()` returns a reference to a global; mark it as const to be sure someone doesn't accidently modify it.

---

Backport of [[bitcoin/bitcoin#20016 | core#20016]]

Test Plan:
  ninja all check check-functional

Reviewers: #bitcoin_abc, deadalnix

Reviewed By: #bitcoin_abc, deadalnix

Differential Revision: https://reviews.bitcoinabc.org/D9477
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Feb 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants
0