diff options
author | Ubuntu <s.wegele@gmx.de> | 2017-12-19 11:57:44 +0000 |
---|---|---|
committer | Ubuntu <s.wegele@gmx.de> | 2017-12-19 11:57:44 +0000 |
commit | ffa18ad28fe73dc81088988fa7ea86c23784237a (patch) | |
tree | e9dcc8644f9ccf2c74708d16dcb31bd3c164669a | |
parent | 860d693cf70f13d823ab8e6f9118d94485fa95bd (diff) | |
download | protobuf-ffa18ad28fe73dc81088988fa7ea86c23784237a.tar.gz protobuf-ffa18ad28fe73dc81088988fa7ea86c23784237a.tar.bz2 protobuf-ffa18ad28fe73dc81088988fa7ea86c23784237a.zip |
resolve issue 4032 and added a unit test
-rw-r--r-- | src/google/protobuf/util/json_util.cc | 3 | ||||
-rw-r--r-- | src/google/protobuf/util/json_util_test.cc | 15 |
2 files changed, 17 insertions, 1 deletions
diff --git a/src/google/protobuf/util/json_util.cc b/src/google/protobuf/util/json_util.cc index ce3569ce..e917ecc4 100644 --- a/src/google/protobuf/util/json_util.cc +++ b/src/google/protobuf/util/json_util.cc @@ -50,7 +50,8 @@ namespace util { namespace internal { ZeroCopyStreamByteSink::~ZeroCopyStreamByteSink() { - stream_->BackUp(buffer_size_); + if (buffer_size_ > 0) + stream_->BackUp(buffer_size_); } void ZeroCopyStreamByteSink::Append(const char* bytes, size_t len) { diff --git a/src/google/protobuf/util/json_util_test.cc b/src/google/protobuf/util/json_util_test.cc index 25c7e96c..1395356b 100644 --- a/src/google/protobuf/util/json_util_test.cc +++ b/src/google/protobuf/util/json_util_test.cc @@ -40,6 +40,7 @@ #include <google/protobuf/util/json_format_proto3.pb.h> #include <google/protobuf/util/type_resolver.h> #include <google/protobuf/util/type_resolver_util.h> +#include <google/protobuf/io/zero_copy_stream_impl.h> #include <gtest/gtest.h> namespace google { @@ -457,6 +458,20 @@ TEST(ZeroCopyStreamByteSinkTest, TestAllInputOutputPatterns) { } } +TEST_F(JsonUtilTest, TestWronJsonInput) { + const char json[] = "{\"unknown_field\":\"some_value\"}"; + google::protobuf::io::ArrayInputStream input_stream(json, strlen(json)); + char protoBuffer[10000]; + google::protobuf::io::ArrayOutputStream output_stream(protoBuffer, sizeof(protoBuffer)); + const char *messageType = "type.googleapis.com/proto3.TestMessage"; + TypeResolver* resolver = NewTypeResolverForDescriptorPool("type.googleapis.com", DescriptorPool::generated_pool()); + + util::Status resultStatus = util::JsonToBinaryStream(resolver, messageType, &input_stream, &output_stream); + + EXPECT_FALSE(resultStatus.ok()); + EXPECT_EQ(resultStatus.error_code(), google::protobuf::util::error::INVALID_ARGUMENT); +} + } // namespace } // namespace util } // namespace protobuf |