From a62c2003328804cc66e6f4bd5bf8f95a1ac07ebb Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Thu, 14 Jan 2021 10:42:12 -0800 Subject: [PATCH] proto: re-implement RepeatedPtrUtil::hash(). (#14701) 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 --- source/common/protobuf/utility.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/source/common/protobuf/utility.h b/source/common/protobuf/utility.h index a23fe7d216f3..e6480c2aee2f 100644 --- a/source/common/protobuf/utility.h +++ b/source/common/protobuf/utility.h @@ -166,17 +166,17 @@ class RepeatedPtrUtil { // Based on MessageUtil::hash() defined below. template static std::size_t hash(const Protobuf::RepeatedPtrField& 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);