1+ From b955165ebdcc5a8ba9c267230d6305f4e3d9c118 Mon Sep 17 00:00:00 2001
2+ From: Adam Cozzette <
[email protected] >
3+ Date: Fri, 13 Oct 2023 15:20:54 -0700
4+ Subject: [PATCH] Internal change
5+
6+ PiperOrigin-RevId: 573332237
7+ ---
8+ .../protobuf/io/test_zero_copy_stream.h | 22 ++++++++++++-------
9+ src/google/protobuf/json/BUILD.bazel | 1 +
10+ src/google/protobuf/json/internal/parser.cc | 2 +-
11+ src/google/protobuf/json/json_test.cc | 20 +++++++++++++++++
12+ 4 files changed, 36 insertions(+), 9 deletions(-)
13+
14+ diff --git a/src/google/protobuf/io/test_zero_copy_stream.h b/src/google/protobuf/io/test_zero_copy_stream.h
15+ index 4c5a06db400e..1a56d7038c96 100644
16+ --- a/extra/protobuf/protobuf-24.4/src/google/protobuf/io/test_zero_copy_stream.h
17+ +++ b/extra/protobuf/protobuf-24.4/src/google/protobuf/io/test_zero_copy_stream.h
18+ @@ -9,12 +9,12 @@
19+ #define GOOGLE_PROTOBUF_IO_TEST_ZERO_COPY_STREAM_H__
20+
21+ #include <deque>
22+ + #include <memory>
23+ #include <string>
24+ #include <utility>
25+ #include <vector>
26+
27+ #include "absl/log/absl_check.h"
28+ - #include "absl/types/optional.h"
29+ #include "google/protobuf/io/zero_copy_stream.h"
30+
31+ // Must be included last.
32+ @@ -37,18 +37,22 @@ class TestZeroCopyInputStream final : public ZeroCopyInputStream {
33+ TestZeroCopyInputStream(const TestZeroCopyInputStream& other)
34+ : ZeroCopyInputStream(),
35+ buffers_(other.buffers_),
36+ - last_returned_buffer_(other.last_returned_buffer_),
37+ + last_returned_buffer_(
38+ + other.last_returned_buffer_
39+ + ? std::make_unique<std::string>(*other.last_returned_buffer_)
40+ + : nullptr),
41+ byte_count_(other.byte_count_) {}
42+
43+ bool Next(const void** data, int* size) override {
44+ ABSL_CHECK(data) << "data must not be null";
45+ ABSL_CHECK(size) << "size must not be null";
46+ - last_returned_buffer_ = absl::nullopt;
47+ + last_returned_buffer_ = nullptr;
48+
49+ // We are done
50+ if (buffers_.empty()) return false;
51+
52+ - last_returned_buffer_ = std::move(buffers_.front());
53+ + last_returned_buffer_ =
54+ + std::make_unique<std::string>(std::move(buffers_.front()));
55+ buffers_.pop_front();
56+ *data = last_returned_buffer_->data();
57+ *size = static_cast<int>(last_returned_buffer_->size());
58+ @@ -58,19 +62,19 @@ class TestZeroCopyInputStream final : public ZeroCopyInputStream {
59+
60+ void BackUp(int count) override {
61+ ABSL_CHECK_GE(count, 0) << "count must not be negative";
62+ - ABSL_CHECK(last_returned_buffer_.has_value())
63+ + ABSL_CHECK(last_returned_buffer_ != nullptr)
64+ << "The last call was not a successful Next()";
65+ ABSL_CHECK_LE(count, last_returned_buffer_->size())
66+ << "count must be within bounds of last buffer";
67+ buffers_.push_front(
68+ last_returned_buffer_->substr(last_returned_buffer_->size() - count));
69+ - last_returned_buffer_ = absl::nullopt;
70+ + last_returned_buffer_ = nullptr;
71+ byte_count_ -= count;
72+ }
73+
74+ bool Skip(int count) override {
75+ ABSL_CHECK_GE(count, 0) << "count must not be negative";
76+ - last_returned_buffer_ = absl::nullopt;
77+ + last_returned_buffer_ = nullptr;
78+ while (true) {
79+ if (count == 0) return true;
80+ if (buffers_.empty()) return false;
81+ @@ -96,7 +100,9 @@ class TestZeroCopyInputStream final : public ZeroCopyInputStream {
82+ // move them to `last_returned_buffer_`. It makes it simpler to keep track of
83+ // the state of the object. The extra cost is not relevant for testing.
84+ std::deque<std::string> buffers_;
85+ - absl::optional<std::string> last_returned_buffer_;
86+ + // absl::optional could work here, but std::unique_ptr makes it more likely
87+ + // for sanitizers to detect if the string is used after it is destroyed.
88+ + std::unique_ptr<std::string> last_returned_buffer_;
89+ int64_t byte_count_ = 0;
90+ };
91+
92+ diff --git a/src/google/protobuf/json/BUILD.bazel b/src/google/protobuf/json/BUILD.bazel
93+ index dece74e4d0f0..6ec8184e0e09 100644
94+ --- a/extra/protobuf/protobuf-24.4/src/google/protobuf/json/BUILD.bazel
95+ +++ b/extra/protobuf/protobuf-24.4/src/google/protobuf/json/BUILD.bazel
96+ @@ -41,6 +41,7 @@ cc_test(
97+ "//src/google/protobuf:cc_test_protos",
98+ "//src/google/protobuf:port_def",
99+ "//src/google/protobuf/io",
100+ + "//src/google/protobuf/io:test_zero_copy_stream",
101+ "//src/google/protobuf/util:json_format_cc_proto",
102+ "//src/google/protobuf/util:json_format_proto3_cc_proto",
103+ "//src/google/protobuf/util:type_resolver_util",
104+ diff --git a/src/google/protobuf/json/internal/parser.cc b/src/google/protobuf/json/internal/parser.cc
105+ index 17e8fcc07c42..fbf492afa715 100644
106+ --- a/extra/protobuf/protobuf-24.4/src/google/protobuf/json/internal/parser.cc
107+ +++ b/extra/protobuf/protobuf-24.4/src/google/protobuf/json/internal/parser.cc
108+ @@ -1273,7 +1273,7 @@ absl::Status ParseMessage(JsonLexer& lex, const Desc<Traits>& desc,
109+ }
110+ }
111+
112+ - return ParseField<Traits>(lex, desc, name.value.AsView(), msg);
113+ + return ParseField<Traits>(lex, desc, name.value.ToString(), msg);
114+ });
115+ }
116+ } // namespace
117+ diff --git a/src/google/protobuf/json/json_test.cc b/src/google/protobuf/json/json_test.cc
118+ index 48379ceeb5f9..2ff1e87a90fe 100644
119+ --- a/extra/protobuf/protobuf-24.4/src/google/protobuf/json/json_test.cc
120+ +++ b/extra/protobuf/protobuf-24.4/src/google/protobuf/json/json_test.cc
121+ @@ -26,6 +26,7 @@
122+ #include "absl/strings/string_view.h"
123+ #include "google/protobuf/descriptor_database.h"
124+ #include "google/protobuf/dynamic_message.h"
125+ + #include "google/protobuf/io/test_zero_copy_stream.h"
126+ #include "google/protobuf/io/zero_copy_stream.h"
127+ #include "google/protobuf/io/zero_copy_stream_impl_lite.h"
128+ #include "google/protobuf/util/json_format.pb.h"
129+ @@ -50,6 +51,7 @@ using ::proto3::TestMap;
130+ using ::proto3::TestMessage;
131+ using ::proto3::TestOneof;
132+ using ::proto3::TestWrapper;
133+ + using ::testing::ContainsRegex;
134+ using ::testing::ElementsAre;
135+ using ::testing::IsEmpty;
136+ using ::testing::Not;
137+ @@ -1331,6 +1333,24 @@ TEST_P(JsonTest, ClearPreExistingRepeatedInJsonValues) {
138+ EXPECT_THAT(s.fields(), IsEmpty());
139+ }
140+
141+ + TEST(JsonErrorTest, FieldNameAndSyntaxErrorInSeparateChunks) {
142+ + std::unique_ptr<TypeResolver> resolver{
143+ + google::protobuf::util::NewTypeResolverForDescriptorPool(
144+ + "type.googleapis.com", DescriptorPool::generated_pool())};
145+ + io::internal::TestZeroCopyInputStream input_stream(
146+ + {"{\"bool_value\":", "5}"});
147+ + std::string result;
148+ + io::StringOutputStream output_stream(&result);
149+ + absl::Status s = JsonToBinaryStream(
150+ + resolver.get(), "type.googleapis.com/proto3.TestMessage", &input_stream,
151+ + &output_stream, ParseOptions{});
152+ + ASSERT_FALSE(s.ok());
153+ + EXPECT_THAT(
154+ + s.message(),
155+ + ContainsRegex("invalid *JSON *in *type.googleapis.com/proto3.TestMessage "
156+ + "*@ *bool_value"));
157+ + }
158+ +
159+ } // namespace
160+ } // namespace json
161+ } // namespace protobuf
162+
0 commit comments