Skip to content

Commit 486ce3d

Browse files
author
ignat
committed
Add exception on duplicate keys during protobuf map serialization/deserialization
commit_hash:73552810ad9072ebfd5a1d2c67f58aadd78d0cca
1 parent 1ce6fd4 commit 486ce3d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

yt/yt/core/misc/protobuf_helpers-inl.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,12 @@ void ToProto(
486486
{
487487
serializedMap->clear();
488488
for (const auto& [key, value] : originalMap) {
489-
serializedMap->insert(std::pair(ToProto<TSerializedKey>(key), ToProto<TSerializedValue>(value)));
489+
auto [_, inserted] = serializedMap->insert(std::pair(ToProto<TSerializedKey>(key), ToProto<TSerializedValue>(value)));
490+
if (!inserted) {
491+
THROW_ERROR_EXCEPTION("Found duplicate key during protobuf map serialization")
492+
<< TErrorAttribute("key", key)
493+
<< TErrorAttribute("serialized_key", ToProto<TSerializedKey>(key));
494+
}
490495
}
491496
}
492497

@@ -533,7 +538,11 @@ void FromProto(
533538
{
534539
originalMap->clear();
535540
for (const auto& [serializedKey, serializedValue] : serializedMap) {
536-
EmplaceOrCrash(*originalMap, FromProto<TKey>(serializedKey), FromProto<TValue>(serializedValue));
541+
auto [_, inserted] = originalMap->emplace(FromProto<TKey>(serializedKey), FromProto<TValue>(serializedValue));
542+
if (!inserted) {
543+
THROW_ERROR_EXCEPTION("Found duplicate key during protobuf map deserialization")
544+
<< TErrorAttribute("key", FromProto<TKey>(serializedKey));
545+
}
537546
}
538547
}
539548

0 commit comments

Comments
 (0)