From aff10976fc7722b1174fc3dcce15bfe8ebdfcbcd Mon Sep 17 00:00:00 2001 From: John Millikin Date: Mon, 16 Oct 2017 12:05:21 -0700 Subject: Fix undefined memory management found by Clang's sanitizers. See https://github.com/google/protobuf/issues/3752 for context. --- src/google/protobuf/util/json_util.cc | 8 +++++--- src/google/protobuf/util/json_util.h | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'src/google/protobuf/util') diff --git a/src/google/protobuf/util/json_util.cc b/src/google/protobuf/util/json_util.cc index c85f1899..ce3569ce 100644 --- a/src/google/protobuf/util/json_util.cc +++ b/src/google/protobuf/util/json_util.cc @@ -61,9 +61,11 @@ void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) { buffer_size_ -= len; return; } - memcpy(buffer_, bytes, buffer_size_); - bytes += buffer_size_; - len -= buffer_size_; + if (buffer_size_ > 0) { + memcpy(buffer_, bytes, buffer_size_); + bytes += buffer_size_; + len -= buffer_size_; + } if (!stream_->Next(&buffer_, &buffer_size_)) { // There isn't a way for ByteSink to report errors. buffer_size_ = 0; diff --git a/src/google/protobuf/util/json_util.h b/src/google/protobuf/util/json_util.h index f4f4380a..dee3ddba 100644 --- a/src/google/protobuf/util/json_util.h +++ b/src/google/protobuf/util/json_util.h @@ -179,7 +179,7 @@ namespace internal { class LIBPROTOBUF_EXPORT ZeroCopyStreamByteSink : public strings::ByteSink { public: explicit ZeroCopyStreamByteSink(io::ZeroCopyOutputStream* stream) - : stream_(stream), buffer_size_(0) {} + : stream_(stream), buffer_(NULL), buffer_size_(0) {} ~ZeroCopyStreamByteSink(); virtual void Append(const char* bytes, size_t len); -- cgit v1.2.3