From 5a76e633ea9b5adb215e93fdc11e1c0c08b3fc74 Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Thu, 17 Nov 2016 16:48:38 -0800 Subject: Integrated internal changes from Google --- src/google/protobuf/io/coded_stream.cc | 146 ++++++------------------ src/google/protobuf/io/coded_stream.h | 125 ++++++++++++++------ src/google/protobuf/io/coded_stream_unittest.cc | 82 ++++++++++--- src/google/protobuf/io/gzip_stream.cc | 2 +- src/google/protobuf/io/printer.cc | 36 +++--- src/google/protobuf/io/printer.h | 18 +-- src/google/protobuf/io/printer_unittest.cc | 30 ++--- src/google/protobuf/io/tokenizer.cc | 6 +- src/google/protobuf/io/tokenizer.h | 2 +- src/google/protobuf/io/tokenizer_unittest.cc | 2 +- 10 files changed, 238 insertions(+), 211 deletions(-) (limited to 'src/google/protobuf/io') diff --git a/src/google/protobuf/io/coded_stream.cc b/src/google/protobuf/io/coded_stream.cc index 08394ca7..93748ee3 100644 --- a/src/google/protobuf/io/coded_stream.cc +++ b/src/google/protobuf/io/coded_stream.cc @@ -47,7 +47,6 @@ #include #include #include -#include namespace google { @@ -550,9 +549,15 @@ bool CodedInputStream::ReadVarint64Slow(uint64* value) { uint32 b; do { - if (count == kMaxVarintBytes) return false; + if (count == kMaxVarintBytes) { + *value = 0; + return false; + } while (buffer_ == buffer_end_) { - if (!Refresh()) return false; + if (!Refresh()) { + *value = 0; + return false; + } } b = *buffer_; result |= static_cast(b & 0x7F) << (7 * count); @@ -602,13 +607,13 @@ bool CodedInputStream::Refresh() { if (total_bytes_warning_threshold_ >= 0 && total_bytes_read_ >= total_bytes_warning_threshold_) { - GOOGLE_LOG(WARNING) << "Reading dangerously large protocol message. If the " - "message turns out to be larger than " - << total_bytes_limit_ << " bytes, parsing will be halted " - "for security reasons. To increase the limit (or to " - "disable these warnings), see " - "CodedInputStream::SetTotalBytesLimit() in " - "google/protobuf/io/coded_stream.h."; + GOOGLE_LOG(INFO) << "Reading dangerously large protocol message. If the " + "message turns out to be larger than " + << total_bytes_limit_ << " bytes, parsing will be halted " + "for security reasons. To increase the limit (or to " + "disable these warnings), see " + "CodedInputStream::SetTotalBytesLimit() in " + "google/protobuf/io/coded_stream.h."; // Don't warn again for this stream, and print total size at the end. total_bytes_warning_threshold_ = -2; @@ -789,104 +794,12 @@ void CodedOutputStream::WriteVarint32SlowPath(uint32 value) { WriteRaw(bytes, size); } -inline uint8* CodedOutputStream::WriteVarint64ToArrayInline( - uint64 value, uint8* target) { - // Splitting into 32-bit pieces gives better performance on 32-bit - // processors. - uint32 part0 = static_cast(value ); - uint32 part1 = static_cast(value >> 28); - uint32 part2 = static_cast(value >> 56); - - int size; - - // Here we can't really optimize for small numbers, since the value is - // split into three parts. Cheking for numbers < 128, for instance, - // would require three comparisons, since you'd have to make sure part1 - // and part2 are zero. However, if the caller is using 64-bit integers, - // it is likely that they expect the numbers to often be very large, so - // we probably don't want to optimize for small numbers anyway. Thus, - // we end up with a hardcoded binary search tree... - if (part2 == 0) { - if (part1 == 0) { - if (part0 < (1 << 14)) { - if (part0 < (1 << 7)) { - size = 1; goto size1; - } else { - size = 2; goto size2; - } - } else { - if (part0 < (1 << 21)) { - size = 3; goto size3; - } else { - size = 4; goto size4; - } - } - } else { - if (part1 < (1 << 14)) { - if (part1 < (1 << 7)) { - size = 5; goto size5; - } else { - size = 6; goto size6; - } - } else { - if (part1 < (1 << 21)) { - size = 7; goto size7; - } else { - size = 8; goto size8; - } - } - } - } else { - if (part2 < (1 << 7)) { - size = 9; goto size9; - } else { - size = 10; goto size10; - } - } - - GOOGLE_LOG(FATAL) << "Can't get here."; - - size10: target[9] = static_cast((part2 >> 7) | 0x80); - size9 : target[8] = static_cast((part2 ) | 0x80); - size8 : target[7] = static_cast((part1 >> 21) | 0x80); - size7 : target[6] = static_cast((part1 >> 14) | 0x80); - size6 : target[5] = static_cast((part1 >> 7) | 0x80); - size5 : target[4] = static_cast((part1 ) | 0x80); - size4 : target[3] = static_cast((part0 >> 21) | 0x80); - size3 : target[2] = static_cast((part0 >> 14) | 0x80); - size2 : target[1] = static_cast((part0 >> 7) | 0x80); - size1 : target[0] = static_cast((part0 ) | 0x80); - - target[size-1] &= 0x7F; - return target + size; -} - -void CodedOutputStream::WriteVarint64(uint64 value) { - if (buffer_size_ >= kMaxVarintBytes) { - // Fast path: We have enough bytes left in the buffer to guarantee that - // this write won't cross the end, so we can skip the checks. - uint8* target = buffer_; - - uint8* end = WriteVarint64ToArrayInline(value, target); - int size = end - target; - Advance(size); - } else { - // Slow path: This write might cross the end of the buffer, so we - // compose the bytes first then use WriteRaw(). - uint8 bytes[kMaxVarintBytes]; - int size = 0; - while (value > 0x7F) { - bytes[size++] = (static_cast(value) & 0x7F) | 0x80; - value >>= 7; - } - bytes[size++] = static_cast(value) & 0x7F; - WriteRaw(bytes, size); - } -} - -uint8* CodedOutputStream::WriteVarint64ToArray( - uint64 value, uint8* target) { - return WriteVarint64ToArrayInline(value, target); +void CodedOutputStream::WriteVarint64SlowPath(uint64 value) { + uint8 bytes[kMaxVarintBytes]; + uint8* target = &bytes[0]; + uint8* end = WriteVarint64ToArray(value, target); + int size = end - target; + WriteRaw(bytes, size); } bool CodedOutputStream::Refresh() { @@ -904,17 +817,22 @@ bool CodedOutputStream::Refresh() { } size_t CodedOutputStream::VarintSize32Fallback(uint32 value) { + // This computes floor(log2(value)) / 7 + 1 + // Use an explicit multiplication to implement the divide of + // a number in the 1..31 range. GOOGLE_DCHECK_NE(0, value); // This is enforced by our caller. - return 1 + Bits::Log2FloorNonZero(value) / 7; + uint32 log2value = Bits::Log2FloorNonZero(value); + return static_cast((log2value * 9 + 73) / 64); } size_t CodedOutputStream::VarintSize64(uint64 value) { - if (value < (1 << 7)) { - return 1; - } - - return 1 + Bits::Log2FloorNonZero64(value) / 7; + // This computes value == 0 ? 1 : floor(log2(value)) / 7 + 1 + // Use an explicit multiplication to implement the divide of + // a number in the 1..63 range. + // Explicit OR 0x1 to avoid calling clz(0), which is undefined. + uint32 log2value = Bits::Log2FloorNonZero64(value | 0x1); + return static_cast((log2value * 9 + 73) / 64); } uint8* CodedOutputStream::WriteStringWithSizeToArray(const string& str, diff --git a/src/google/protobuf/io/coded_stream.h b/src/google/protobuf/io/coded_stream.h index 1402cc17..d2a3e279 100644 --- a/src/google/protobuf/io/coded_stream.h +++ b/src/google/protobuf/io/coded_stream.h @@ -249,12 +249,16 @@ class LIBPROTOBUF_EXPORT CodedInputStream { bool ReadVarintSizeAsInt(int* value); // Read a tag. This calls ReadVarint32() and returns the result, or returns - // zero (which is not a valid tag) if ReadVarint32() fails. Also, it updates - // the last tag value, which can be checked with LastTagWas(). + // zero (which is not a valid tag) if ReadVarint32() fails. Also, ReadTag + // (but not ReadTagNoLastTag) updates the last tag value, which can be checked + // with LastTagWas(). + // // Always inline because this is only called in one place per parse loop // but it is called for every iteration of said loop, so it should be fast. // GCC doesn't want to inline this by default. GOOGLE_ATTRIBUTE_ALWAYS_INLINE uint32 ReadTag(); + GOOGLE_ATTRIBUTE_ALWAYS_INLINE uint32 ReadTagNoLastTag(); + // This usually a faster alternative to ReadTag() when cutoff is a manifest // constant. It does particularly well for cutoff >= 127. The first part @@ -266,6 +270,8 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // to avoid an extra "is tag == 0?" check here.) GOOGLE_ATTRIBUTE_ALWAYS_INLINE std::pair ReadTagWithCutoff( uint32 cutoff); + GOOGLE_ATTRIBUTE_ALWAYS_INLINE std::pair ReadTagWithCutoffNoLastTag( + uint32 cutoff); // Usually returns true if calling ReadVarint32() now would produce the given // value. Will always return false if ReadVarint32() would not return the @@ -293,8 +299,10 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // zero, and ConsumedEntireMessage() will return true. bool ExpectAtEnd(); - // If the last call to ReadTag() or ReadTagWithCutoff() returned the - // given value, returns true. Otherwise, returns false; + // If the last call to ReadTag() or ReadTagWithCutoff() returned the given + // value, returns true. Otherwise, returns false. + // ReadTagNoLastTag/ReadTagWithCutoffNoLastTag do not preserve the last + // returned value. // // This is needed because parsers for some types of embedded messages // (with field type TYPE_GROUP) don't actually know that they've reached the @@ -612,6 +620,13 @@ class LIBPROTOBUF_EXPORT CodedInputStream { int ReadVarintSizeAsIntSlow(); bool ReadLittleEndian32Fallback(uint32* value); bool ReadLittleEndian64Fallback(uint64* value); + + template + GOOGLE_ATTRIBUTE_ALWAYS_INLINE uint32 ReadTagImplementation(); + template + GOOGLE_ATTRIBUTE_ALWAYS_INLINE + std::pair ReadTagWithCutoffImplementation(uint32 cutoff); + // Fallback/slow methods for reading tags. These do not update last_tag_, // but will set legitimate_message_end_ if we are at the end of the input // stream. @@ -622,7 +637,7 @@ class LIBPROTOBUF_EXPORT CodedInputStream { // Return the size of the buffer. int BufferSize() const; - static const int kDefaultTotalBytesLimit = 64 << 20; // 64MB + static const int kDefaultTotalBytesLimit = INT_MAX; static const int kDefaultTotalBytesWarningThreshold = 32 << 20; // 32MB @@ -842,10 +857,10 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { serialization_deterministic_override_ = value; } // See above. Also, note that users of this CodedOutputStream may need to - // call IsSerializationDeterminstic() to serialize in the intended way. This + // call IsSerializationDeterministic() to serialize in the intended way. This // CodedOutputStream cannot enforce a desire for deterministic serialization // by itself. - bool IsSerializationDeterminstic() const { + bool IsSerializationDeterministic() const { return serialization_deterministic_is_overridden_ ? serialization_deterministic_override_ : default_serialization_deterministic_; @@ -879,16 +894,7 @@ class LIBPROTOBUF_EXPORT CodedOutputStream { // If this write might cross the end of the buffer, we compose the bytes first // then use WriteRaw(). void WriteVarint32SlowPath(uint32 value); - - // Always-inlined versions of WriteVarint* functions so that code can be - // reused, while still controlling size. For instance, WriteVarint32ToArray() - // should not directly call this: since it is inlined itself, doing so - // would greatly increase the size of generated code. Instead, it should call - // WriteVarint32FallbackToArray. Meanwhile, WriteVarint32() is already - // out-of-line, so it should just invoke this directly to avoid any extra - // function call overhead. - GOOGLE_ATTRIBUTE_ALWAYS_INLINE static uint8* WriteVarint64ToArrayInline( - uint64 value, uint8* target); + void WriteVarint64SlowPath(uint64 value); static size_t VarintSize32Fallback(uint32 value); @@ -1007,21 +1013,47 @@ inline bool CodedInputStream::ReadLittleEndian64(uint64* value) { } inline uint32 CodedInputStream::ReadTag() { + return ReadTagImplementation(); +} + +inline uint32 CodedInputStream::ReadTagNoLastTag() { + return ReadTagImplementation(); +} + +template +inline uint32 CodedInputStream::ReadTagImplementation() { uint32 v = 0; if (GOOGLE_PREDICT_TRUE(buffer_ < buffer_end_)) { v = *buffer_; if (v < 0x80) { - last_tag_ = v; + if (update_last_tag) { + last_tag_ = v; + } Advance(1); return v; } } - last_tag_ = ReadTagFallback(v); - return last_tag_; + v = ReadTagFallback(v); + if (update_last_tag) { + last_tag_ = v; + } + return v; } inline std::pair CodedInputStream::ReadTagWithCutoff( uint32 cutoff) { + return ReadTagWithCutoffImplementation(cutoff); +} + +inline std::pair CodedInputStream::ReadTagWithCutoffNoLastTag( + uint32 cutoff) { + return ReadTagWithCutoffImplementation(cutoff); +} + +template +inline std::pair +CodedInputStream::ReadTagWithCutoffImplementation( + uint32 cutoff) { // In performance-sensitive code we can expect cutoff to be a compile-time // constant, and things like "cutoff >= kMax1ByteVarint" to be evaluated at // compile time. @@ -1033,7 +1065,10 @@ inline std::pair CodedInputStream::ReadTagWithCutoff( first_byte_or_zero = buffer_[0]; if (static_cast(buffer_[0]) > 0) { const uint32 kMax1ByteVarint = 0x7f; - uint32 tag = last_tag_ = buffer_[0]; + uint32 tag = buffer_[0]; + if (update_last_tag) { + last_tag_ = tag; + } Advance(1); return std::make_pair(tag, cutoff >= kMax1ByteVarint || tag <= cutoff); } @@ -1044,7 +1079,10 @@ inline std::pair CodedInputStream::ReadTagWithCutoff( GOOGLE_PREDICT_TRUE(buffer_ + 1 < buffer_end_) && GOOGLE_PREDICT_TRUE((buffer_[0] & ~buffer_[1]) >= 0x80)) { const uint32 kMax2ByteVarint = (0x7f << 7) + 0x7f; - uint32 tag = last_tag_ = (1u << 7) * buffer_[1] + (buffer_[0] - 0x80); + uint32 tag = (1u << 7) * buffer_[1] + (buffer_[0] - 0x80); + if (update_last_tag) { + last_tag_ = tag; + } Advance(2); // It might make sense to test for tag == 0 now, but it is so rare that // that we don't bother. A varint-encoded 0 should be one byte unless @@ -1057,8 +1095,11 @@ inline std::pair CodedInputStream::ReadTagWithCutoff( } } // Slow path - last_tag_ = ReadTagFallback(first_byte_or_zero); - return std::make_pair(last_tag_, static_cast(last_tag_ - 1) < cutoff); + const uint32 tag = ReadTagFallback(first_byte_or_zero); + if (update_last_tag) { + last_tag_ = tag; + } + return std::make_pair(tag, static_cast(tag - 1) < cutoff); } inline bool CodedInputStream::LastTagWas(uint32 expected) { @@ -1153,21 +1194,24 @@ inline uint8* CodedOutputStream::WriteVarint32ToArray(uint32 value, return target + 1; } -inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) { - if (value < 0) { - WriteVarint64(static_cast(value)); - } else { - WriteVarint32(static_cast(value)); +inline uint8* CodedOutputStream::WriteVarint64ToArray(uint64 value, + uint8* target) { + while (value >= 0x80) { + *target = static_cast(value | 0x80); + value >>= 7; + ++target; } + *target = static_cast(value); + return target + 1; +} + +inline void CodedOutputStream::WriteVarint32SignExtended(int32 value) { + WriteVarint64(static_cast(value)); } inline uint8* CodedOutputStream::WriteVarint32SignExtendedToArray( int32 value, uint8* target) { - if (value < 0) { - return WriteVarint64ToArray(static_cast(value), target); - } else { - return WriteVarint32ToArray(static_cast(value), target); - } + return WriteVarint64ToArray(static_cast(value), target); } inline uint8* CodedOutputStream::WriteLittleEndian32ToArray(uint32 value, @@ -1216,6 +1260,19 @@ inline void CodedOutputStream::WriteVarint32(uint32 value) { } } +inline void CodedOutputStream::WriteVarint64(uint64 value) { + if (buffer_size_ >= 10) { + // Fast path: We have enough bytes left in the buffer to guarantee that + // this write won't cross the end, so we can skip the checks. + uint8* target = buffer_; + uint8* end = WriteVarint64ToArray(value, target); + int size = static_cast(end - target); + Advance(size); + } else { + WriteVarint64SlowPath(value); + } +} + inline void CodedOutputStream::WriteTag(uint32 value) { WriteVarint32(value); } diff --git a/src/google/protobuf/io/coded_stream_unittest.cc b/src/google/protobuf/io/coded_stream_unittest.cc index 9f1ebc3f..31574d5b 100644 --- a/src/google/protobuf/io/coded_stream_unittest.cc +++ b/src/google/protobuf/io/coded_stream_unittest.cc @@ -135,7 +135,7 @@ class CodedStreamTest : public testing::Test { // for further information. static void SetupTotalBytesLimitWarningTest( int total_bytes_limit, int warning_threshold, - vector* out_errors, vector* out_warnings); + std::vector* out_errors, std::vector* out_warnings); // Buffer used during most of the tests. This assumes tests run sequentially. static const int kBufferSize = 1024 * 64; @@ -245,7 +245,7 @@ TEST_F(CodedStreamTest, EmptyInputBeforeEos) { int count_; } in; CodedInputStream input(&in); - input.ReadTag(); + input.ReadTagNoLastTag(); EXPECT_TRUE(input.ConsumedEntireMessage()); } @@ -446,6 +446,21 @@ TEST_2D(CodedStreamTest, ReadVarint32Error, kVarintErrorCases, kBlockSizes) { EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&value)); } +TEST_2D(CodedStreamTest, ReadVarint32Error_LeavesValueInInitializedState, + kVarintErrorCases, kBlockSizes) { + memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size); + ArrayInputStream input(buffer_, kVarintErrorCases_case.size, + kBlockSizes_case); + CodedInputStream coded_input(&input); + + uint32 value = 0; + EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint32(&value)); + // While the specific value following a failure is not critical, we do want to + // ensure that it doesn't get set to an uninitialized value. (This check fails + // in MSAN mode if value has been set to an uninitialized value.) + EXPECT_EQ(value, value); +} + TEST_2D(CodedStreamTest, ReadVarint64Error, kVarintErrorCases, kBlockSizes) { memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size); ArrayInputStream input(buffer_, kVarintErrorCases_case.size, @@ -456,6 +471,21 @@ TEST_2D(CodedStreamTest, ReadVarint64Error, kVarintErrorCases, kBlockSizes) { EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&value)); } +TEST_2D(CodedStreamTest, ReadVarint64Error_LeavesValueInInitializedState, + kVarintErrorCases, kBlockSizes) { + memcpy(buffer_, kVarintErrorCases_case.bytes, kVarintErrorCases_case.size); + ArrayInputStream input(buffer_, kVarintErrorCases_case.size, + kBlockSizes_case); + CodedInputStream coded_input(&input); + + uint64 value = 0; + EXPECT_EQ(kVarintErrorCases_case.can_parse, coded_input.ReadVarint64(&value)); + // While the specific value following a failure is not critical, we do want to + // ensure that it doesn't get set to an uninitialized value. (This check fails + // in MSAN mode if value has been set to an uninitialized value.) + EXPECT_EQ(value, value); +} + // ------------------------------------------------------------------- // VarintSize @@ -495,6 +525,28 @@ TEST_1D(CodedStreamTest, VarintSize64, kVarintSizeCases) { CodedOutputStream::VarintSize64(kVarintSizeCases_case.value)); } +TEST_F(CodedStreamTest, VarintSize32PowersOfTwo) { + int expected = 1; + for (int i = 1; i < 32; i++) { + if (i % 7 == 0) { + expected += 1; + } + EXPECT_EQ(expected, + CodedOutputStream::VarintSize32(static_cast(0x1u << i))); + } +} + +TEST_F(CodedStreamTest, VarintSize64PowersOfTwo) { + int expected = 1; + for (int i = 1; i < 64; i++) { + if (i % 7 == 0) { + expected += 1; + } + EXPECT_EQ(expected, CodedOutputStream::VarintSize64( + static_cast(0x1ull << i))); + } +} + // ------------------------------------------------------------------- // Fixed-size int tests @@ -1177,7 +1229,7 @@ TEST_F(CodedStreamTest, TotalBytesLimit) { EXPECT_TRUE(coded_input.ReadString(&str, 16)); EXPECT_EQ(0, coded_input.BytesUntilTotalBytesLimit()); - vector errors; + std::vector errors; { ScopedMemoryLog error_log; @@ -1211,7 +1263,7 @@ TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) { // Read a tag. Should fail, but report being a valid endpoint since it's // a regular limit. - EXPECT_EQ(0, coded_input.ReadTag()); + EXPECT_EQ(0, coded_input.ReadTagNoLastTag()); EXPECT_TRUE(coded_input.ConsumedEntireMessage()); // Pop the limit. @@ -1219,7 +1271,7 @@ TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) { // Read a tag. Should fail, and report *not* being a valid endpoint, since // this time we're hitting the total bytes limit. - EXPECT_EQ(0, coded_input.ReadTag()); + EXPECT_EQ(0, coded_input.ReadTagNoLastTag()); EXPECT_FALSE(coded_input.ConsumedEntireMessage()); } @@ -1229,7 +1281,7 @@ TEST_F(CodedStreamTest, TotalBytesLimitNotValidMessageEnd) { // vectors. void CodedStreamTest::SetupTotalBytesLimitWarningTest( int total_bytes_limit, int warning_threshold, - vector* out_errors, vector* out_warnings) { + std::vector* out_errors, std::vector* out_warnings) { ArrayInputStream raw_input(buffer_, sizeof(buffer_), 128); ScopedMemoryLog scoped_log; @@ -1245,25 +1297,21 @@ void CodedStreamTest::SetupTotalBytesLimitWarningTest( } TEST_F(CodedStreamTest, TotalBytesLimitWarning) { - vector errors; - vector warnings; + std::vector errors; + std::vector warnings; SetupTotalBytesLimitWarningTest(10240, 1024, &errors, &warnings); EXPECT_EQ(0, errors.size()); - ASSERT_EQ(2, warnings.size()); - EXPECT_PRED_FORMAT2(testing::IsSubstring, - "Reading dangerously large protocol message. If the message turns out to " - "be larger than 10240 bytes, parsing will be halted for security reasons.", - warnings[0]); + EXPECT_EQ(1, warnings.size()); EXPECT_PRED_FORMAT2(testing::IsSubstring, "The total number of bytes read was 2048", - warnings[1]); + warnings[0]); } TEST_F(CodedStreamTest, TotalBytesLimitWarningDisabled) { - vector errors; - vector warnings; + std::vector errors; + std::vector warnings; // Test with -1 SetupTotalBytesLimitWarningTest(10240, -1, &errors, &warnings); @@ -1362,7 +1410,7 @@ TEST_F(CodedStreamTest, InputOver2G) { // input.BackUp() with the correct number of bytes on destruction. ReallyBigInputStream input; - vector errors; + std::vector errors; { ScopedMemoryLog error_log; diff --git a/src/google/protobuf/io/gzip_stream.cc b/src/google/protobuf/io/gzip_stream.cc index 9c621b6a..a569eff0 100644 --- a/src/google/protobuf/io/gzip_stream.cc +++ b/src/google/protobuf/io/gzip_stream.cc @@ -168,7 +168,7 @@ void GzipInputStream::BackUp(int count) { } bool GzipInputStream::Skip(int count) { const void* data; - int size; + int size = 0; bool ok = Next(&data, &size); while (ok && (size < count)) { count -= size; diff --git a/src/google/protobuf/io/printer.cc b/src/google/protobuf/io/printer.cc index 7532b098..99e895f5 100644 --- a/src/google/protobuf/io/printer.cc +++ b/src/google/protobuf/io/printer.cc @@ -70,8 +70,8 @@ Printer::~Printer() { } bool Printer::GetSubstitutionRange(const char* varname, - pair* range) { - map >::const_iterator iter = + std::pair* range) { + std::map >::const_iterator iter = substitutions_.find(varname); if (iter == substitutions_.end()) { GOOGLE_LOG(DFATAL) << " Undefined variable in annotation: " << varname; @@ -87,12 +87,12 @@ bool Printer::GetSubstitutionRange(const char* varname, } void Printer::Annotate(const char* begin_varname, const char* end_varname, - const string& file_path, const vector& path) { + const string& file_path, const std::vector& path) { if (annotation_collector_ == NULL) { // Can't generate signatures with this Printer. return; } - pair begin, end; + std::pair begin, end; if (!GetSubstitutionRange(begin_varname, &begin) || !GetSubstitutionRange(end_varname, &end)) { return; @@ -106,7 +106,8 @@ void Printer::Annotate(const char* begin_varname, const char* end_varname, } } -void Printer::Print(const map& variables, const char* text) { +void Printer::Print(const std::map& variables, + const char* text) { int size = strlen(text); int pos = 0; // The number of bytes we've written so far. substitutions_.clear(); @@ -143,14 +144,15 @@ void Printer::Print(const map& variables, const char* text) { WriteRaw(&variable_delimiter_, 1); } else { // Replace with the variable's value. - map::const_iterator iter = variables.find(varname); + std::map::const_iterator iter = variables.find(varname); if (iter == variables.end()) { GOOGLE_LOG(DFATAL) << " Undefined variable: " << varname; } else { size_t begin = offset_; WriteRaw(iter->second.data(), iter->second.size()); - pair >::iterator, bool> inserted = - substitutions_.insert( + std::pair >::iterator, + bool> + inserted = substitutions_.insert( std::make_pair(varname, std::make_pair(begin, offset_))); if (!inserted.second) { // This variable was used multiple times. Make its span have @@ -172,13 +174,13 @@ void Printer::Print(const map& variables, const char* text) { } void Printer::Print(const char* text) { - static map empty; + static std::map empty; Print(empty, text); } void Printer::Print(const char* text, const char* variable, const string& value) { - map vars; + std::map vars; vars[variable] = value; Print(vars, text); } @@ -186,7 +188,7 @@ void Printer::Print(const char* text, void Printer::Print(const char* text, const char* variable1, const string& value1, const char* variable2, const string& value2) { - map vars; + std::map vars; vars[variable1] = value1; vars[variable2] = value2; Print(vars, text); @@ -196,7 +198,7 @@ void Printer::Print(const char* text, const char* variable1, const string& value1, const char* variable2, const string& value2, const char* variable3, const string& value3) { - map vars; + std::map vars; vars[variable1] = value1; vars[variable2] = value2; vars[variable3] = value3; @@ -208,7 +210,7 @@ void Printer::Print(const char* text, const char* variable2, const string& value2, const char* variable3, const string& value3, const char* variable4, const string& value4) { - map vars; + std::map vars; vars[variable1] = value1; vars[variable2] = value2; vars[variable3] = value3; @@ -222,7 +224,7 @@ void Printer::Print(const char* text, const char* variable3, const string& value3, const char* variable4, const string& value4, const char* variable5, const string& value5) { - map vars; + std::map vars; vars[variable1] = value1; vars[variable2] = value2; vars[variable3] = value3; @@ -238,7 +240,7 @@ void Printer::Print(const char* text, const char* variable4, const string& value4, const char* variable5, const string& value5, const char* variable6, const string& value6) { - map vars; + std::map vars; vars[variable1] = value1; vars[variable2] = value2; vars[variable3] = value3; @@ -256,7 +258,7 @@ void Printer::Print(const char* text, const char* variable5, const string& value5, const char* variable6, const string& value6, const char* variable7, const string& value7) { - map vars; + std::map vars; vars[variable1] = value1; vars[variable2] = value2; vars[variable3] = value3; @@ -276,7 +278,7 @@ void Printer::Print(const char* text, const char* variable6, const string& value6, const char* variable7, const string& value7, const char* variable8, const string& value8) { - map vars; + std::map vars; vars[variable1] = value1; vars[variable2] = value2; vars[variable3] = value3; diff --git a/src/google/protobuf/io/printer.h b/src/google/protobuf/io/printer.h index e78e2efd..e666445b 100644 --- a/src/google/protobuf/io/printer.h +++ b/src/google/protobuf/io/printer.h @@ -55,7 +55,7 @@ class LIBPROTOBUF_EXPORT AnnotationCollector { // before end_offset are associated with the SourceCodeInfo-style path. virtual void AddAnnotation(size_t begin_offset, size_t end_offset, const string& file_path, - const vector& path) = 0; + const std::vector& path) = 0; virtual ~AnnotationCollector() {} }; @@ -73,7 +73,8 @@ class AnnotationProtoCollector : public AnnotationCollector { // Override for AnnotationCollector::AddAnnotation. virtual void AddAnnotation(size_t begin_offset, size_t end_offset, - const string& file_path, const vector& path) { + const string& file_path, + const std::vector& path) { typename AnnotationProto::Annotation* annotation = annotation_proto_->add_annotation(); for (int i = 0; i < path.size(); ++i) { @@ -195,7 +196,7 @@ class LIBPROTOBUF_EXPORT Printer { // of building the location path. return; } - vector path; + std::vector path; descriptor->GetLocationPath(&path); Annotate(begin_varname, end_varname, descriptor->file()->name(), path); } @@ -216,7 +217,7 @@ class LIBPROTOBUF_EXPORT Printer { // Annotations aren't turned on for this Printer. return; } - vector empty_path; + std::vector empty_path; Annotate(begin_varname, end_varname, file_name, empty_path); } @@ -225,7 +226,7 @@ class LIBPROTOBUF_EXPORT Printer { // substituted are identified by their names surrounded by delimiter // characters (as given to the constructor). The variable bindings are // defined by the given map. - void Print(const map& variables, const char* text); + void Print(const std::map& variables, const char* text); // Like the first Print(), except the substitutions are given as parameters. void Print(const char* text); @@ -308,7 +309,7 @@ class LIBPROTOBUF_EXPORT Printer { // substituted for end_varname. Note that begin_varname and end_varname // may refer to the same variable. void Annotate(const char* begin_varname, const char* end_varname, - const string& file_path, const vector& path); + const string& file_path, const std::vector& path); const char variable_delimiter_; @@ -331,13 +332,14 @@ class LIBPROTOBUF_EXPORT Printer { // start offset is the beginning of the substitution; the end offset is the // last byte of the substitution plus one (such that (end - start) is the // length of the substituted string). - map > substitutions_; + std::map > substitutions_; // Returns true and sets range to the substitution range in the output for // varname if varname was used once in the last call to Print. If varname // was not used, or if it was used multiple times, returns false (and // fails a debug assertion). - bool GetSubstitutionRange(const char* varname, pair* range); + bool GetSubstitutionRange(const char* varname, + std::pair* range); // If non-null, annotation_collector_ is used to store annotations about // generated code. diff --git a/src/google/protobuf/io/printer_unittest.cc b/src/google/protobuf/io/printer_unittest.cc index 95f3afa2..0435228a 100644 --- a/src/google/protobuf/io/printer_unittest.cc +++ b/src/google/protobuf/io/printer_unittest.cc @@ -121,7 +121,7 @@ TEST(Printer, VariableSubstitution) { { Printer printer(&output, '$'); - map vars; + std::map vars; vars["foo"] = "World"; vars["bar"] = "$foo$"; @@ -187,7 +187,7 @@ class MockDescriptorFile { // annotations. class MockDescriptor { public: - MockDescriptor(const string& file, const vector& path) + MockDescriptor(const string& file, const std::vector& path) : file_(file), path_(path) {} // The mock file in which this descriptor was defined. @@ -201,7 +201,7 @@ class MockDescriptor { void GetLocationPath(std::vector* output) const { *output = path_; } MockDescriptorFile file_; - vector path_; + std::vector path_; }; TEST(Printer, AnnotateMap) { @@ -211,13 +211,13 @@ TEST(Printer, AnnotateMap) { AnnotationProtoCollector info_collector(&info); { Printer printer(&output, '$', &info_collector); - map vars; + std::map vars; vars["foo"] = "3"; vars["bar"] = "5"; printer.Print(vars, "012$foo$4$bar$\n"); - vector path_1; + std::vector path_1; path_1.push_back(33); - vector path_2; + std::vector path_2; path_2.push_back(11); path_2.push_back(22); MockDescriptor descriptor_1("path_1", path_1); @@ -255,9 +255,9 @@ TEST(Printer, AnnotateInline) { { Printer printer(&output, '$', &info_collector); printer.Print("012$foo$4$bar$\n", "foo", "3", "bar", "5"); - vector path_1; + std::vector path_1; path_1.push_back(33); - vector path_2; + std::vector path_2; path_2.push_back(11); path_2.push_back(22); MockDescriptor descriptor_1("path_1", path_1); @@ -295,7 +295,7 @@ TEST(Printer, AnnotateRange) { { Printer printer(&output, '$', &info_collector); printer.Print("012$foo$4$bar$\n", "foo", "3", "bar", "5"); - vector path; + std::vector path; path.push_back(33); MockDescriptor descriptor("path", path); printer.Annotate("foo", "bar", &descriptor); @@ -320,7 +320,7 @@ TEST(Printer, AnnotateEmptyRange) { Printer printer(&output, '$', &info_collector); printer.Print("012$foo$4$baz$$bam$$bar$\n", "foo", "3", "bar", "5", "baz", "", "bam", ""); - vector path; + std::vector path; path.push_back(33); MockDescriptor descriptor("path", path); printer.Annotate("baz", "bam", &descriptor); @@ -344,7 +344,7 @@ TEST(Printer, AnnotateDespiteUnrelatedMultipleUses) { { Printer printer(&output, '$', &info_collector); printer.Print("012$foo$4$foo$$bar$\n", "foo", "3", "bar", "5"); - vector path; + std::vector path; path.push_back(33); MockDescriptor descriptor("path", path); printer.Annotate("bar", "bar", &descriptor); @@ -368,7 +368,7 @@ TEST(Printer, Indenting) { { Printer printer(&output, '$'); - map vars; + std::map vars; vars["newline"] = "\n"; @@ -432,7 +432,7 @@ TEST(Printer, AnnotateMultipleUsesDeath) { { Printer printer(&output, '$', &info_collector); printer.Print("012$foo$4$foo$\n", "foo", "3"); - vector path; + std::vector path; path.push_back(33); MockDescriptor descriptor("path", path); EXPECT_DEBUG_DEATH(printer.Annotate("foo", "foo", &descriptor), "multiple"); @@ -447,7 +447,7 @@ TEST(Printer, AnnotateNegativeLengthDeath) { { Printer printer(&output, '$', &info_collector); printer.Print("012$foo$4$bar$\n", "foo", "3", "bar", "5"); - vector path; + std::vector path; path.push_back(33); MockDescriptor descriptor("path", path); EXPECT_DEBUG_DEATH(printer.Annotate("bar", "foo", &descriptor), "negative"); @@ -462,7 +462,7 @@ TEST(Printer, AnnotateUndefinedDeath) { { Printer printer(&output, '$', &info_collector); printer.Print("012$foo$4$foo$\n", "foo", "3"); - vector path; + std::vector path; path.push_back(33); MockDescriptor descriptor("path", path); EXPECT_DEBUG_DEATH(printer.Annotate("bar", "bar", &descriptor), diff --git a/src/google/protobuf/io/tokenizer.cc b/src/google/protobuf/io/tokenizer.cc index b3550dfb..916d1606 100644 --- a/src/google/protobuf/io/tokenizer.cc +++ b/src/google/protobuf/io/tokenizer.cc @@ -665,7 +665,7 @@ namespace { class CommentCollector { public: CommentCollector(string* prev_trailing_comments, - vector* detached_comments, + std::vector* detached_comments, string* next_leading_comments) : prev_trailing_comments_(prev_trailing_comments), detached_comments_(detached_comments), @@ -737,7 +737,7 @@ class CommentCollector { private: string* prev_trailing_comments_; - vector* detached_comments_; + std::vector* detached_comments_; string* next_leading_comments_; string comment_buffer_; @@ -757,7 +757,7 @@ class CommentCollector { } // namespace bool Tokenizer::NextWithComments(string* prev_trailing_comments, - vector* detached_comments, + std::vector* detached_comments, string* next_leading_comments) { CommentCollector collector(prev_trailing_comments, detached_comments, next_leading_comments); diff --git a/src/google/protobuf/io/tokenizer.h b/src/google/protobuf/io/tokenizer.h index 77a873bc..e80d564c 100644 --- a/src/google/protobuf/io/tokenizer.h +++ b/src/google/protobuf/io/tokenizer.h @@ -191,7 +191,7 @@ class LIBPROTOBUF_EXPORT Tokenizer { // * grault. */ // optional int32 grault = 6; bool NextWithComments(string* prev_trailing_comments, - vector* detached_comments, + std::vector* detached_comments, string* next_leading_comments); // Parse helpers --------------------------------------------------- diff --git a/src/google/protobuf/io/tokenizer_unittest.cc b/src/google/protobuf/io/tokenizer_unittest.cc index ae0811f8..a2c19522 100644 --- a/src/google/protobuf/io/tokenizer_unittest.cc +++ b/src/google/protobuf/io/tokenizer_unittest.cc @@ -693,7 +693,7 @@ TEST_2D(TokenizerTest, DocComments, kDocCommentCases, kBlockSizes) { EXPECT_EQ("prev", tokenizer2.current().text); string prev_trailing_comments; - vector detached_comments; + std::vector detached_comments; string next_leading_comments; tokenizer.NextWithComments(&prev_trailing_comments, &detached_comments, &next_leading_comments); -- cgit v1.2.3