Internal change

PiperOrigin-RevId: 573332237
22.x
Adam Cozzette 1 year ago
parent c24ca6b36f
commit 8352afe1d3
  1. 22
      src/google/protobuf/io/test_zero_copy_stream.h
  2. 1
      src/google/protobuf/json/BUILD.bazel
  3. 2
      src/google/protobuf/json/internal/parser.cc
  4. 20
      src/google/protobuf/json/json_test.cc

@ -32,12 +32,12 @@
#define GOOGLE_PROTOBUF_IO_TEST_ZERO_COPY_STREAM_H__ #define GOOGLE_PROTOBUF_IO_TEST_ZERO_COPY_STREAM_H__
#include <deque> #include <deque>
#include <memory>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "absl/log/absl_check.h" #include "absl/log/absl_check.h"
#include "absl/types/optional.h"
#include "google/protobuf/io/zero_copy_stream.h" #include "google/protobuf/io/zero_copy_stream.h"
// Must be included last. // Must be included last.
@ -60,18 +60,22 @@ class TestZeroCopyInputStream final : public ZeroCopyInputStream {
TestZeroCopyInputStream(const TestZeroCopyInputStream& other) TestZeroCopyInputStream(const TestZeroCopyInputStream& other)
: ZeroCopyInputStream(), : ZeroCopyInputStream(),
buffers_(other.buffers_), buffers_(other.buffers_),
last_returned_buffer_(other.last_returned_buffer_), last_returned_buffer_(
other.last_returned_buffer_
? std::make_unique<std::string>(*other.last_returned_buffer_)
: nullptr),
byte_count_(other.byte_count_) {} byte_count_(other.byte_count_) {}
bool Next(const void** data, int* size) override { bool Next(const void** data, int* size) override {
ABSL_CHECK(data) << "data must not be null"; ABSL_CHECK(data) << "data must not be null";
ABSL_CHECK(size) << "size must not be null"; ABSL_CHECK(size) << "size must not be null";
last_returned_buffer_ = absl::nullopt; last_returned_buffer_ = nullptr;
// We are done // We are done
if (buffers_.empty()) return false; if (buffers_.empty()) return false;
last_returned_buffer_ = std::move(buffers_.front()); last_returned_buffer_ =
std::make_unique<std::string>(std::move(buffers_.front()));
buffers_.pop_front(); buffers_.pop_front();
*data = last_returned_buffer_->data(); *data = last_returned_buffer_->data();
*size = static_cast<int>(last_returned_buffer_->size()); *size = static_cast<int>(last_returned_buffer_->size());
@ -81,19 +85,19 @@ class TestZeroCopyInputStream final : public ZeroCopyInputStream {
void BackUp(int count) override { void BackUp(int count) override {
ABSL_CHECK_GE(count, 0) << "count must not be negative"; ABSL_CHECK_GE(count, 0) << "count must not be negative";
ABSL_CHECK(last_returned_buffer_.has_value()) ABSL_CHECK(last_returned_buffer_ != nullptr)
<< "The last call was not a successful Next()"; << "The last call was not a successful Next()";
ABSL_CHECK_LE(count, last_returned_buffer_->size()) ABSL_CHECK_LE(count, last_returned_buffer_->size())
<< "count must be within bounds of last buffer"; << "count must be within bounds of last buffer";
buffers_.push_front( buffers_.push_front(
last_returned_buffer_->substr(last_returned_buffer_->size() - count)); last_returned_buffer_->substr(last_returned_buffer_->size() - count));
last_returned_buffer_ = absl::nullopt; last_returned_buffer_ = nullptr;
byte_count_ -= count; byte_count_ -= count;
} }
bool Skip(int count) override { bool Skip(int count) override {
ABSL_CHECK_GE(count, 0) << "count must not be negative"; ABSL_CHECK_GE(count, 0) << "count must not be negative";
last_returned_buffer_ = absl::nullopt; last_returned_buffer_ = nullptr;
while (true) { while (true) {
if (count == 0) return true; if (count == 0) return true;
if (buffers_.empty()) return false; if (buffers_.empty()) return false;
@ -119,7 +123,9 @@ class TestZeroCopyInputStream final : public ZeroCopyInputStream {
// move them to `last_returned_buffer_`. It makes it simpler to keep track of // move them to `last_returned_buffer_`. It makes it simpler to keep track of
// the state of the object. The extra cost is not relevant for testing. // the state of the object. The extra cost is not relevant for testing.
std::deque<std::string> buffers_; std::deque<std::string> buffers_;
absl::optional<std::string> last_returned_buffer_; // absl::optional could work here, but std::unique_ptr makes it more likely
// for sanitizers to detect if the string is used after it is destroyed.
std::unique_ptr<std::string> last_returned_buffer_;
int64_t byte_count_ = 0; int64_t byte_count_ = 0;
}; };

@ -41,6 +41,7 @@ cc_test(
"//src/google/protobuf:cc_test_protos", "//src/google/protobuf:cc_test_protos",
"//src/google/protobuf:port_def", "//src/google/protobuf:port_def",
"//src/google/protobuf/io", "//src/google/protobuf/io",
"//src/google/protobuf/io:test_zero_copy_stream",
"//src/google/protobuf/util:json_format_cc_proto", "//src/google/protobuf/util:json_format_cc_proto",
"//src/google/protobuf/util:json_format_proto3_cc_proto", "//src/google/protobuf/util:json_format_proto3_cc_proto",
"//src/google/protobuf/util:type_resolver_util", "//src/google/protobuf/util:type_resolver_util",

@ -1294,7 +1294,7 @@ absl::Status ParseMessage(JsonLexer& lex, const Desc<Traits>& desc,
} }
} }
return ParseField<Traits>(lex, desc, name.value.AsView(), msg); return ParseField<Traits>(lex, desc, name.value.ToString(), msg);
}); });
} }
} // namespace } // namespace

