Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(748)

Side by Side Diff: third_party/protobuf/src/google/protobuf/message_lite.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 15 matching lines...) Expand all
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 30
31 // Authors: wink@google.com (Wink Saville), 31 // Authors: wink@google.com (Wink Saville),
32 // kenton@google.com (Kenton Varda) 32 // kenton@google.com (Kenton Varda)
33 // Based on original Protocol Buffers design by 33 // Based on original Protocol Buffers design by
34 // Sanjay Ghemawat, Jeff Dean, and others. 34 // Sanjay Ghemawat, Jeff Dean, and others.
35 35
36 #include <google/protobuf/arena.h>
37 #include <google/protobuf/generated_message_util.h>
36 #include <google/protobuf/message_lite.h> 38 #include <google/protobuf/message_lite.h>
37 #include <google/protobuf/arena.h>
38 #include <google/protobuf/repeated_field.h> 39 #include <google/protobuf/repeated_field.h>
39 #include <string> 40 #include <string>
40 #include <google/protobuf/stubs/logging.h> 41 #include <google/protobuf/stubs/logging.h>
41 #include <google/protobuf/stubs/common.h> 42 #include <google/protobuf/stubs/common.h>
42 #include <google/protobuf/io/coded_stream.h> 43 #include <google/protobuf/io/coded_stream.h>
43 #include <google/protobuf/io/zero_copy_stream_impl_lite.h> 44 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
44 #include <google/protobuf/stubs/stl_util.h> 45 #include <google/protobuf/stubs/stl_util.h>
45 46
46 namespace google { 47 namespace google {
47 namespace protobuf { 48 namespace protobuf {
48 49
49 MessageLite::~MessageLite() {}
50
51 string MessageLite::InitializationErrorString() const { 50 string MessageLite::InitializationErrorString() const {
52 return "(cannot determine missing fields for lite message)"; 51 return "(cannot determine missing fields for lite message)";
53 } 52 }
54 53
55 namespace { 54 namespace {
56 55
57 // When serializing, we first compute the byte size, then serialize the message. 56 // When serializing, we first compute the byte size, then serialize the message.
58 // If serialization produces a different number of bytes than expected, we 57 // If serialization produces a different number of bytes than expected, we
59 // call this function, which crashes. The problem could be due to a bug in the 58 // call this function, which crashes. The problem could be due to a bug in the
60 // protobuf implementation but is more likely caused by concurrent modification 59 // protobuf implementation but is more likely caused by concurrent modification
61 // of the message. This function attempts to distinguish between the two and 60 // of the message. This function attempts to distinguish between the two and
62 // provide a useful error message. 61 // provide a useful error message.
63 void ByteSizeConsistencyError(int byte_size_before_serialization, 62 void ByteSizeConsistencyError(size_t byte_size_before_serialization,
64 int byte_size_after_serialization, 63 size_t byte_size_after_serialization,
65 int bytes_produced_by_serialization, 64 size_t bytes_produced_by_serialization,
66 const MessageLite& message) { 65 const MessageLite& message) {
67 GOOGLE_CHECK_EQ(byte_size_before_serialization, byte_size_after_serialization) 66 GOOGLE_CHECK_EQ(byte_size_before_serialization, byte_size_after_serialization)
68 << message.GetTypeName() 67 << message.GetTypeName()
69 << " was modified concurrently during serialization."; 68 << " was modified concurrently during serialization.";
70 GOOGLE_CHECK_EQ(bytes_produced_by_serialization, byte_size_before_serializatio n) 69 GOOGLE_CHECK_EQ(bytes_produced_by_serialization, byte_size_before_serializatio n)
71 << "Byte size calculation and serialization were inconsistent. This " 70 << "Byte size calculation and serialization were inconsistent. This "
72 "may indicate a bug in protocol buffers or it may be caused by " 71 "may indicate a bug in protocol buffers or it may be caused by "
73 "concurrent modification of " << message.GetTypeName() << "."; 72 "concurrent modification of " << message.GetTypeName() << ".";
74 GOOGLE_LOG(FATAL) << "This shouldn't be called if all the sizes are equal."; 73 GOOGLE_LOG(FATAL) << "This shouldn't be called if all the sizes are equal.";
75 } 74 }
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 return InlineParseFromArray(data, size, this); 213 return InlineParseFromArray(data, size, this);
215 } 214 }
216 215
217 bool MessageLite::ParsePartialFromArray(const void* data, int size) { 216 bool MessageLite::ParsePartialFromArray(const void* data, int size) {
218 return InlineParsePartialFromArray(data, size, this); 217 return InlineParsePartialFromArray(data, size, this);
219 } 218 }
220 219
221 220
222 // =================================================================== 221 // ===================================================================
223 222
224 uint8* MessageLite::SerializeWithCachedSizesToArray(uint8* target) const { 223 uint8* MessageLite::InternalSerializeWithCachedSizesToArray(
224 bool deterministic, uint8* target) const {
225 // We only optimize this when using optimize_for = SPEED. In other cases 225 // We only optimize this when using optimize_for = SPEED. In other cases
226 // we just use the CodedOutputStream path. 226 // we just use the CodedOutputStream path.
227 int size = GetCachedSize(); 227 int size = GetCachedSize();
228 io::ArrayOutputStream out(target, size); 228 io::ArrayOutputStream out(target, size);
229 io::CodedOutputStream coded_out(&out); 229 io::CodedOutputStream coded_out(&out);
230 SerializeWithCachedSizes(&coded_out); 230 SerializeWithCachedSizes(&coded_out);
231 GOOGLE_CHECK(!coded_out.HadError()); 231 GOOGLE_CHECK(!coded_out.HadError());
232 return target + size; 232 return target + size;
233 } 233 }
234 234
235 bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) const { 235 bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) const {
236 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *thi s); 236 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *thi s);
237 return SerializePartialToCodedStream(output); 237 return SerializePartialToCodedStream(output);
238 } 238 }
239 239
240 size_t MessageLite::ByteSizeLong() const {
241 return internal::FromIntSize(ByteSize());
242 }
243
240 bool MessageLite::SerializePartialToCodedStream( 244 bool MessageLite::SerializePartialToCodedStream(
241 io::CodedOutputStream* output) const { 245 io::CodedOutputStream* output) const {
242 const int size = ByteSize(); // Force size to be cached. 246 const size_t size = ByteSizeLong(); // Force size to be cached.
243 if (size < 0) { 247 if (size > INT_MAX) {
244 // Messages >2G cannot be serialized due to overflow computing ByteSize. 248 GOOGLE_LOG(ERROR) << "Exceeded maximum protobuf size of 2GB.";
245 GOOGLE_LOG(ERROR) << "Error computing ByteSize (possible overflow?).";
246 return false; 249 return false;
247 } 250 }
248 251
249 uint8* buffer = output->GetDirectBufferForNBytesAndAdvance(size); 252 uint8* buffer = output->GetDirectBufferForNBytesAndAdvance(size);
250 if (buffer != NULL) { 253 if (buffer != NULL) {
251 uint8* end = SerializeWithCachedSizesToArray(buffer); 254 uint8* end = SerializeWithCachedSizesToArray(buffer);
252 if (end - buffer != size) { 255 if (end - buffer != size) {
253 ByteSizeConsistencyError(size, ByteSize(), end - buffer, *this); 256 ByteSizeConsistencyError(size, ByteSizeLong(), end - buffer, *this);
254 } 257 }
255 return true; 258 return true;
256 } else { 259 } else {
257 int original_byte_count = output->ByteCount(); 260 int original_byte_count = output->ByteCount();
258 SerializeWithCachedSizes(output); 261 SerializeWithCachedSizes(output);
259 if (output->HadError()) { 262 if (output->HadError()) {
260 return false; 263 return false;
261 } 264 }
262 int final_byte_count = output->ByteCount(); 265 int final_byte_count = output->ByteCount();
263 266
264 if (final_byte_count - original_byte_count != size) { 267 if (final_byte_count - original_byte_count != size) {
265 ByteSizeConsistencyError(size, ByteSize(), 268 ByteSizeConsistencyError(size, ByteSizeLong(),
266 final_byte_count - original_byte_count, *this); 269 final_byte_count - original_byte_count, *this);
267 } 270 }
268 271
269 return true; 272 return true;
270 } 273 }
271 } 274 }
272 275
273 bool MessageLite::SerializeToZeroCopyStream( 276 bool MessageLite::SerializeToZeroCopyStream(
274 io::ZeroCopyOutputStream* output) const { 277 io::ZeroCopyOutputStream* output) const {
275 io::CodedOutputStream encoder(output); 278 io::CodedOutputStream encoder(output);
276 return SerializeToCodedStream(&encoder); 279 return SerializeToCodedStream(&encoder);
277 } 280 }
278 281
279 bool MessageLite::SerializePartialToZeroCopyStream( 282 bool MessageLite::SerializePartialToZeroCopyStream(
280 io::ZeroCopyOutputStream* output) const { 283 io::ZeroCopyOutputStream* output) const {
281 io::CodedOutputStream encoder(output); 284 io::CodedOutputStream encoder(output);
282 return SerializePartialToCodedStream(&encoder); 285 return SerializePartialToCodedStream(&encoder);
283 } 286 }
284 287
285 bool MessageLite::AppendToString(string* output) const { 288 bool MessageLite::AppendToString(string* output) const {
286 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *thi s); 289 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *thi s);
287 return AppendPartialToString(output); 290 return AppendPartialToString(output);
288 } 291 }
289 292
290 bool MessageLite::AppendPartialToString(string* output) const { 293 bool MessageLite::AppendPartialToString(string* output) const {
291 int old_size = output->size(); 294 size_t old_size = output->size();
292 int byte_size = ByteSize(); 295 size_t byte_size = ByteSizeLong();
293 if (byte_size < 0) { 296 if (byte_size > INT_MAX) {
294 // Messages >2G cannot be serialized due to overflow computing ByteSize. 297 GOOGLE_LOG(ERROR) << "Exceeded maximum protobuf size of 2GB.";
295 GOOGLE_LOG(ERROR) << "Error computing ByteSize (possible overflow?).";
296 return false; 298 return false;
297 } 299 }
298 300
299 STLStringResizeUninitialized(output, old_size + byte_size); 301 STLStringResizeUninitialized(output, old_size + byte_size);
300 uint8* start = 302 uint8* start =
301 reinterpret_cast<uint8*>(io::mutable_string_data(output) + old_size); 303 reinterpret_cast<uint8*>(io::mutable_string_data(output) + old_size);
302 uint8* end = SerializeWithCachedSizesToArray(start); 304 uint8* end = SerializeWithCachedSizesToArray(start);
303 if (end - start != byte_size) { 305 if (end - start != byte_size) {
304 ByteSizeConsistencyError(byte_size, ByteSize(), end - start, *this); 306 ByteSizeConsistencyError(byte_size, ByteSizeLong(), end - start, *this);
305 } 307 }
306 return true; 308 return true;
307 } 309 }
308 310
309 bool MessageLite::SerializeToString(string* output) const { 311 bool MessageLite::SerializeToString(string* output) const {
310 output->clear(); 312 output->clear();
311 return AppendToString(output); 313 return AppendToString(output);
312 } 314 }
313 315
314 bool MessageLite::SerializePartialToString(string* output) const { 316 bool MessageLite::SerializePartialToString(string* output) const {
315 output->clear(); 317 output->clear();
316 return AppendPartialToString(output); 318 return AppendPartialToString(output);
317 } 319 }
318 320
319 bool MessageLite::SerializeToArray(void* data, int size) const { 321 bool MessageLite::SerializeToArray(void* data, int size) const {
320 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *thi s); 322 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *thi s);
321 return SerializePartialToArray(data, size); 323 return SerializePartialToArray(data, size);
322 } 324 }
323 325
324 bool MessageLite::SerializePartialToArray(void* data, int size) const { 326 bool MessageLite::SerializePartialToArray(void* data, int size) const {
325 int byte_size = ByteSize(); 327 int byte_size = ByteSizeLong();
326 if (size < byte_size) return false; 328 if (size < byte_size) return false;
327 uint8* start = reinterpret_cast<uint8*>(data); 329 uint8* start = reinterpret_cast<uint8*>(data);
328 uint8* end = SerializeWithCachedSizesToArray(start); 330 uint8* end = SerializeWithCachedSizesToArray(start);
329 if (end - start != byte_size) { 331 if (end - start != byte_size) {
330 ByteSizeConsistencyError(byte_size, ByteSize(), end - start, *this); 332 ByteSizeConsistencyError(byte_size, ByteSizeLong(), end - start, *this);
331 } 333 }
332 return true; 334 return true;
333 } 335 }
334 336
335 string MessageLite::SerializeAsString() const { 337 string MessageLite::SerializeAsString() const {
336 // If the compiler implements the (Named) Return Value Optimization, 338 // If the compiler implements the (Named) Return Value Optimization,
337 // the local variable 'output' will not actually reside on the stack 339 // the local variable 'output' will not actually reside on the stack
338 // of this function, but will be overlaid with the object that the 340 // of this function, but will be overlaid with the object that the
339 // caller supplied for the return value to be constructed in. 341 // caller supplied for the return value to be constructed in.
340 string output; 342 string output;
(...skipping 22 matching lines...) Expand all
363 } 365 }
364 template<> 366 template<>
365 void GenericTypeHandler<string>::Merge(const string& from, 367 void GenericTypeHandler<string>::Merge(const string& from,
366 string* to) { 368 string* to) {
367 *to = from; 369 *to = from;
368 } 370 }
369 } // namespace internal 371 } // namespace internal
370 372
371 } // namespace protobuf 373 } // namespace protobuf
372 } // namespace google 374 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698