[libcxx-commits] [libcxx] [libc++][string] Add regression test for sized new/delete bug (PR #110210)

Vitaly Buka via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 30 10:43:58 PDT 2024


https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/110210

>From 44da8d612670f97633a5c2832f2ef037c508af9b Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 26 Sep 2024 23:09:25 -0700
Subject: [PATCH 1/5] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.4
---
 .../string.capacity/deallocate_size.pass.cpp  | 63 +++++++++++++++++++
 1 file changed, 63 insertions(+)
 create mode 100644 libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp

diff --git a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
new file mode 100644
index 00000000000000..612b66195141c5
--- /dev/null
+++ b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
@@ -0,0 +1,63 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// <string>
+
+// allocate/deallocate must match.
+
+#include <string>
+#include <cassert>
+#include <type_traits>
+
+#include "test_macros.h"
+
+static int allocated_;
+
+template <class T, class Sz>
+struct test_alloc {
+  typedef Sz size_type;
+  typedef std::make_signed<Sz>::type difference_type;
+  typedef T value_type;
+  typedef value_type* pointer;
+  typedef const value_type* const_pointer;
+  typedef typename std::add_lvalue_reference<value_type>::type reference;
+  typedef typename std::add_lvalue_reference<const value_type>::type const_reference;
+
+  template <class U>
+  struct rebind {
+    typedef test_alloc<U, Sz> other;
+  };
+
+  TEST_CONSTEXPR_CXX14 pointer allocate(size_type n, const void* = nullptr) {
+    allocated_ += n;
+    return std::allocator<value_type>().allocate(n);
+  }
+
+  TEST_CONSTEXPR_CXX14 void deallocate(pointer p, size_type s) {
+    allocated_ -= s;
+    std::allocator<value_type>().deallocate(p, s);
+  }
+};
+
+template <class Sz>
+void test() {
+  for (int i = 1; i < 1000; ++i) {
+    using Str = std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> >;
+    {
+      Str s(i, 't');
+      assert(allocated_ == 0 || allocated_ >= i);
+    }
+  }
+  assert(allocated_ == 0);
+}
+
+int main(int, char**) {
+  test<uint32_t>();
+  test<uint64_t>();
+  test<size_t>();
+}

>From efa9c509ff48bb71b809b00f6371321730db839b Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 26 Sep 2024 23:29:10 -0700
Subject: [PATCH 2/5] typename

Created using spr 1.3.4
---
 .../basic.string/string.capacity/deallocate_size.pass.cpp       | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
index 612b66195141c5..ad45aafc8832c7 100644
--- a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
@@ -21,7 +21,7 @@ static int allocated_;
 template <class T, class Sz>
 struct test_alloc {
   typedef Sz size_type;
-  typedef std::make_signed<Sz>::type difference_type;
+  typedef typename std::make_signed<Sz>::type difference_type;
   typedef T value_type;
   typedef value_type* pointer;
   typedef const value_type* const_pointer;

>From 6f9e92671cffb46996b1d30da60232c73c7c7bad Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Thu, 26 Sep 2024 23:49:03 -0700
Subject: [PATCH 3/5] #include <cstdint>

Created using spr 1.3.4
---
 .../basic.string/string.capacity/deallocate_size.pass.cpp        | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
index ad45aafc8832c7..4536a0514b133c 100644
--- a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
@@ -12,6 +12,7 @@
 
 #include <string>
 #include <cassert>
+#include <cstdint>
 #include <type_traits>
 
 #include "test_macros.h"

>From dfff4019fcbdf7128dc597ed18a83eb81bf66256 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 30 Sep 2024 10:43:40 -0700
Subject: [PATCH 4/5] Update
 libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp

Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>
---
 .../basic.string/string.capacity/deallocate_size.pass.cpp       | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
index 4536a0514b133c..23a71c8e9044c0 100644
--- a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
@@ -61,4 +61,6 @@ int main(int, char**) {
   test<uint32_t>();
   test<uint64_t>();
   test<size_t>();
+  
+  return 0;
 }

>From d9d53b961a6fecfe748c09f1cbfb2c0c9b036665 Mon Sep 17 00:00:00 2001
From: Vitaly Buka <vitalybuka at google.com>
Date: Mon, 30 Sep 2024 10:43:49 -0700
Subject: [PATCH 5/5] Update
 libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp

Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>
---
 .../basic.string/string.capacity/deallocate_size.pass.cpp       | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
index 23a71c8e9044c0..8556b31fa62741 100644
--- a/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.capacity/deallocate_size.pass.cpp
@@ -8,7 +8,7 @@
 
 // <string>
 
-// allocate/deallocate must match.
+// Make sure the size we allocate and deallocate match. See https://github.com/llvm/llvm-project/pull/90292.
 
 #include <string>
 #include <cassert>



More information about the libcxx-commits mailing list