Hello,
I'm using version 3.2. I have a google::protobuf::map<int, Msg> and would
like to implement a function that returns either a pointer to value in the
map given or a key or null if it does not exist. If I was using a
std::map, I would do this.
Msg const * Foo::TryGetValue(int key) const {
try {
auto& value = my_map.at(key);
return &value;
} catch (...) {
return nullptr;
}
}
Because protobuf's contract for "at" is different (it does a GOOGLE_CHECK
rather than a throw if the item with the given key is different), I thought
I could implement something similar like this, with the downside of doing
two lookups rather than one.
Msg const * Foo::TryGetValue(int key) const {
auto iter = my_map.find(key);
if (iter == my_map.end())
return nullptr;
auto& value = my_map.at(key);
return &value;
}
However, after peeking at the implementation of google::protobuf::map (and
I don't claim to understand it completely) it seems that the "at" method
does a find and then just returns iter->second which does a copy. So the
client of TryGetValue will have used after free. Is there another way to
do this?
Thanks!
Mohamed Koubaa
Software Developer
Ansys, Inc
--
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.