Skip to content

[libc++][test] Fix invalid low-level const conversion in limited_allocator #118189

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

Merged
merged 2 commits into from
Dec 10, 2024

Conversation

winner245
Copy link
Contributor

@winner245 winner245 commented Dec 1, 2024

This PR fixes an invalid low-level const conversion bug in limited_allocator. As this class is a widely used utility for testing allocator-aware containers, it is important to fix it. It is a very simple fix by just adding a const qualification.

The bug did not crash our tests likely because our current usage of the template has not invoked the specific member function. However, we can trigger an immediate compilation error through explicit template instantiation or by using the operators == or !=.

@winner245 winner245 force-pushed the fix-limited-allocator branch 4 times, most recently from 2f2f79a to 1d5f220 Compare December 1, 2024 16:15
@winner245 winner245 marked this pull request as ready for review December 1, 2024 20:58
@winner245 winner245 requested a review from a team as a code owner December 1, 2024 20:58
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Dec 1, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 1, 2024

@llvm/pr-subscribers-libcxx

Author: Peng Liu (winner245)

Changes

This PR fixes an invalid low-level const conversion bug in limited_allocator. As this class is a widely used utility for testing allocator-aware containers, it is important to fix it. It is a very simple fix by just adding a const qualification.

The bug did not crash our tests likely because our current usage of the template has not invoked the specific member function. However, we can trigger an immediate compilation error through explicit template instantiation or by using the operators == or !=.


Full diff: https://github.com/llvm/llvm-project/pull/118189.diff

1 Files Affected:

  • (modified) libcxx/test/support/test_allocator.h (+4-1)
diff --git a/libcxx/test/support/test_allocator.h b/libcxx/test/support/test_allocator.h
index dcd15332ca304f..49dad642184da3 100644
--- a/libcxx/test/support/test_allocator.h
+++ b/libcxx/test/support/test_allocator.h
@@ -467,7 +467,10 @@ class limited_allocator {
   TEST_CONSTEXPR_CXX20 pointer allocate(size_type n) { return handle_->template allocate<T>(n); }
   TEST_CONSTEXPR_CXX20 void deallocate(pointer p, size_type n) { handle_->template deallocate<T>(p, n); }
   TEST_CONSTEXPR size_type max_size() const { return N; }
-  TEST_CONSTEXPR BuffT* getHandle() const { return handle_.get(); }
+
+  // In C++11, constexpr non-static member functions are implicitly const, but this is no longer the case since C++14.
+  TEST_CONSTEXPR_CXX14 BuffT* getHandle() { return handle_.get(); }
+  TEST_CONSTEXPR const BuffT* getHandle() const { return handle_.get(); }
 };
 
 template <class T, class U, std::size_t N>

@@ -467,7 +467,10 @@ class limited_allocator {
TEST_CONSTEXPR_CXX20 pointer allocate(size_type n) { return handle_->template allocate<T>(n); }
TEST_CONSTEXPR_CXX20 void deallocate(pointer p, size_type n) { handle_->template deallocate<T>(p, n); }
TEST_CONSTEXPR size_type max_size() const { return N; }
TEST_CONSTEXPR BuffT* getHandle() const { return handle_.get(); }

// In C++11, constexpr non-static member functions are implicitly const, but this is no longer the case since C++14.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you come across this issue?

Copy link
Contributor Author

@winner245 winner245 Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was working on a draft PR #118141 adding exception tests for other vector operations, which heavily used the limited_allocator class and somehow triggered the usage of operator==.

Comment on lines 472 to 480
TEST_CONSTEXPR_CXX14 BuffT* getHandle() { return handle_.get(); }
TEST_CONSTEXPR const BuffT* getHandle() const { return handle_.get(); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should instead do this:

Suggested change
TEST_CONSTEXPR_CXX14 BuffT* getHandle() { return handle_.get(); }
TEST_CONSTEXPR const BuffT* getHandle() const { return handle_.get(); }
TEST_CONSTEXPR const BuffT* getHandle() const { return handle_.get(); }

Let's just provide the const version, since the non-const version is not needed. This method is only used by operator== below.

The comment can also be removed if we only have the const version. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. We only need a const version.

@winner245 winner245 force-pushed the fix-limited-allocator branch 5 times, most recently from 479e0e4 to 313bfb3 Compare December 9, 2024 12:06
@ldionne ldionne force-pushed the fix-limited-allocator branch from 313bfb3 to a51a2db Compare December 9, 2024 16:47
@ldionne
Copy link
Member

ldionne commented Dec 9, 2024

(I ran clang-format separately and rebased your patch)

@ldionne ldionne merged commit eacdbc2 into llvm:main Dec 10, 2024
58 of 62 checks passed
@winner245 winner245 deleted the fix-limited-allocator branch December 10, 2024 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants