Commit c4f59dcc authored by Feng Xiao's avatar Feng Xiao Committed by GitHub

Merge pull request #3760 from jmillikin-stripe/descriptor-memset-ub

Fix undefined memory management found by Clang's sanitizers.
parents f850188e aff10976
...@@ -4309,8 +4309,10 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl( ...@@ -4309,8 +4309,10 @@ FileDescriptor* DescriptorBuilder::BuildFileImpl(
result->dependencies_once_ = tables_->AllocateOnceDynamic(); result->dependencies_once_ = tables_->AllocateOnceDynamic();
result->dependencies_names_ = result->dependencies_names_ =
tables_->AllocateArray<const string*>(proto.dependency_size()); tables_->AllocateArray<const string*>(proto.dependency_size());
memset(result->dependencies_names_, 0, if (proto.dependency_size() > 0) {
sizeof(*result->dependencies_names_) * proto.dependency_size()); memset(result->dependencies_names_, 0,
sizeof(*result->dependencies_names_) * proto.dependency_size());
}
} else { } else {
result->dependencies_once_ = NULL; result->dependencies_once_ = NULL;
result->dependencies_names_ = NULL; result->dependencies_names_ = NULL;
......
...@@ -1244,10 +1244,12 @@ class TextFormat::Printer::TextGenerator ...@@ -1244,10 +1244,12 @@ class TextFormat::Printer::TextGenerator
while (size > buffer_size_) { while (size > buffer_size_) {
// Data exceeds space in the buffer. Copy what we can and request a // Data exceeds space in the buffer. Copy what we can and request a
// new buffer. // new buffer.
memcpy(buffer_, data, buffer_size_); if (buffer_size_ > 0) {
data += buffer_size_; memcpy(buffer_, data, buffer_size_);
size -= buffer_size_; data += buffer_size_;
void* void_buffer; size -= buffer_size_;
}
void* void_buffer = NULL;
failed_ = !output_->Next(&void_buffer, &buffer_size_); failed_ = !output_->Next(&void_buffer, &buffer_size_);
if (failed_) return; if (failed_) return;
buffer_ = reinterpret_cast<char*>(void_buffer); buffer_ = reinterpret_cast<char*>(void_buffer);
......
...@@ -61,9 +61,11 @@ void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) { ...@@ -61,9 +61,11 @@ void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) {
buffer_size_ -= len; buffer_size_ -= len;
return; return;
} }
memcpy(buffer_, bytes, buffer_size_); if (buffer_size_ > 0) {
bytes += buffer_size_; memcpy(buffer_, bytes, buffer_size_);
len -= buffer_size_; bytes += buffer_size_;
len -= buffer_size_;
}
if (!stream_->Next(&buffer_, &buffer_size_)) { if (!stream_->Next(&buffer_, &buffer_size_)) {
// There isn't a way for ByteSink to report errors. // There isn't a way for ByteSink to report errors.
buffer_size_ = 0; buffer_size_ = 0;
......
...@@ -179,7 +179,7 @@ namespace internal { ...@@ -179,7 +179,7 @@ namespace internal {
class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink { class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink {
public: public:
explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream) explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream)
: stream_(stream), buffer_size_(0) {} : stream_(stream), buffer_(NULL), buffer_size_(0) {}
~ZeroCopyStreamByteSink(); ~ZeroCopyStreamByteSink();
virtual void Append(const char* bytes, size_t len); virtual void Append(const char* bytes, size_t len);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment