[libcxx-commits] [libcxx] [libc++] Handle 0 size case for testing support operator new (PR #93834)

Jake Egan via libcxx-commits libcxx-commits at lists.llvm.org
Thu May 30 08:15:50 PDT 2024


https://github.com/jakeegan created https://github.com/llvm/llvm-project/pull/93834

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.

>From d0f96bdb78be1e6fe9086e6a77558e52832ed39e Mon Sep 17 00:00:00 2001
From: Jake Egan <jake.egan at ibm.com>
Date: Thu, 30 May 2024 10:44:07 -0400
Subject: [PATCH] [libc++] Handle 0 size case for testing support operator new

---
 .../algorithms/alg.sorting/alg.merge/inplace_merge.pass.cpp   | 2 --
 libcxx/test/support/count_new.h                               | 4 ++++
 2 files changed, 4 insertions(+), 2 deletions(-)

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();



More information about the libcxx-commits mailing list