@ -49,6 +49,7 @@
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "google/protobuf/descriptor_database.h" #include "google/protobuf/descriptor_database.h"
#include "google/protobuf/dynamic_message.h" #include "google/protobuf/dynamic_message.h"
#include "google/protobuf/io/test_zero_copy_stream.h"
#include "google/protobuf/io/zero_copy_stream.h" #include "google/protobuf/io/zero_copy_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl_lite.h" #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
#include "google/protobuf/util/json_format.pb.h" #include "google/protobuf/util/json_format.pb.h"
@ -73,6 +74,7 @@ using ::proto3::TestMap;
using ::proto3::TestMessage; using ::proto3::TestMessage;
using ::proto3::TestOneof; using ::proto3::TestOneof;
using ::proto3::TestWrapper; using ::proto3::TestWrapper;
using ::testing::ContainsRegex;
using ::testing::ElementsAre; using ::testing::ElementsAre;
using ::testing::IsEmpty; using ::testing::IsEmpty;
using ::testing::Not; using ::testing::Not;
@ -1332,6 +1334,24 @@ TEST_P(JsonTest, ClearPreExistingRepeatedInJsonValues) {
EXPECT_THAT(s.fields(), IsEmpty()); EXPECT_THAT(s.fields(), IsEmpty());
} }
TEST(JsonErrorTest, FieldNameAndSyntaxErrorInSeparateChunks) {
std::unique_ptr<TypeResolver> resolver{
google::protobuf::util::NewTypeResolverForDescriptorPool(
"type.googleapis.com", DescriptorPool::generated_pool())};
io::internal::TestZeroCopyInputStream input_stream(
{"{\"bool_value\":", "5}"});
std::string result;
io::StringOutputStream output_stream(&result);
absl::Status s = JsonToBinaryStream(
resolver.get(), "type.googleapis.com/proto3.TestMessage", &input_stream,
&output_stream, ParseOptions{});
ASSERT_FALSE(s.ok());
EXPECT_THAT(
s.message(),
ContainsRegex("invalid *JSON *in *type.googleapis.com/proto3.TestMessage "
"*@ *bool_value"));
}
} // namespace } // namespace
} // namespace json } // namespace json
} // namespace protobuf } // namespace protobuf

Loading…
Cancel
Save