MINOR: [C++] Avoid multimap::find unspecified behavior#48607
Merged
lidavidm merged 2 commits intoapache:mainfrom Dec 23, 2025
Merged
MINOR: [C++] Avoid multimap::find unspecified behavior#48607lidavidm merged 2 commits intoapache:mainfrom
lidavidm merged 2 commits intoapache:mainfrom
Conversation
Member
|
It appears this needs to be formatted: --- a/cpp/src/arrow/flight/transport/grpc/util_internal.cc
+++ b/cpp/src/arrow/flight/transport/grpc/util_internal.cc
@@ -55,30 +55,25 @@ static bool FromGrpcContext(const ::grpc::ClientContext& ctx,
const std::multimap<::grpc::string_ref, ::grpc::string_ref>& trailers =
ctx.GetServerTrailingMetadata();
- const auto [code_val_begin, code_val_end] =
- trailers.equal_range(kGrpcStatusCodeHeader);
+ const auto [code_val_begin, code_val_end] = trailers.equal_range(kGrpcStatusCodeHeader);
if (code_val_begin == code_val_end) return false;
std::optional<std::string> message;
- if (const auto [it, end] = trailers.equal_range(kGrpcStatusMessageHeader);
- it != end) {
+ if (const auto [it, end] = trailers.equal_range(kGrpcStatusMessageHeader); it != end) {
message = std::string(it->second.data(), it->second.size());
}
std::optional<std::string> detail_message;
- if (const auto [it, end] = trailers.equal_range(kGrpcStatusDetailHeader);
- it != end) {
+ if (const auto [it, end] = trailers.equal_range(kGrpcStatusDetailHeader); it != end) {
detail_message = std::string(it->second.data(), it->second.size());
}
std::optional<std::string> detail_bin;
- if (const auto [it, end] = trailers.equal_range(kBinaryErrorDetailsKey);
- it != end) {
+ if (const auto [it, end] = trailers.equal_range(kBinaryErrorDetailsKey); it != end) {
detail_bin = std::string(it->second.data(), it->second.size());
}
- std::string code_str(code_val_begin->second.data(),
- code_val_begin->second.size());
+ std::string code_str(code_val_begin->second.data(), code_val_begin->second.size());
*status = internal::ReconstructStatus(code_str, current_status, std::move(message),
std::move(detail_message), std::move(detail_bin),
std::move(flight_status_detail)); |
Contributor
Author
Oops, fixed. |
lidavidm
approved these changes
Dec 23, 2025
|
After merging your PR, Conbench analyzed the 3 benchmarking runs that have been run so far on merge-commit 8040f2a. There were no benchmark performance regressions. 🎉 The full Conbench report has more details. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
When a
std::multimaphas multiple entries with the same key, callingm.find(key)returns an unspecified element.Historically, this returns the first matching element. However, this is not guaranteed, and recent libc++ changes make this return an arbitrary element. This can lead to surprising behavior, i.e. different values returned based on what other entries are in the multimap (and what the shape of the internal tree is).
What changes are included in this PR?
Replace
m.find(key)withm.equal_range(key)to preserve behavior and have predictable results. The behavior of this is guaranteed to return a range of all matching elements in insertion order, and the beginning of the range is the same element as what's normally returned bym.find(key).Are these changes tested?
Yes
Are there any user-facing changes?
No