-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Fixes subscript 0 (&var[0]) where should use (var.data()) instead. #9804
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
Conversation
src/utilstrencodings.cpp
Outdated
@@ -229,7 +229,7 @@ vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid) | |||
string DecodeBase64(const string& str) | |||
{ | |||
vector<unsigned char> vchRet = DecodeBase64(str.c_str()); | |||
return (vchRet.size() == 0) ? string() : string((const char*)vchRet.data(), vchRet.size()); | |||
return string((const char*)vchRet.data(), vchRet.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::string
maybe?
21b88a4
to
efa6dea
Compare
Sorry for the bit of line noise, there was some UB when I removed some of the branches (memset/memcpy/read/write 0 bytes on nullptr/invalid ptr) that was causing one of the builds (with more aggressive runtime checks) to fail. I added them back in and all seems well now. |
Whoa, good catch. I'm shocked that &vch[0] use was still so rampant in the code. We'd introduced Anyhow, concept ACK. |
src/serialize.h
Outdated
@@ -571,7 +571,7 @@ void Serialize_impl(Stream& os, const prevector<N, T>& v, const unsigned char&) | |||
{ | |||
WriteCompactSize(os, v.size()); | |||
if (!v.empty()) | |||
os.write((char*)&v[0], v.size() * sizeof(T)); | |||
os.write((char*) v.data(), v.size() * sizeof(T)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why we're casting to (char*)
here iso (const char*)
, even though these are write operations and the type passed in is const
.
There's a couple more of these. Though seems out of scope to fix in this pull.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that seems obviously incorrect, but I think I understand why it was done (or at least have a theory). Because the methods are all generic w.r.t. the stream type, they probably are made to work with some arbitrary stream that can't accept a const char *
. Hopefully not the case anymore...
Anyways, probably best to fix those up separately as you suggest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but it would be wrong for a stream write to take a char* i.s.o. const char*, so fixing those just means the const-correctness spreads.
There are still a few more places that were trickier/not possible to correct (where the type passed in is templated, such that it would be a pointer or a vector). I also didn't fix anything in subdirectories, because I felt this PR was getting a bit large already, and there are a few you can still grep for pretty easily there:
(there are a lot more worth checking if you don't use the hungarian notation |
src/serialize.h
Outdated
@@ -549,7 +549,7 @@ void Serialize(Stream& os, const std::basic_string<C>& str) | |||
{ | |||
WriteCompactSize(os, str.size()); | |||
if (!str.empty()) | |||
os.write((char*)&str[0], str.size() * sizeof(str[0])); | |||
os.write((char*) str.data(), str.size() * sizeof(typename std::basic_string<C>::value_type)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT, this is equivalent to sizeof(C) (std::basic_string<C>::value_type is std::basic_string<C, std::char_traits<C>>::value_type which is std::char_traits<C>::char_type, which is C).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TheBlueMatt is right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I was aware :) I felt that the version I chose was more similar to the existing code (the size of whatever str[0] returns), but if everyone is comfortable with sizeof(C) I can change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 fixed
src/utilstrencodings.cpp
Outdated
@@ -229,7 +229,7 @@ vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid) | |||
string DecodeBase64(const string& str) | |||
{ | |||
vector<unsigned char> vchRet = DecodeBase64(str.c_str()); | |||
return (vchRet.size() == 0) ? string() : string((const char*)&vchRet[0], vchRet.size()); | |||
return std::string((const char*)vchRet.data(), vchRet.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, based on cppreference I cant tell if this is well-defined with vchRet.size() == 0 or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From my understanding (it doesn't seem ambiguous, but, it certainly isn't definitive), a len
of 0
is well-defined, and simply an empty string. Aka, equivalent to std::string()
.
I don't have a definitive statement from the reference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, a string with size 0 is well-defined. Calling .data()
on a string of size 0 is also defined. This should be ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The question is whether calling std::string(garbage_pointer, 0)
is well-defined. I can't tell from what cppreference.com says (The behavior is undefined if s does not point at an array of at least count elements of CharT, including the case when s is a null pointer.).
As an alternative, what about std::string(vchRet.begin(), vchRet.end())
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read that as: If the count is 0, it always points at least 0 elements, thus "point at an array of at least count elements of CharT" is always satisfied.
Alternatively: if count is 0 it would be wrong to dereference the pointer's address at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced that "a pointer to an array of 0 elements of type T" can be any pointer. I think it's extremely unlikely to matter, and that every C++ std library effectively is implemented in a way that makes the constructor, when invoked with (pointer, 0) effectively ignore pointer, however.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean when cppreference is ambiguous thats probably a good sign that trying to parse a wiki is going to get your an incorrect result...best to go to the standard if in doubt in that case. :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is can be a garbage pointer. vector.data() is guaranteed to return a valid range (even when empty), which I think would require that it have proper alignment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be complete: this is not the same as std::string(vchRet.begin(), vchRet.end())
in the general case. When creating an iterator range, if the type of vch is different from the type of string, a per-element cast will happen. So string.size()
will be vch.size()
. Usual issues with casting between signed and unsigned apply.
std::string((const char*)vchRet.data(), vchRet.size())
however just return the memory area with all the elements of the vector as a string. So string.size()
will be vch.size()*sizeof(vch::value_type)
. It is likely also more efficient.
Concept ACK. |
Concept ACK! 👍 |
7a3a263
to
337e562
Compare
Rebased. |
337e562
to
69fd8ab
Compare
src/hash.cpp
Outdated
const uint8_t* tail = (const uint8_t*)(&vDataToHash[0] + nblocks * 4); | ||
//---------- | ||
// tail | ||
const uint8_t* tail = (const uint8_t*)(vDataToHash.data() + nblocks * 4); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit: "Cleanup (safe, it was checked) 0-subscript in MurmurHash3"
Could you drop this (const uint8_t*) cast here? It shouldn't be needed since we already call vDataToHash.data() with no cast above on line 27.
src/utilstrencodings.cpp
Outdated
@@ -227,8 +227,8 @@ std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid) | |||
|
|||
std::string DecodeBase64(const std::string& str) | |||
{ | |||
std::vector<unsigned char> vchRet = DecodeBase64(str.c_str()); | |||
return (vchRet.size() == 0) ? std::string() : std::string((const char*)&vchRet[0], vchRet.size()); | |||
vector<unsigned char> vchRet = DecodeBase64(str.c_str()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit "Fix 0-subscript in utilstrencodings.cpp"
Should put back std:: prefixes.
src/utilstrencodings.cpp
Outdated
@@ -414,8 +414,13 @@ std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid) | |||
|
|||
std::string DecodeBase32(const std::string& str) | |||
{ | |||
<<<<<<< 1f0401c11315b5090cfe2b37f6338205e2984372 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit "Fix 0-subscript in utilstrencodings.cpp"
Bad merge here.
src/streams.h
Outdated
@@ -385,7 +385,7 @@ class CDataStream | |||
{ | |||
// Special case: stream << stream concatenates like stream += stream | |||
if (!vch.empty()) | |||
s.write((char*)&vch[0], vch.size() * sizeof(vch[0])); | |||
s.write((char*) vch.data(), vch.size() * sizeof(typename decltype(vch)::value_type)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit "Fix subscript-0 in streams.h"
It would seem more direct (and shorter) to write sizeof(*vch.data()).
src/utilstrencodings.cpp
Outdated
@@ -227,8 +227,8 @@ std::vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid) | |||
|
|||
std::string DecodeBase64(const std::string& str) | |||
{ | |||
vector<unsigned char> vchRet = DecodeBase64(str.c_str()); | |||
return (vchRet.size() == 0) ? string() : string((const char*)vchRet.data(), vchRet.size()); | |||
std::vector<unsigned char> vchRet = DecodeBase64(str.c_str()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In commit "Remove uneccessary branches in utilstrencodings string constructors."
Some of the changes in this commit should be squashed into previous commit "In commit "Fix 0-subscript in utilstrencodings.cpp."
Also missing an n in unnecessary.
c4d844a
to
275caff
Compare
@ryanofsky feedback addressed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
utACK 275caff431cea65aa77d6a3035597b4b2b9b0052. Only changes since previous review (69fd8ab0a6543dace4b080166b21b2aafdd2c0a9) were addressing feedback and combining / rearranging some of the commits.
Needs rebase again (sorry). |
…up MurmurHash3 to be more clear).
…) instead. Summary: 30ac7688e Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2e2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b5a Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd98 Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1e0 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e55f Fix subscript[0] in torcontrol (Jeremy Rubin) b6856ebed Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d95265 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf16 Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119e6 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710bd2 Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3e2 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362 Backport of Core PR 9693 bitcoin/bitcoin#9804 Test Plan: make check test_runner.py Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc Subscribers: markblundeberg Differential Revision: https://reviews.bitcoinabc.org/D2964
…) instead. Summary: 30ac7688e Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2e2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b5a Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd98 Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1e0 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e55f Fix subscript[0] in torcontrol (Jeremy Rubin) b6856ebed Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d95265 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf16 Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119e6 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710bd2 Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3e2 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362 Backport of Core PR 9693 bitcoin/bitcoin#9804 Test Plan: make check test_runner.py Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: Fabien, O1 Bitcoin ABC, #bitcoin_abc Subscribers: markblundeberg Differential Revision: https://reviews.bitcoinabc.org/D2964
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
Summary: 592404f03 Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin/bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656 Backport of Core PR10793 bitcoin/bitcoin#10793 Test Plan: make check test_runner.py Reviewers: deadalnix, Fabien, jasonbcox, O1 Bitcoin ABC, #bitcoin_abc Reviewed By: deadalnix, O1 Bitcoin ABC, #bitcoin_abc Differential Revision: https://reviews.bitcoinabc.org/D3656
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
….data()) instead. 30ac768 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 4b1c0f2 Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) e19db7b Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) bc2e7fd Fix subscript[0] in streams.h (Jeremy Rubin) 4cac0d1 Fix subscript[0] in validation.cpp (Jeremy Rubin) ac658e5 Fix subscript[0] in torcontrol (Jeremy Rubin) b6856eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) 361d952 Fix subscript[0] in base58.cpp (Jeremy Rubin) 6896dbf Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 96f2119 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 500710b Fix 2 subscript[0] bugs in pubkey.cpp, and eliminate one extra size check (Jeremy Rubin) e0451e3 Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) Tree-SHA512: 5b9103652cf8c615bd8f4f32b3573d291d6b67c39e0308ce00100bc6625f346e8e016b4c999f4f34f5c37ae059490a83c3b513deb21f838af785227d06e02362
Locked memory manager Add a pool for locked memory chunks, replacing `LockedPageManager`. Cherry-picked from the following upstream PRs: - bitcoin/bitcoin#8321 - bitcoin/bitcoin#8753 - bitcoin/bitcoin#9063 - bitcoin/bitcoin#9070 - bitcoin/bitcoin#11385 - bitcoin/bitcoin#12048 - Excludes change to benchmark. - bitcoin/bitcoin#15117 - bitcoin/bitcoin#16161 - Excludes Travis CI changes. - Includes change from bitcoin/bitcoin#13163 - bitcoin/bitcoin#15600 - bitcoin/bitcoin#18443 - Assorted small changes from: - bitcoin/bitcoin#9233 - bitcoin/bitcoin#10483 - bitcoin/bitcoin#10645 - bitcoin/bitcoin#10969 - bitcoin/bitcoin#11351 - bitcoin/bitcoin#19111 - Excludes change to `src/rpc/server.cpp` - bitcoin/bitcoin#9804 - Only the commit for `src/key.cpp` - bitcoin/bitcoin#9598
fac30ee refactor: Replace &foo[0] with foo.data() (MarcoFalke) faece47 refactor: Avoid &foo[0] on C-Style arrays (MarcoFalke) face961 refactor: Use only one temporary buffer in CreateObfuscateKey (MarcoFalke) fa05ddd refactor: Use CPubKey vector constructor where possible (MarcoFalke) fabb6df script: Replace address-of idiom with vector data() method (Guido Vranken) Pull request description: The main theme of this refactor is to replace `&foo[0]` with `foo.data()`. The first commit is taken from #21781 with the rationale: * In CSignatureCache::ComputeEntryECDSA, change the way a vector pointer is resolved to prevent invoking undefined behavior if the vector is empty. The other commits aim to remove all `&foo[0]`, where `foo` is any kind of byte representation. The rationale: * Sometimes alternative code without any raw data pointers is easier to read (refer to the respective commit message for details) * If the raw data pointer is needed, `foo.data()` should be preferred, as pointed out in the developer notes. This addresses the instances that have been missed in commit 592404f, and #9804 ACKs for top commit: laanwj: Code review ACK fac30ee practicalswift: cr ACK fac30ee: patch looks correct promag: Code review ACK fac30ee. Tree-SHA512: e7e73146edbc78911a8e8c728b0a1c6b0ed9a88a008e650aa5dbffe72425bd42c76df70199a9cf7e02637448d7593e0eac52fd0f91f59240283e1390ee21bfa5
fcdf87a Populate services in GetLocalAddress (Alex Morcos) a0a079f Return early in IsBanned. (Gregory Maxwell) 7772138 Remove redundant nullptr checks before deallocation (practicalswift) 5390862 net: remove unimplemented functions. (furszy) 74d0482 Limit setAskFor and retire requested entries only when a getdata returns. (Gregory Maxwell) 3b3bf63 prevent peer flooding request queue for an inv (kazcw) c814967 Fix subscript[0] potential bugs in key.cpp (Jeremy Rubin) 8e2e79e Remove unnecessary branches in utilstrencodings string constructors. (Jeremy Rubin) 25a16b3 Fix subscript[0] in utilstrencodings.cpp (Jeremy Rubin) df0584e Fix subscript[0] in streams.h (Jeremy Rubin) b7e64a5 Fix subscript[0] in validation.cpp (Jeremy Rubin) 2f7b73b Fix subscript[0] in torcontrol (Jeremy Rubin) 3b883eb Fix subscript[0] in netaddress.cpp (Jeremy Rubin) e5f5401 Fix subscript[0] in base58.cpp (Jeremy Rubin) 7d4ec87 Cleanup (safe, it was checked) subscript[0] in MurmurHash3 (and cleanup MurmurHash3 to be more clear). (Jeremy Rubin) 5e14f54 Fix subscript[0] in compressor.cpp (Jeremy Rubin) 48a622d Fix subscript[0] bug in net.cpp if GetGroup returns a 0-sized vector (Jeremy Rubin) af4cba3 net: use an internal address for fixed seeds (Cory Fields) 36368c5 net: switch to dummy internal ip for dns seed source (Cory Fields) 8f31295 net: do not allow resolving to an internal address (Cory Fields) 8ba3a40 net: add an internal subnet for representing unresolved hostnames (Cory Fields) c60b875 fix uninitialized read when stringifying an addrLocal (Kaz Wesley) 3d36540 add test demonstrating addrLocal UB (Kaz Wesley) Pull request description: Grouped some net updates (plus a miscellaneous one) coming from upstream. Part of another deep rabbit hole that i'm doing in parallel of the tier two work. PRs List: * bitcoin#7079. * bitcoin#9804. * bitcoin#10424 * bitcoin#10446. * bitcoin#10564. * bitcoin#14728. ACKs for top commit: random-zebra: Code ACK fcdf87a Tree-SHA512: ee81c834641aa6fdb9ca4396657457358a4b32f7862d60716e914dcfc2d355970937bd0fb4e164faaa0f4ea26d263ca8a2af4d8d5d6615b2db5bf89ec70d15f3
592404f Changing &vec[0] to vec.data(), what 9804 missed (MeshCollider) Pull request description: This just continues the work of bitcoin#9804 Modifies a lot of `&vector[]`'s to `vector.data()`'s across all the files including tests, just the stuff that 9804 missed Tree-SHA512: dd1a9dffb999dea4fba78dcc91fe02f90250db86f5c74948e1ff3e8b4036b2154b600555eaa04dece5368920aae3513bc36425dc96e4319ca1041b0928a6b656
This PR fixes up a couple places where the codebase has semi-risky uses of 0 subscript. I don't think any of these are actively bugs, but some of them are unsafe (or unexpected-errors) for unsanitized inputs (such as in addrdb or key.cpp).
This also allows us to eliminate some bounds checks, so I guess this is a net-negative patch. I don't think any of those bounds checks are performance sensitive, but worth double checking in review.
p.s. I also fix some confusing code in murmurhash (with non obvious offset-math) as it took me a little bit to see that it was correct, could separate that out if desired.