Skip to content

Commit

Permalink
proto: re-implement RepeatedPtrUtil::hash(). (#14701)
Browse files Browse the repository at this point in the history
This changes RepeatedPtrUtil::hash() implementation to match
MessageUtil::hash(), which was re-implemented in #8231.

Reported by Tomoaki Fujii (Google).

Signed-off-by: Piotr Sikora <piotrsikora@google.com>
  • Loading branch information
PiotrSikora authored Jan 14, 2021
1 parent e9f9df3 commit a62c200
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions source/common/protobuf/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,17 @@ class RepeatedPtrUtil {
// Based on MessageUtil::hash() defined below.
template <class ProtoType>
static std::size_t hash(const Protobuf::RepeatedPtrField<ProtoType>& source) {
// Use Protobuf::io::CodedOutputStream to force deterministic serialization, so that the same
// message doesn't hash to different values.
std::string text;
{
// For memory safety, the StringOutputStream needs to be destroyed before
// we read the string.
Protobuf::io::StringOutputStream string_stream(&text);
Protobuf::io::CodedOutputStream coded_stream(&string_stream);
coded_stream.SetSerializationDeterministic(true);
Protobuf::TextFormat::Printer printer;
printer.SetExpandAny(true);
printer.SetUseFieldNumber(true);
printer.SetSingleLineMode(true);
printer.SetHideUnknownFields(true);
for (const auto& message : source) {
message.SerializeToCodedStream(&coded_stream);
std::string text_message;
printer.PrintToString(message, &text_message);
absl::StrAppend(&text, text_message);
}
}
return HashUtil::xxHash64(text);
Expand Down

0 comments on commit a62c200

Please sign in to comment.