[libcxx-commits] [libcxx] [libc++] Fix behavior for `get_temporary_buffer` with non-positive size (PR #206871)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jun 30 19:18:20 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

<details>
<summary>Changes</summary>

Per C++17 [depr.temporary.buffer]/4, `get_temporary_buffer` is required to return `{nullptr, 0}` when the size argument is zero or negative. libc++ used to correctly handle this, but the refactoring in 94e7c0b051c79fd56205f115771980f2e7812306 got this wrong.

Fixes #<!-- -->204082.

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


3 Files Affected:

- (modified) libcxx/include/__memory/temporary_buffer.h (+3) 
- (modified) libcxx/test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp (+14) 
- (modified) libcxx/test/std/utilities/memory/temporary.buffer/temporary_buffer.pass.cpp (+14) 


``````````diff
diff --git a/libcxx/include/__memory/temporary_buffer.h b/libcxx/include/__memory/temporary_buffer.h
index d18717f52d1cd..3c572e43db641 100644
--- a/libcxx/include/__memory/temporary_buffer.h
+++ b/libcxx/include/__memory/temporary_buffer.h
@@ -26,6 +26,9 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template <class _Tp>
 [[__nodiscard__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI _LIBCPP_DEPRECATED_IN_CXX17 pair<_Tp*, ptrdiff_t>
 get_temporary_buffer(ptrdiff_t __n) _NOEXCEPT {
+  if (__n <= 0)
+    return pair<_Tp*, ptrdiff_t>();
+
   __unique_temporary_buffer<_Tp> __unique_buf = std::__allocate_unique_temporary_buffer<_Tp>(__n);
   pair<_Tp*, ptrdiff_t> __result(__unique_buf.get(), __unique_buf.get_deleter().__count_);
   __unique_buf.release();
diff --git a/libcxx/test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp b/libcxx/test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
index 3fa9355270866..7c520e83f81f0 100644
--- a/libcxx/test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
+++ b/libcxx/test/std/utilities/memory/temporary.buffer/overaligned.pass.cpp
@@ -38,5 +38,19 @@ int main(int, char**)
     assert(reinterpret_cast<std::uintptr_t>(ip.first) % alignof(A) == 0);
     std::return_temporary_buffer(ip.first);
 
+    // C++17 [depr.temporary.buffer]/4
+    // Returns: If n <= 0 or if no storage could be obtained,
+    // returns a pair P such that P.first is a null pointer value and P.second == 0;
+    {
+      std::pair<A*, std::ptrdiff_t> ip = std::get_temporary_buffer<A>(0);
+      assert(ip.first == NULL);
+      assert(ip.second == 0);
+    }
+    {
+      std::pair<A*, std::ptrdiff_t> ip = std::get_temporary_buffer<A>(-5);
+      assert(ip.first == NULL);
+      assert(ip.second == 0);
+    }
+
   return 0;
 }
diff --git a/libcxx/test/std/utilities/memory/temporary.buffer/temporary_buffer.pass.cpp b/libcxx/test/std/utilities/memory/temporary.buffer/temporary_buffer.pass.cpp
index 39a4767d874e3..c2ee3900409fb 100644
--- a/libcxx/test/std/utilities/memory/temporary.buffer/temporary_buffer.pass.cpp
+++ b/libcxx/test/std/utilities/memory/temporary.buffer/temporary_buffer.pass.cpp
@@ -31,5 +31,19 @@ int main(int, char**)
     assert(ip.second == 5);
     std::return_temporary_buffer(ip.first);
 
+    // C++17 [depr.temporary.buffer]/4
+    // Returns: If n <= 0 or if no storage could be obtained,
+    // returns a pair P such that P.first is a null pointer value and P.second == 0;
+    {
+      std::pair<int*, std::ptrdiff_t> ip = std::get_temporary_buffer<int>(0);
+      assert(ip.first == NULL);
+      assert(ip.second == 0);
+    }
+    {
+      std::pair<int*, std::ptrdiff_t> ip = std::get_temporary_buffer<int>(-5);
+      assert(ip.first == NULL);
+      assert(ip.second == 0);
+    }
+
   return 0;
 }

``````````

</details>


https://github.com/llvm/llvm-project/pull/206871


More information about the libcxx-commits mailing list