ensure no NULL is passed to memcpy

NULL is valid when size is 0, but memcpy uses nonnull attributes,
so let's not poke the bear
This commit is contained in:
moneromooo-monero 2019-04-29 08:17:32 +00:00
parent bc09766bf9
commit 0564da5fdc
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
4 changed files with 18 additions and 9 deletions

View file

@ -64,6 +64,7 @@ void buffer::append(const void *data, size_t sz)
size_t reserve = (((size() + sz) * 3 / 2) + 4095) & ~4095; size_t reserve = (((size() + sz) * 3 / 2) + 4095) & ~4095;
new_storage.reserve(reserve); new_storage.reserve(reserve);
new_storage.resize(size()); new_storage.resize(size());
if (size() > 0)
memcpy(new_storage.data(), storage.data() + offset, storage.size() - offset); memcpy(new_storage.data(), storage.data() + offset, storage.size() - offset);
offset = 0; offset = 0;
std::swap(storage, new_storage); std::swap(storage, new_storage);

View file

@ -62,12 +62,14 @@ wipeable_string::wipeable_string(wipeable_string &&other)
wipeable_string::wipeable_string(const std::string &other) wipeable_string::wipeable_string(const std::string &other)
{ {
grow(other.size()); grow(other.size());
if (size() > 0)
memcpy(buffer.data(), other.c_str(), size()); memcpy(buffer.data(), other.c_str(), size());
} }
wipeable_string::wipeable_string(std::string &&other) wipeable_string::wipeable_string(std::string &&other)
{ {
grow(other.size()); grow(other.size());
if (size() > 0)
memcpy(buffer.data(), other.c_str(), size()); memcpy(buffer.data(), other.c_str(), size());
if (!other.empty()) if (!other.empty())
{ {
@ -79,6 +81,7 @@ wipeable_string::wipeable_string(std::string &&other)
wipeable_string::wipeable_string(const char *s) wipeable_string::wipeable_string(const char *s)
{ {
grow(strlen(s)); grow(strlen(s));
if (size() > 0)
memcpy(buffer.data(), s, size()); memcpy(buffer.data(), s, size());
} }
@ -112,15 +115,19 @@ void wipeable_string::grow(size_t sz, size_t reserved)
} }
size_t old_sz = buffer.size(); size_t old_sz = buffer.size();
std::unique_ptr<char[]> tmp{new char[old_sz]}; std::unique_ptr<char[]> tmp{new char[old_sz]};
memcpy(tmp.get(), buffer.data(), old_sz * sizeof(char));
if (old_sz > 0) if (old_sz > 0)
{
memcpy(tmp.get(), buffer.data(), old_sz * sizeof(char));
memwipe(buffer.data(), old_sz * sizeof(char)); memwipe(buffer.data(), old_sz * sizeof(char));
}
buffer.reserve(reserved); buffer.reserve(reserved);
buffer.resize(sz); buffer.resize(sz);
memcpy(buffer.data(), tmp.get(), old_sz * sizeof(char));
if (old_sz > 0) if (old_sz > 0)
{
memcpy(buffer.data(), tmp.get(), old_sz * sizeof(char));
memwipe(tmp.get(), old_sz * sizeof(char)); memwipe(tmp.get(), old_sz * sizeof(char));
} }
}
void wipeable_string::push_back(char c) void wipeable_string::push_back(char c)
{ {

View file

@ -1077,11 +1077,11 @@ void BlockchainLMDB::add_tx_amount_output_indices(const uint64_t tx_id,
int result = 0; int result = 0;
int num_outputs = amount_output_indices.size(); size_t num_outputs = amount_output_indices.size();
MDB_val_set(k_tx_id, tx_id); MDB_val_set(k_tx_id, tx_id);
MDB_val v; MDB_val v;
v.mv_data = (void *)amount_output_indices.data(); v.mv_data = num_outputs ? (void *)amount_output_indices.data() : (void*)"";
v.mv_size = sizeof(uint64_t) * num_outputs; v.mv_size = sizeof(uint64_t) * num_outputs;
// LOG_PRINT_L1("tx_outputs[tx_hash] size: " << v.mv_size); // LOG_PRINT_L1("tx_outputs[tx_hash] size: " << v.mv_size);

View file

@ -116,6 +116,7 @@ void keccak(const uint8_t *in, size_t inlen, uint8_t *md, int mdlen)
local_abort("Bad keccak use"); local_abort("Bad keccak use");
} }
if (inlen > 0)
memcpy(temp, in, inlen); memcpy(temp, in, inlen);
temp[inlen++] = 1; temp[inlen++] = 1;
memset(temp + inlen, 0, rsiz - inlen); memset(temp + inlen, 0, rsiz - inlen);