Skip to content

Commit

Permalink
Bug 1307046 - Avoid memcpy(_, nullptr) calls in DataBuffer::Write() r…
Browse files Browse the repository at this point in the history
…=mt,ekr
  • Loading branch information
Tim Taubert committed Oct 4, 2016
1 parent 82cfe2f commit b087bfc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
12 changes: 9 additions & 3 deletions external_tests/ssl_gtest/databuffer.h
Expand Up @@ -64,19 +64,24 @@ class DataBuffer {
// Write will do a new allocation and expand the size of the buffer if needed.
// Returns the offset of the end of the write.
size_t Write(size_t index, const uint8_t* val, size_t count) {
assert(val);
if (index + count > len_) {
size_t newlen = index + count;
uint8_t* tmp = new uint8_t[newlen]; // Always > 0.
memcpy(static_cast<void*>(tmp), static_cast<const void*>(data_), len_);
if (data_) {
memcpy(static_cast<void*>(tmp), static_cast<const void*>(data_), len_);
}
if (index > len_) {
memset(static_cast<void*>(tmp + len_), 0, index - len_);
}
delete[] data_;
data_ = tmp;
len_ = newlen;
}
memcpy(static_cast<void*>(data_ + index), static_cast<const void*>(val),
count);
if (data_) {
memcpy(static_cast<void*>(data_ + index), static_cast<const void*>(val),
count);
}
return index + count;
}

Expand Down Expand Up @@ -116,6 +121,7 @@ class DataBuffer {

void Splice(const uint8_t* ins, size_t ins_len, size_t index,
size_t remove = 0) {
assert(ins);
uint8_t* old_value = data_;
size_t old_len = len_;

Expand Down
4 changes: 3 additions & 1 deletion external_tests/ssl_gtest/ssl_extension_unittest.cc
Expand Up @@ -102,7 +102,9 @@ class TlsExtensionInjector : public TlsHandshakeFilter {
output->Splice(type_length, offset + 2);

// Insert the payload.
output->Splice(data_, offset + 6);
if (data_.len() > 0) {
output->Splice(data_, offset + 6);
}

return CHANGE;
}
Expand Down
4 changes: 3 additions & 1 deletion external_tests/ssl_gtest/tls_filter.cc
Expand Up @@ -405,7 +405,9 @@ PacketFilter::Action TlsExtensionFilter::FilterExtensions(
// Write out extension.
offset = output->Write(offset, extension_type, 2);
offset = output->Write(offset, source->len(), 2);
offset = output->Write(offset, *source);
if (source->len() > 0) {
offset = output->Write(offset, *source);
}
}
output->Truncate(offset);

Expand Down

0 comments on commit b087bfc

Please sign in to comment.