Fix a bug where memcmp takes the address of a pointer as second argument. As far as I can see, the intention with this line of code appears to be comparing two plain arrays.
I have not tested this fix thoroughly.
Fix a bug where memcmp takes the address of a pointer as second argument. As far as I can see, the intention with this line of code appears to be comparing two plain arrays.
I have not tested this fix thoroughly.
52 | @@ -53,7 +53,7 @@ std::map<std::string, BerkeleyEnvironment> g_dbenvs GUARDED_BY(cs_db); //!< Map 53 | 54 | bool WalletDatabaseFileId::operator==(const WalletDatabaseFileId& rhs) const 55 | { 56 | - return memcmp(value, &rhs.value, sizeof(value)) == 0; 57 | + return memcmp(value, rhs.value, sizeof(value)) == 0;
Might I ask, why is this a memcmp at all, when value is basically a u_int8_t?
Wouldn't return rhs.value == value be simpler?
😬sorry about that.
No problem :smile:
Taking the address of an array gives a pointer to its first element. It does not give you a pointer-to-a-pointer. I believe both the old and the new code are correct here.
utACK cd509e59, at least arguments are "consistent" now.
Taking the address of an array gives a pointer to its first element. It does not give you a pointer-to-a-pointer. I believe both the old and the new code are correct here.
That's what I was unsure about. For a static array this may be true, but are you sure it is defined behaviour across compilers?
If it was &rhs.value[0] it would be another matter.
This is not an implementation detail.
C arrays' behavior is specified by the language in this regard. For most operations, arrays automatically decay into a pointer to its first element. But there are a few operations specific to arrays for which this does not happen, including sizeof (which gives you the size of the array, not of a pointer to it), and the & operator, which also gives a pointer to the first element directly, not after first also converting to a pointer. That would be nonsensical anyway, as it'd be returning a pointer to a temporary pointer.
If this fixes a bug, this needs a test. If not, please change the PR and commit title to say something like clarifying the code.
NAK, doesn't fix a bug, doesn't make the code more clear: memcmp (&,&) is perfectly idiomatic code.
I take your word for it, this being defined behaviour and idiomatic code.
I'll close this one.