1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
30
31 // Authors: wink@google.com (Wink Saville),
32 // kenton@google.com (Kenton Varda)
33 // Based on original Protocol Buffers design by
34 // Sanjay Ghemawat, Jeff Dean, and others.
35
36 #include <google/protobuf/message_lite.h>
37
38 #include <climits>
39 #include <cstdint>
40 #include <string>
41
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/parse_context.h>
45 #include <google/protobuf/io/coded_stream.h>
46 #include <google/protobuf/io/zero_copy_stream.h>
47 #include <google/protobuf/io/zero_copy_stream_impl.h>
48 #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
49 #include <google/protobuf/arena.h>
50 #include <google/protobuf/stubs/strutil.h>
51 #include <google/protobuf/generated_message_util.h>
52 #include <google/protobuf/repeated_field.h>
53 #include <google/protobuf/stubs/stl_util.h>
54 #include <google/protobuf/stubs/mutex.h>
55
56 // Must be included last.
57 #include <google/protobuf/port_def.inc>
58
59 namespace google {
60 namespace protobuf {
61
InitializationErrorString() const62 std::string MessageLite::InitializationErrorString() const {
63 return "(cannot determine missing fields for lite message)";
64 }
65
DebugString() const66 std::string MessageLite::DebugString() const {
67 std::uintptr_t address = reinterpret_cast<std::uintptr_t>(this);
68 return StrCat("MessageLite at 0x", strings::Hex(address));
69 }
70
71 namespace {
72
73 // When serializing, we first compute the byte size, then serialize the message.
74 // If serialization produces a different number of bytes than expected, we
75 // call this function, which crashes. The problem could be due to a bug in the
76 // protobuf implementation but is more likely caused by concurrent modification
77 // of the message. This function attempts to distinguish between the two and
78 // provide a useful error message.
ByteSizeConsistencyError(size_t byte_size_before_serialization,size_t byte_size_after_serialization,size_t bytes_produced_by_serialization,const MessageLite & message)79 void ByteSizeConsistencyError(size_t byte_size_before_serialization,
80 size_t byte_size_after_serialization,
81 size_t bytes_produced_by_serialization,
82 const MessageLite& message) {
83 GOOGLE_CHECK_EQ(byte_size_before_serialization, byte_size_after_serialization)
84 << message.GetTypeName()
85 << " was modified concurrently during serialization.";
86 GOOGLE_CHECK_EQ(bytes_produced_by_serialization, byte_size_before_serialization)
87 << "Byte size calculation and serialization were inconsistent. This "
88 "may indicate a bug in protocol buffers or it may be caused by "
89 "concurrent modification of "
90 << message.GetTypeName() << ".";
91 GOOGLE_LOG(FATAL) << "This shouldn't be called if all the sizes are equal.";
92 }
93
InitializationErrorMessage(const char * action,const MessageLite & message)94 std::string InitializationErrorMessage(const char* action,
95 const MessageLite& message) {
96 // Note: We want to avoid depending on strutil in the lite library, otherwise
97 // we'd use:
98 //
99 // return strings::Substitute(
100 // "Can't $0 message of type \"$1\" because it is missing required "
101 // "fields: $2",
102 // action, message.GetTypeName(),
103 // message.InitializationErrorString());
104
105 std::string result;
106 result += "Can't ";
107 result += action;
108 result += " message of type \"";
109 result += message.GetTypeName();
110 result += "\" because it is missing required fields: ";
111 result += message.InitializationErrorString();
112 return result;
113 }
114
as_string_view(const void * data,int size)115 inline StringPiece as_string_view(const void* data, int size) {
116 return StringPiece(static_cast<const char*>(data), size);
117 }
118
119 // Returns true of all required fields are present / have values.
CheckFieldPresence(const internal::ParseContext & ctx,const MessageLite & msg,MessageLite::ParseFlags parse_flags)120 inline bool CheckFieldPresence(const internal::ParseContext& ctx,
121 const MessageLite& msg,
122 MessageLite::ParseFlags parse_flags) {
123 (void)ctx; // Parameter is used by Google-internal code.
124 if (PROTOBUF_PREDICT_FALSE((parse_flags & MessageLite::kMergePartial) != 0)) {
125 return true;
126 }
127 return msg.IsInitializedWithErrors();
128 }
129
130 } // namespace
131
LogInitializationErrorMessage() const132 void MessageLite::LogInitializationErrorMessage() const {
133 GOOGLE_LOG(ERROR) << InitializationErrorMessage("parse", *this);
134 }
135
136 namespace internal {
137
138 template <bool aliasing>
MergeFromImpl(StringPiece input,MessageLite * msg,MessageLite::ParseFlags parse_flags)139 bool MergeFromImpl(StringPiece input, MessageLite* msg,
140 MessageLite::ParseFlags parse_flags) {
141 const char* ptr;
142 internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
143 aliasing, &ptr, input);
144 ptr = msg->_InternalParse(ptr, &ctx);
145 // ctx has an explicit limit set (length of string_view).
146 if (PROTOBUF_PREDICT_TRUE(ptr && ctx.EndedAtLimit())) {
147 return CheckFieldPresence(ctx, *msg, parse_flags);
148 }
149 return false;
150 }
151
152 template <bool aliasing>
MergeFromImpl(io::ZeroCopyInputStream * input,MessageLite * msg,MessageLite::ParseFlags parse_flags)153 bool MergeFromImpl(io::ZeroCopyInputStream* input, MessageLite* msg,
154 MessageLite::ParseFlags parse_flags) {
155 const char* ptr;
156 internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
157 aliasing, &ptr, input);
158 ptr = msg->_InternalParse(ptr, &ctx);
159 // ctx has no explicit limit (hence we end on end of stream)
160 if (PROTOBUF_PREDICT_TRUE(ptr && ctx.EndedAtEndOfStream())) {
161 return CheckFieldPresence(ctx, *msg, parse_flags);
162 }
163 return false;
164 }
165
166 template <bool aliasing>
MergeFromImpl(BoundedZCIS input,MessageLite * msg,MessageLite::ParseFlags parse_flags)167 bool MergeFromImpl(BoundedZCIS input, MessageLite* msg,
168 MessageLite::ParseFlags parse_flags) {
169 const char* ptr;
170 internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
171 aliasing, &ptr, input.zcis, input.limit);
172 ptr = msg->_InternalParse(ptr, &ctx);
173 if (PROTOBUF_PREDICT_FALSE(!ptr)) return false;
174 ctx.BackUp(ptr);
175 if (PROTOBUF_PREDICT_TRUE(ctx.EndedAtLimit())) {
176 return CheckFieldPresence(ctx, *msg, parse_flags);
177 }
178 return false;
179 }
180
181 template bool MergeFromImpl<false>(StringPiece input, MessageLite* msg,
182 MessageLite::ParseFlags parse_flags);
183 template bool MergeFromImpl<true>(StringPiece input, MessageLite* msg,
184 MessageLite::ParseFlags parse_flags);
185 template bool MergeFromImpl<false>(io::ZeroCopyInputStream* input,
186 MessageLite* msg,
187 MessageLite::ParseFlags parse_flags);
188 template bool MergeFromImpl<true>(io::ZeroCopyInputStream* input,
189 MessageLite* msg,
190 MessageLite::ParseFlags parse_flags);
191 template bool MergeFromImpl<false>(BoundedZCIS input, MessageLite* msg,
192 MessageLite::ParseFlags parse_flags);
193 template bool MergeFromImpl<true>(BoundedZCIS input, MessageLite* msg,
194 MessageLite::ParseFlags parse_flags);
195
196 } // namespace internal
197
198 class ZeroCopyCodedInputStream : public io::ZeroCopyInputStream {
199 public:
ZeroCopyCodedInputStream(io::CodedInputStream * cis)200 ZeroCopyCodedInputStream(io::CodedInputStream* cis) : cis_(cis) {}
Next(const void ** data,int * size)201 bool Next(const void** data, int* size) final {
202 if (!cis_->GetDirectBufferPointer(data, size)) return false;
203 cis_->Skip(*size);
204 return true;
205 }
BackUp(int count)206 void BackUp(int count) final { cis_->Advance(-count); }
Skip(int count)207 bool Skip(int count) final { return cis_->Skip(count); }
ByteCount() const208 int64_t ByteCount() const final { return 0; }
209
aliasing_enabled()210 bool aliasing_enabled() { return cis_->aliasing_enabled_; }
211
212 private:
213 io::CodedInputStream* cis_;
214 };
215
MergeFromImpl(io::CodedInputStream * input,MessageLite::ParseFlags parse_flags)216 bool MessageLite::MergeFromImpl(io::CodedInputStream* input,
217 MessageLite::ParseFlags parse_flags) {
218 ZeroCopyCodedInputStream zcis(input);
219 const char* ptr;
220 internal::ParseContext ctx(input->RecursionBudget(), zcis.aliasing_enabled(),
221 &ptr, &zcis);
222 // MergePartialFromCodedStream allows terminating the wireformat by 0 or
223 // end-group tag. Leaving it up to the caller to verify correct ending by
224 // calling LastTagWas on input. We need to maintain this behavior.
225 ctx.TrackCorrectEnding();
226 ctx.data().pool = input->GetExtensionPool();
227 ctx.data().factory = input->GetExtensionFactory();
228 ptr = _InternalParse(ptr, &ctx);
229 if (PROTOBUF_PREDICT_FALSE(!ptr)) return false;
230 ctx.BackUp(ptr);
231 if (!ctx.EndedAtEndOfStream()) {
232 GOOGLE_DCHECK_NE(ctx.LastTag(), 1); // We can't end on a pushed limit.
233 if (ctx.IsExceedingLimit(ptr)) return false;
234 input->SetLastTag(ctx.LastTag());
235 } else {
236 input->SetConsumed();
237 }
238 return CheckFieldPresence(ctx, *this, parse_flags);
239 }
240
MergePartialFromCodedStream(io::CodedInputStream * input)241 bool MessageLite::MergePartialFromCodedStream(io::CodedInputStream* input) {
242 return MergeFromImpl(input, kMergePartial);
243 }
244
MergeFromCodedStream(io::CodedInputStream * input)245 bool MessageLite::MergeFromCodedStream(io::CodedInputStream* input) {
246 return MergeFromImpl(input, kMerge);
247 }
248
ParseFromCodedStream(io::CodedInputStream * input)249 bool MessageLite::ParseFromCodedStream(io::CodedInputStream* input) {
250 Clear();
251 return MergeFromImpl(input, kParse);
252 }
253
ParsePartialFromCodedStream(io::CodedInputStream * input)254 bool MessageLite::ParsePartialFromCodedStream(io::CodedInputStream* input) {
255 Clear();
256 return MergeFromImpl(input, kParsePartial);
257 }
258
ParseFromZeroCopyStream(io::ZeroCopyInputStream * input)259 bool MessageLite::ParseFromZeroCopyStream(io::ZeroCopyInputStream* input) {
260 return ParseFrom<kParse>(input);
261 }
262
ParsePartialFromZeroCopyStream(io::ZeroCopyInputStream * input)263 bool MessageLite::ParsePartialFromZeroCopyStream(
264 io::ZeroCopyInputStream* input) {
265 return ParseFrom<kParsePartial>(input);
266 }
267
ParseFromFileDescriptor(int file_descriptor)268 bool MessageLite::ParseFromFileDescriptor(int file_descriptor) {
269 io::FileInputStream input(file_descriptor);
270 return ParseFromZeroCopyStream(&input) && input.GetErrno() == 0;
271 }
272
ParsePartialFromFileDescriptor(int file_descriptor)273 bool MessageLite::ParsePartialFromFileDescriptor(int file_descriptor) {
274 io::FileInputStream input(file_descriptor);
275 return ParsePartialFromZeroCopyStream(&input) && input.GetErrno() == 0;
276 }
277
ParseFromIstream(std::istream * input)278 bool MessageLite::ParseFromIstream(std::istream* input) {
279 io::IstreamInputStream zero_copy_input(input);
280 return ParseFromZeroCopyStream(&zero_copy_input) && input->eof();
281 }
282
ParsePartialFromIstream(std::istream * input)283 bool MessageLite::ParsePartialFromIstream(std::istream* input) {
284 io::IstreamInputStream zero_copy_input(input);
285 return ParsePartialFromZeroCopyStream(&zero_copy_input) && input->eof();
286 }
287
MergePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)288 bool MessageLite::MergePartialFromBoundedZeroCopyStream(
289 io::ZeroCopyInputStream* input, int size) {
290 return ParseFrom<kMergePartial>(internal::BoundedZCIS{input, size});
291 }
292
MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)293 bool MessageLite::MergeFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
294 int size) {
295 return ParseFrom<kMerge>(internal::BoundedZCIS{input, size});
296 }
297
ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)298 bool MessageLite::ParseFromBoundedZeroCopyStream(io::ZeroCopyInputStream* input,
299 int size) {
300 return ParseFrom<kParse>(internal::BoundedZCIS{input, size});
301 }
302
ParsePartialFromBoundedZeroCopyStream(io::ZeroCopyInputStream * input,int size)303 bool MessageLite::ParsePartialFromBoundedZeroCopyStream(
304 io::ZeroCopyInputStream* input, int size) {
305 return ParseFrom<kParsePartial>(internal::BoundedZCIS{input, size});
306 }
307
ParseFromString(ConstStringParam data)308 bool MessageLite::ParseFromString(ConstStringParam data) {
309 return ParseFrom<kParse>(data);
310 }
311
ParsePartialFromString(ConstStringParam data)312 bool MessageLite::ParsePartialFromString(ConstStringParam data) {
313 return ParseFrom<kParsePartial>(data);
314 }
315
ParseFromArray(const void * data,int size)316 bool MessageLite::ParseFromArray(const void* data, int size) {
317 return ParseFrom<kParse>(as_string_view(data, size));
318 }
319
ParsePartialFromArray(const void * data,int size)320 bool MessageLite::ParsePartialFromArray(const void* data, int size) {
321 return ParseFrom<kParsePartial>(as_string_view(data, size));
322 }
323
MergeFromString(ConstStringParam data)324 bool MessageLite::MergeFromString(ConstStringParam data) {
325 return ParseFrom<kMerge>(data);
326 }
327
328
329 // ===================================================================
330
SerializeToArrayImpl(const MessageLite & msg,uint8_t * target,int size)331 inline uint8_t* SerializeToArrayImpl(const MessageLite& msg, uint8_t* target,
332 int size) {
333 constexpr bool debug = false;
334 if (debug) {
335 // Force serialization to a stream with a block size of 1, which forces
336 // all writes to the stream to cross buffers triggering all fallback paths
337 // in the unittests when serializing to string / array.
338 io::ArrayOutputStream stream(target, size, 1);
339 uint8_t* ptr;
340 io::EpsCopyOutputStream out(
341 &stream, io::CodedOutputStream::IsDefaultSerializationDeterministic(),
342 &ptr);
343 ptr = msg._InternalSerialize(ptr, &out);
344 out.Trim(ptr);
345 GOOGLE_DCHECK(!out.HadError() && stream.ByteCount() == size);
346 return target + size;
347 } else {
348 io::EpsCopyOutputStream out(
349 target, size,
350 io::CodedOutputStream::IsDefaultSerializationDeterministic());
351 auto res = msg._InternalSerialize(target, &out);
352 GOOGLE_DCHECK(target + size == res);
353 return res;
354 }
355 }
356
SerializeWithCachedSizesToArray(uint8_t * target) const357 uint8_t* MessageLite::SerializeWithCachedSizesToArray(uint8_t* target) const {
358 // We only optimize this when using optimize_for = SPEED. In other cases
359 // we just use the CodedOutputStream path.
360 return SerializeToArrayImpl(*this, target, GetCachedSize());
361 }
362
SerializeToCodedStream(io::CodedOutputStream * output) const363 bool MessageLite::SerializeToCodedStream(io::CodedOutputStream* output) const {
364 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
365 return SerializePartialToCodedStream(output);
366 }
367
SerializePartialToCodedStream(io::CodedOutputStream * output) const368 bool MessageLite::SerializePartialToCodedStream(
369 io::CodedOutputStream* output) const {
370 const size_t size = ByteSizeLong(); // Force size to be cached.
371 if (size > INT_MAX) {
372 GOOGLE_LOG(ERROR) << GetTypeName()
373 << " exceeded maximum protobuf size of 2GB: " << size;
374 return false;
375 }
376
377 int original_byte_count = output->ByteCount();
378 SerializeWithCachedSizes(output);
379 if (output->HadError()) {
380 return false;
381 }
382 int final_byte_count = output->ByteCount();
383
384 if (final_byte_count - original_byte_count != static_cast<int64_t>(size)) {
385 ByteSizeConsistencyError(size, ByteSizeLong(),
386 final_byte_count - original_byte_count, *this);
387 }
388
389 return true;
390 }
391
SerializeToZeroCopyStream(io::ZeroCopyOutputStream * output) const392 bool MessageLite::SerializeToZeroCopyStream(
393 io::ZeroCopyOutputStream* output) const {
394 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
395 return SerializePartialToZeroCopyStream(output);
396 }
397
SerializePartialToZeroCopyStream(io::ZeroCopyOutputStream * output) const398 bool MessageLite::SerializePartialToZeroCopyStream(
399 io::ZeroCopyOutputStream* output) const {
400 const size_t size = ByteSizeLong(); // Force size to be cached.
401 if (size > INT_MAX) {
402 GOOGLE_LOG(ERROR) << GetTypeName()
403 << " exceeded maximum protobuf size of 2GB: " << size;
404 return false;
405 }
406
407 uint8_t* target;
408 io::EpsCopyOutputStream stream(
409 output, io::CodedOutputStream::IsDefaultSerializationDeterministic(),
410 &target);
411 target = _InternalSerialize(target, &stream);
412 stream.Trim(target);
413 if (stream.HadError()) return false;
414 return true;
415 }
416
SerializeToFileDescriptor(int file_descriptor) const417 bool MessageLite::SerializeToFileDescriptor(int file_descriptor) const {
418 io::FileOutputStream output(file_descriptor);
419 return SerializeToZeroCopyStream(&output) && output.Flush();
420 }
421
SerializePartialToFileDescriptor(int file_descriptor) const422 bool MessageLite::SerializePartialToFileDescriptor(int file_descriptor) const {
423 io::FileOutputStream output(file_descriptor);
424 return SerializePartialToZeroCopyStream(&output) && output.Flush();
425 }
426
SerializeToOstream(std::ostream * output) const427 bool MessageLite::SerializeToOstream(std::ostream* output) const {
428 {
429 io::OstreamOutputStream zero_copy_output(output);
430 if (!SerializeToZeroCopyStream(&zero_copy_output)) return false;
431 }
432 return output->good();
433 }
434
SerializePartialToOstream(std::ostream * output) const435 bool MessageLite::SerializePartialToOstream(std::ostream* output) const {
436 io::OstreamOutputStream zero_copy_output(output);
437 return SerializePartialToZeroCopyStream(&zero_copy_output);
438 }
439
AppendToString(std::string * output) const440 bool MessageLite::AppendToString(std::string* output) const {
441 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
442 return AppendPartialToString(output);
443 }
444
AppendPartialToString(std::string * output) const445 bool MessageLite::AppendPartialToString(std::string* output) const {
446 size_t old_size = output->size();
447 size_t byte_size = ByteSizeLong();
448 if (byte_size > INT_MAX) {
449 GOOGLE_LOG(ERROR) << GetTypeName()
450 << " exceeded maximum protobuf size of 2GB: " << byte_size;
451 return false;
452 }
453
454 STLStringResizeUninitializedAmortized(output, old_size + byte_size);
455 uint8_t* start =
456 reinterpret_cast<uint8_t*>(io::mutable_string_data(output) + old_size);
457 SerializeToArrayImpl(*this, start, byte_size);
458 return true;
459 }
460
SerializeToString(std::string * output) const461 bool MessageLite::SerializeToString(std::string* output) const {
462 output->clear();
463 return AppendToString(output);
464 }
465
SerializePartialToString(std::string * output) const466 bool MessageLite::SerializePartialToString(std::string* output) const {
467 output->clear();
468 return AppendPartialToString(output);
469 }
470
SerializeToArray(void * data,int size) const471 bool MessageLite::SerializeToArray(void* data, int size) const {
472 GOOGLE_DCHECK(IsInitialized()) << InitializationErrorMessage("serialize", *this);
473 return SerializePartialToArray(data, size);
474 }
475
SerializePartialToArray(void * data,int size) const476 bool MessageLite::SerializePartialToArray(void* data, int size) const {
477 const size_t byte_size = ByteSizeLong();
478 if (byte_size > INT_MAX) {
479 GOOGLE_LOG(ERROR) << GetTypeName()
480 << " exceeded maximum protobuf size of 2GB: " << byte_size;
481 return false;
482 }
483 if (size < static_cast<int64_t>(byte_size)) return false;
484 uint8_t* start = reinterpret_cast<uint8_t*>(data);
485 SerializeToArrayImpl(*this, start, byte_size);
486 return true;
487 }
488
SerializeAsString() const489 std::string MessageLite::SerializeAsString() const {
490 // If the compiler implements the (Named) Return Value Optimization,
491 // the local variable 'output' will not actually reside on the stack
492 // of this function, but will be overlaid with the object that the
493 // caller supplied for the return value to be constructed in.
494 std::string output;
495 if (!AppendToString(&output)) output.clear();
496 return output;
497 }
498
SerializePartialAsString() const499 std::string MessageLite::SerializePartialAsString() const {
500 std::string output;
501 if (!AppendPartialToString(&output)) output.clear();
502 return output;
503 }
504
505
506 namespace internal {
507
NewFromPrototypeHelper(const MessageLite * prototype,Arena * arena)508 MessageLite* NewFromPrototypeHelper(const MessageLite* prototype,
509 Arena* arena) {
510 return prototype->New(arena);
511 }
512 template <>
Merge(const MessageLite & from,MessageLite * to)513 void GenericTypeHandler<MessageLite>::Merge(const MessageLite& from,
514 MessageLite* to) {
515 to->CheckTypeAndMergeFrom(from);
516 }
517 template <>
Merge(const std::string & from,std::string * to)518 void GenericTypeHandler<std::string>::Merge(const std::string& from,
519 std::string* to) {
520 *to = from;
521 }
522
523 // Non-inline implementations of InternalMetadata routines
524 #if defined(NDEBUG) || defined(_MSC_VER)
525 // for opt and MSVC builds, the destructor is defined in the header.
526 #else
527 // This is moved out of the header because the GOOGLE_DCHECK produces a lot of code.
~InternalMetadata()528 InternalMetadata::~InternalMetadata() {
529 if (HasMessageOwnedArenaTag()) {
530 GOOGLE_DCHECK(!HasUnknownFieldsTag());
531 delete reinterpret_cast<Arena*>(ptr_ - kMessageOwnedArenaTagMask);
532 }
533 }
534 #endif
535
536 // Non-inline variants of std::string specializations for
537 // various InternalMetadata routines.
538 template <>
DoClear()539 void InternalMetadata::DoClear<std::string>() {
540 mutable_unknown_fields<std::string>()->clear();
541 }
542
543 template <>
DoMergeFrom(const std::string & other)544 void InternalMetadata::DoMergeFrom<std::string>(const std::string& other) {
545 mutable_unknown_fields<std::string>()->append(other);
546 }
547
548 template <>
DoSwap(std::string * other)549 void InternalMetadata::DoSwap<std::string>(std::string* other) {
550 mutable_unknown_fields<std::string>()->swap(*other);
551 }
552
553 } // namespace internal
554
555
556 // ===================================================================
557 // Shutdown support.
558
559 namespace internal {
560
561 struct ShutdownData {
~ShutdownDatagoogle::protobuf::internal::ShutdownData562 ~ShutdownData() {
563 std::reverse(functions.begin(), functions.end());
564 for (auto pair : functions) pair.first(pair.second);
565 }
566
getgoogle::protobuf::internal::ShutdownData567 static ShutdownData* get() {
568 static auto* data = new ShutdownData;
569 return data;
570 }
571
572 std::vector<std::pair<void (*)(const void*), const void*>> functions;
573 Mutex mutex;
574 };
575
RunZeroArgFunc(const void * arg)576 static void RunZeroArgFunc(const void* arg) {
577 void (*func)() = reinterpret_cast<void (*)()>(const_cast<void*>(arg));
578 func();
579 }
580
OnShutdown(void (* func)())581 void OnShutdown(void (*func)()) {
582 OnShutdownRun(RunZeroArgFunc, reinterpret_cast<void*>(func));
583 }
584
OnShutdownRun(void (* f)(const void *),const void * arg)585 void OnShutdownRun(void (*f)(const void*), const void* arg) {
586 auto shutdown_data = ShutdownData::get();
587 MutexLock lock(&shutdown_data->mutex);
588 shutdown_data->functions.push_back(std::make_pair(f, arg));
589 }
590
591 } // namespace internal
592
ShutdownProtobufLibrary()593 void ShutdownProtobufLibrary() {
594 // This function should be called only once, but accepts multiple calls.
595 static bool is_shutdown = false;
596 if (!is_shutdown) {
597 delete internal::ShutdownData::get();
598 is_shutdown = true;
599 }
600 }
601
602
603 } // namespace protobuf
604 } // namespace google
605
606 #include <google/protobuf/port_undef.inc>
607