Skip to content

[libc++] Handle 0 size case for testing support operator new #93834

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
Jun 3, 2024

Conversation

jakeegan
Copy link
Member

The return of malloc is implementation defined when the requested size is 0. On platforms (such as AIX) that return a null pointer for 0 size, operator new will throw a bad_alloc exception. operator new should return a non null pointer for 0 size instead.

@jakeegan jakeegan requested a review from daltenty May 30, 2024 15:15
@jakeegan jakeegan requested a review from a team as a code owner May 30, 2024 15:15
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label May 30, 2024
@llvmbot
Copy link
Member

llvmbot commented May 30, 2024

@llvm/pr-subscribers-libcxx

Author: Jake Egan (jakeegan)

Changes

The return of malloc is implementation defined when the requested size is 0. On platforms (such as AIX) that return a null pointer for 0 size, operator new will throw a bad_alloc exception. operator new should return a non null pointer for 0 size instead.


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

2 Files Affected:

  • (modified) libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge.pass.cpp (-2)
  • (modified) libcxx/test/support/count_new.h (+4)
diff --git a/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge.pass.cpp
index 44f2868af3693..87bf9dc3854b3 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge.pass.cpp
@@ -14,8 +14,6 @@
 //   void
 //   inplace_merge(Iter first, Iter middle, Iter last);
 
-// XFAIL: LIBCXX-AIX-FIXME
-
 #include <algorithm>
 #include <cassert>
 #include <random>
diff --git a/libcxx/test/support/count_new.h b/libcxx/test/support/count_new.h
index dd8c0e54cae7f..0a95e05b72421 100644
--- a/libcxx/test/support/count_new.h
+++ b/libcxx/test/support/count_new.h
@@ -385,6 +385,8 @@ MemCounter &globalMemCounter = *getGlobalMemCounter();
 // operator new(size_t[, nothrow_t]) and operator delete(size_t[, nothrow_t])
 void* operator new(std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
   getGlobalMemCounter()->newCalled(s);
+  if (s == 0)
+    ++s;
   void* p = std::malloc(s);
   if (p == nullptr)
     detail::throw_bad_alloc_helper();
@@ -417,6 +419,8 @@ void operator delete(void* p, std::nothrow_t const&) TEST_NOEXCEPT {
 // operator new[](size_t[, nothrow_t]) and operator delete[](size_t[, nothrow_t])
 void* operator new[](std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
   getGlobalMemCounter()->newArrayCalled(s);
+  if (s == 0)
+    s++;
   void* p = std::malloc(s);
   if (p == nullptr)
     detail::throw_bad_alloc_helper();

@jakeegan jakeegan merged commit 037a052 into llvm:main Jun 3, 2024
58 checks passed
@jakeegan jakeegan deleted the 0_size_new branch June 3, 2024 20:23
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