Skip to content

[libc++] Update polymorphic_allocator to never contain a nullptr #148423

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 1 commit into from
Jul 16, 2025
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
6 changes: 4 additions & 2 deletions libcxx/include/__memory_resource/polymorphic_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ class _LIBCPP_AVAILABILITY_PMR polymorphic_allocator {

_LIBCPP_HIDE_FROM_ABI polymorphic_allocator() noexcept : __res_(std::pmr::get_default_resource()) {}

_LIBCPP_HIDE_FROM_ABI polymorphic_allocator(memory_resource* __r) noexcept : __res_(__r) {}
_LIBCPP_HIDE_FROM_ABI polymorphic_allocator(memory_resource* _LIBCPP_DIAGNOSE_NULLPTR __r) noexcept : __res_(__r) {
_LIBCPP_ASSERT_NON_NULL(__r, "Attempted to pass a nullptr resource to polymorphic_alloator");
}

_LIBCPP_HIDE_FROM_ABI polymorphic_allocator(const polymorphic_allocator&) = default;

Expand Down Expand Up @@ -174,7 +176,7 @@ class _LIBCPP_AVAILABILITY_PMR polymorphic_allocator {
return polymorphic_allocator();
}

_LIBCPP_HIDE_FROM_ABI memory_resource* resource() const noexcept { return __res_; }
[[__gnu__::__returns_nonnull__]] _LIBCPP_HIDE_FROM_ABI memory_resource* resource() const noexcept { return __res_; }

_LIBCPP_HIDE_FROM_ABI friend bool
operator==(const polymorphic_allocator& __lhs, const polymorphic_allocator& __rhs) noexcept {
Expand Down
30 changes: 30 additions & 0 deletions libcxx/test/libcxx/mem/mem.res/ctor.nullptr.assert.pass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <memory_resource>

// Test hardening assertions for std::pmr::polymorphic_allocator.

// REQUIRES: has-unix-headers
// REQUIRES: libcpp-hardening-mode={{extensive|debug}}
// UNSUPPORTED: c++03, c++11, c++14
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

// We're testing nullptr assertions
// ADDITIONAL_COMPILE_FLAGS: -Wno-nonnull

#include <memory_resource>

#include "check_assertion.h"

int main(int, char**) {
TEST_LIBCPP_ASSERT_FAILURE(
std::pmr::polymorphic_allocator<int>(nullptr), "Attempted to pass a nullptr resource to polymorphic_alloator");

return 0;
}
17 changes: 17 additions & 0 deletions libcxx/test/libcxx/mem/mem.res/nonnull.verify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14

// Ensure that passing a nullptr to polymorphic_allocator is diagnosed

#include <memory_resource>

void test() {
std::pmr::polymorphic_allocator<int> alloc(nullptr); // expected-warning {{null passed}}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,39 @@
// memory_resource *
// polymorphic_allocator<T>::resource() const

#include <memory_resource>
#include <cassert>
#include <cstddef>
#include <memory_resource>
#include <new>

#include "test_macros.h"

struct resource : std::pmr::memory_resource {
void* do_allocate(size_t, size_t) override { TEST_THROW(std::bad_alloc()); }
void do_deallocate(void*, size_t, size_t) override { assert(false); }
bool do_is_equal(const std::pmr::memory_resource&) const noexcept override { return false; }
};

int main(int, char**) {
typedef std::pmr::polymorphic_allocator<void> A;
{
A const a;
ASSERT_SAME_TYPE(decltype(a.resource()), std::pmr::memory_resource*);
}
{
std::pmr::memory_resource* mptr = (std::pmr::memory_resource*)42;
A const a(mptr);
assert(a.resource() == mptr);
}
{
A const a(nullptr);
assert(a.resource() == nullptr);
assert(a.resource() == nullptr);
resource res;
A const a(&res);
assert(a.resource() == &res);
}
{
A const a;
assert(a.resource() == std::pmr::get_default_resource());
}
{
std::pmr::memory_resource* mptr = (std::pmr::memory_resource*)42;
std::pmr::set_default_resource(mptr);
resource res;
std::pmr::set_default_resource(&res);
A const a;
assert(a.resource() == mptr);
assert(a.resource() == &res);
assert(a.resource() == std::pmr::get_default_resource());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,32 @@
// polymorphic_allocator
// polymorphic_allocator<T>::select_on_container_copy_construction() const

#include <memory_resource>
#include <cassert>
#include <cstddef>
#include <memory_resource>
#include <new>

#include "test_macros.h"

struct resource : std::pmr::memory_resource {
void* do_allocate(size_t, size_t) override { TEST_THROW(std::bad_alloc()); }
void do_deallocate(void*, size_t, size_t) override { assert(false); }
bool do_is_equal(const std::pmr::memory_resource&) const noexcept override { return false; }
};

int main(int, char**) {
typedef std::pmr::polymorphic_allocator<void> A;
{
A const a;
ASSERT_SAME_TYPE(decltype(a.select_on_container_copy_construction()), A);
}
{
std::pmr::memory_resource* mptr = (std::pmr::memory_resource*)42;
A const a(mptr);
assert(a.resource() == mptr);
A const other = a.select_on_container_copy_construction();
assert(other.resource() == std::pmr::get_default_resource());
assert(a.resource() == mptr);
}
{
std::pmr::memory_resource* mptr = (std::pmr::memory_resource*)42;
std::pmr::set_default_resource(mptr);
A const a(nullptr);
assert(a.resource() == nullptr);
resource res;
A const a(&res);
assert(a.resource() == &res);
A const other = a.select_on_container_copy_construction();
assert(other.resource() == std::pmr::get_default_resource());
assert(other.resource() == mptr);
assert(a.resource() == nullptr);
assert(a.resource() == &res);
}

return 0;
Expand Down
Loading