<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58954>58954</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            pstl: a unit test causes heap-use-after-free
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mikekazakov
      </td>
    </tr>
</table>

<pre>
    The test for `uninitialized_fill/uninitialized_fill_n/destroy/destroy_n` causes double-init and double-free of objects.

The lines causing this behaviour are these:
https://github.com/llvm/llvm-project/blob/2be46b33d3d8bc383c529dca412b71f1f162a439/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_fill_destroy.pass.cpp#L73-L77

A simplified demonstration with ASAN instrumentation is available here: 
https://godbolt.org/z/ro96KPb9d

This could be fixed via this simple patch that removes automatic lifetime management of the test objects:
```
--- a/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_fill_destroy.pass.cpp
+++ b/pstl/test/std/utilities/memory/specialized.algorithms/uninitialized_fill_destroy.pass.cpp
@@ -71,9 +71,10 @@ test_uninitialized_fill_destroy_by_type()
     std::size_t N = 100000;
     for (size_t n = 0; n <= N; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
     {
-        std::unique_ptr<T[]> p(new T[n]);
-        invoke_on_all_policies(test_uninitialized_fill_destroy(), p.get(), std::next(p.get(), n), T(), n,
+        unsigned char *mem = new unsigned char[sizeof(T)*n];
+        invoke_on_all_policies(test_uninitialized_fill_destroy(),(T*)mem, std::next((T*)mem, n), T(), n,
                                std::is_trivial<T>());
+        delete [] mem;
     }
 }
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9VVFvnDgQ_jXwYoHAZll44GGTbV6uik66vCMbhsWNsSk22ya__sbAbrJt7qrTSXXY2DO2x998Mx4L075UTz0QB9aRzkwkyJNZSy2d5Eq-Ql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