Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(common): out of range access in handle cleanup #14134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google/cloud/internal/curl_handle_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void PooledCurlHandleFactory::CleanupMultiHandle(CurlMulti m,
if (multi_handles_.size() >= maximum_size_) {
// Same idea as is CleanupHandle()
auto const release_count =
(std::min)(handles_.size() - maximum_size_ / 2,
(std::min)(multi_handles_.size() - maximum_size_ / 2,
active_multi_handles_ - maximum_size_);
released.reserve(release_count);
auto const end = std::next(multi_handles_.begin(), release_count);
Expand Down
41 changes: 41 additions & 0 deletions google/cloud/internal/curl_handle_factory_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,47 @@ TEST(CurlHandleFactoryTest, PooledFactoryChannelOptionsCallsSetOptions) {
EXPECT_THAT(object_under_test.set_options_, ElementsAre(expected));
}

TEST(CurlHandleFactoryTest, Bug14132Regression) {
auto constexpr kPoolSize = 32;
PooledCurlHandleFactory pool(kPoolSize);
std::vector<CurlPtr> handles;
std::generate_n(std::back_inserter(handles), 2 * kPoolSize,
[&]() { return pool.CreateHandle(); });

for (int i = 4 * kPoolSize; i != 0; --i) {
std::vector<CurlMulti> multi;
std::generate_n(std::back_inserter(multi), i,
[&]() { return pool.CreateMultiHandle(); });
for (auto& m : multi) {
pool.CleanupMultiHandle(std::move(m), HandleDisposition::kKeep);
}
}
for (auto& h : handles) {
pool.CleanupHandle(std::move(h), HandleDisposition::kDiscard);
}
}

/// @test An analog to #14132 but with CurlMulti and CurlPtr roles reversed.
TEST(CurlHandleFactoryTest, AnalogToBug14132Regression) {
auto constexpr kPoolSize = 32;
PooledCurlHandleFactory pool(kPoolSize);
std::vector<CurlMulti> multi;
std::generate_n(std::back_inserter(multi), 2 * kPoolSize,
[&]() { return pool.CreateMultiHandle(); });

for (int i = 4 * kPoolSize; i != 0; --i) {
std::vector<CurlPtr> handles;
std::generate_n(std::back_inserter(handles), i,
[&]() { return pool.CreateHandle(); });
for (auto& h : handles) {
pool.CleanupHandle(std::move(h), HandleDisposition::kKeep);
}
}
for (auto& m : multi) {
pool.CleanupMultiHandle(std::move(m), HandleDisposition::kDiscard);
}
}

} // namespace
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace rest_internal
Expand Down