[{"id":3690990,"web_url":"http://patchwork.ozlabs.org/comment/3690990/","msgid":"<agSngQH7dEtzoJbA@zen.kayari.org>","list_archive_url":null,"date":"2026-05-13T16:32:01","subject":"Re: [PATCH v3] libstdc++: Fix reserve of size_t(-1) elements in\n piecewise_constant_distribution. [PR113761]","submitter":{"id":48004,"url":"http://patchwork.ozlabs.org/api/people/48004/","name":"Jonathan Wakely","email":"jwakely@redhat.com"},"content":"On Wed, 13 May 2026 at 16:52 +0200, Tomasz Kamiński wrote:\n>The piecewise_constant_distribution constructor from std::initializer_list il,\n>unconditionally reserve _M_den for il.size()-1 elements. In case when the il.size()\n>was zero, this led to unsigned overflow, and attempt to allocate size_t(-1)\n>elements.\n>\n>This patch addresses above, by refactoring the constructors of param_type for\n>both piecewise_constant_distribution and piecewise_linear_distribution, to\n>exit early (and do not populate internal vectors) if number of intervals range is\n>smaller than two. For the constructor accepting pair of iterators, this is done\n>by checking result of __detail::__load_first2, that extracts up to two elements,\n>and returns false, if less than two is found.\n>\n>Furthermore, we if the number of intervals is equal to two (for iterator __bbegin\n>is at __bend after __load_first2), we store densities value on stack, and call\n>newly introduced an _M_initialize2 helper, that does not populate internal vector\n>if __ints and __dens values correspond to default configuration.\n>\n>With both of above changes, we no longer populate _M_int and _M_den with default\n>values, and corresponding code that clears them in _M_initialize is not necessary.\n>This avoids any unnecessary memory allocators.\n>\n>For constructor accepting two iterators, we reserve required space in _M_int vector,\n>if the iterators are forward (or model sized_sentinel in C++20). The _M_den\n>initialization is performed afterwards, so _M_int size is already determined\n>(even for input iterators) and it can be used for call to reserve.\n>\n>Finally, the _M_initialize members are renamed to _M_configure, so the new\n>implementation that skips the check, is not used by TU compiler will other\n>that would invoke it with empty vector, and thus retrigger the issue.\n>\n>\tPR libstdc++/113761\n>\n>libstdc++-v3/ChangeLog:\n>\n>\t* include/bits/random.h\n>\t(piecewise_constant_distribution::param_type::_M_initialize)\n>\t(piecewise_linear_distribution::param_type::_M_initialize): Remove.\n>\t(piecewise_constant_distribution::param_type::_M_configure)\n>\t(piecewise_linear_distribution::param_type::_M_configure): Define.\n>\t(piecewise_constant_distribution::param_type::_M_initialize2)\n>\t(piecewise_linear_distribution::param_type::_M_initialize2): Declare.\n>\t* include/bits/random.tcc (__detail::__load_first2): Define.\n>\t(piecewise_constant_distribution::param_type::_M_initialize)\n>\t(piecewise_linear_distribution::param_type::_M_initialize):\n>\tRename to...\n>\t(piecewise_constant_distribution::param_type::_M_configure)\n>\t(piecewise_linear_distribution::param_type::_M_configure):\n>\tRenamed implementation of _M_initialize, that removes checks for\n>\tdefault values.\n>\t(piecewise_constant_distribution::param_type::_M_initialize2)\n>\t(piecewise_linear_distribution::param_type::_M_initialize2): Define.\n>\t(piecewise_constant_distribution::param_type::param_type)\n>\t(piecewise_linear_distribution::param_type::param_type):\n>\tExit early for less that two intervals. Use _M_initialize2 to handle\n>\ttwo intervals case. Reserve _M_int for iterators case.\n>\t* testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc:\n>\tTest input and forward iterators, in addition to random_access ones.\n>\t* testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc:\n>\tLikewise.\n>\t* testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc:\n>\tNew test.\n>\t* testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc:\n>\tNew test.\n>---\n>v2 changes:\n>- rename _M_intialize to _M_configure to avoid ABI break, I used configure\n>  as name, because we call it with already initialized _M_int and _M_den\n>- updated commit description to fix the typos and unclear sentences\n>- added explanation for rename to commit description\n>- moved * to type in _M_initialize2 signatures\n>- used for-each to initialize entities for picewise_linear_distribution\n>  constructor from initializer_list\n>I haven't updated the copying from __wbegin to use _M_assign, as the\n>conditions need to look at reference type (if it is _Real), so we add\n>complexity, without real benefit.\n>\n>Testing on x86_64-linux. *picewise* test passed again, so didn't make\n>new typos. OK for trunk when all test passes?\n\nOK\n\n\n> libstdc++-v3/include/bits/random.h            |  10 +-\n> libstdc++-v3/include/bits/random.tcc          | 224 ++++++++++++++----\n> .../cons/fallback.cc                          | 108 +++++++++\n> .../cons/range.cc                             |  17 +-\n> .../cons/fallback.cc                          | 127 ++++++++++\n> .../cons/range.cc                             |  17 +-\n> 6 files changed, 439 insertions(+), 64 deletions(-)\n> create mode 100644 libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc\n> create mode 100644 libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc\n>\n>diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h\n>index 3fda69c4399..5d037465d3c 100644\n>--- a/libstdc++-v3/include/bits/random.h\n>+++ b/libstdc++-v3/include/bits/random.h\n>@@ -6466,7 +6466,10 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)\n>\n>       private:\n> \tvoid\n>-\t_M_initialize();\n>+\t_M_configure();\n>+\n>+\tvoid\n>+\t_M_initialize2(const _RealType* __ints, _RealType __den);\n>\n> \tstd::vector<_RealType> _M_int;\n> \tstd::vector<double> _M_den;\n>@@ -6746,7 +6749,10 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)\n>\n>       private:\n> \tvoid\n>-\t_M_initialize();\n>+\t_M_configure();\n>+\n>+\tvoid\n>+\t_M_initialize2(const _RealType* __ints, const _RealType* __dens);\n>\n> \tstd::vector<_RealType> _M_int;\n> \tstd::vector<double> _M_den;\n>diff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc\n>index 4475e365c2d..80b51d099fa 100644\n>--- a/libstdc++-v3/include/bits/random.tcc\n>+++ b/libstdc++-v3/include/bits/random.tcc\n>@@ -2945,18 +2945,8 @@ namespace __detail\n>   template<typename _RealType>\n>     void\n>     piecewise_constant_distribution<_RealType>::param_type::\n>-    _M_initialize()\n>+    _M_configure()\n>     {\n>-      if (_M_int.size() < 2\n>-\t  || (_M_int.size() == 2\n>-\t      && _M_int[0] == _RealType(0)\n>-\t      && _M_int[1] == _RealType(1)))\n>-\t{\n>-\t  _M_int.clear();\n>-\t  _M_den.clear();\n>-\t  return;\n>-\t}\n>-\n>       const double __sum = std::accumulate(_M_den.begin(),\n> \t\t\t\t\t   _M_den.end(), 0.0);\n>       __glibcxx_assert(__sum > 0);\n>@@ -2975,6 +2965,46 @@ namespace __detail\n> \t_M_den[__k] /= _M_int[__k + 1] - _M_int[__k];\n>     }\n>\n>+  template<typename _RealType>\n>+    void\n>+    piecewise_constant_distribution<_RealType>::param_type::\n>+    _M_initialize2(const _RealType* __ints, _RealType __den)\n>+    {\n>+      if (__ints[0] == _RealType(0) && __ints[1] == _RealType(1))\n>+\treturn;\n>+\n>+      _M_int.reserve(2);\n>+      _M_int.push_back(__ints[0]);\n>+      _M_int.push_back(__ints[1]);\n>+\n>+      _M_den.reserve(1);\n>+      _M_den.push_back(__den);\n>+      _M_configure();\n>+    }\n>+\n>+namespace __detail\n>+{\n>+  template<typename _InputIterator, typename _RealType>\n>+    bool\n>+    __load_first2(_InputIterator& __first, _InputIterator __last,\n>+\t\t  _RealType* __out)\n>+    {\n>+      if (__first == __last)\n>+\treturn false;\n>+\n>+      *__out = *__first;\n>+      ++__first;\n>+      if (__first == __last)\n>+\treturn false;\n>+\n>+      ++__out;\n>+      *__out = *__first;\n>+      ++__first;\n>+      return true;\n>+    }\n>+} // namespace __detail\n>+\n>+\n>   template<typename _RealType>\n>     template<typename _InputIteratorB, typename _InputIteratorW>\n>       piecewise_constant_distribution<_RealType>::param_type::\n>@@ -2983,21 +3013,39 @@ namespace __detail\n> \t\t _InputIteratorW __wbegin)\n>       : _M_int(), _M_den(), _M_cp()\n>       {\n>-\tif (__bbegin != __bend)\n>-\t  {\n>-\t    for (;;)\n>-\t      {\n>-\t\t_M_int.push_back(*__bbegin);\n>-\t\t++__bbegin;\n>-\t\tif (__bbegin == __bend)\n>-\t\t  break;\n>+\t_RealType __ints[2];\n>+\tif (!__detail::__load_first2(__bbegin, __bend, __ints))\n>+\t  return;\n>\n>-\t\t_M_den.push_back(*__wbegin);\n>-\t\t++__wbegin;\n>-\t      }\n>+\tif (__bbegin == __bend)\n>+\t  {\n>+\t    _M_initialize2(__ints, *__wbegin);\n>+\t    return;\n> \t  }\n>\n>-\t_M_initialize();\n>+#if __glibcxx_concepts // C++ >= C++20\n>+\tif constexpr (sized_sentinel_for<_InputIteratorB, _InputIteratorB>\n>+\t\t\t|| forward_iterator<_InputIteratorB>)\n>+\t  _M_int.reserve(2 + size_t(ranges::distance(__bbegin, __bend)));\n>+#else\n>+#pragma GCC diagnostic push\n>+#pragma GCC diagnostic ignored \"-Wc++17-extensions\" // if constexpr\n>+\tif constexpr (is_convertible<__iter_category_t<_InputIteratorB>,\n>+\t\t\t\t     forward_iterator_tag>::value)\n>+\t  _M_int.reserve(2 + size_t(std::distance(__bbegin, __bend)));\n>+#pragma GCC diagnostic pop\n>+#endif\n>+\n>+\t_M_int.push_back(__ints[0]);\n>+\t_M_int.push_back(__ints[1]);\n>+\tfor (; __bbegin != __bend; ++__bbegin)\n>+\t  _M_int.push_back(*__bbegin);\n>+\n>+\t_M_den.reserve(_M_int.size() - 1);\n>+\tfor (size_t __k = 0; __k < _M_int.size() - 1; (void)++__k, ++__wbegin)\n>+\t  _M_den.push_back(*__wbegin);\n>+\n>+\t_M_configure();\n>       }\n>\n>   template<typename _RealType>\n>@@ -3006,15 +3054,23 @@ namespace __detail\n>       param_type(initializer_list<_RealType> __bl, _Func __fw)\n>       : _M_int(), _M_den(), _M_cp()\n>       {\n>-\t_M_int.reserve(__bl.size());\n>-\tfor (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)\n>-\t  _M_int.push_back(*__biter);\n>+\tif (__bl.size() < 2)\n>+\t  return;\n>\n>+\tif (__bl.size() == 2)\n>+\t  {\n>+\t    const _RealType *__ints = __bl.begin();\n>+\t    _RealType __den = __fw(0.5 * (__ints[1] + __ints[0]));\n>+\t    _M_initialize2(__ints, __den);\n>+\t    return;\n>+\t  }\n>+\n>+\t_M_int = __bl;\n> \t_M_den.reserve(_M_int.size() - 1);\n> \tfor (size_t __k = 0; __k < _M_int.size() - 1; ++__k)\n> \t  _M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));\n>\n>-\t_M_initialize();\n>+\t_M_configure();\n>       }\n>\n>   template<typename _RealType>\n>@@ -3025,6 +3081,13 @@ namespace __detail\n>       {\n> \tconst size_t __n = __nw == 0 ? 1 : __nw;\n> \tconst _RealType __delta = (__xmax - __xmin) / __n;\n>+\tif (__n == 1)\n>+\t  {\n>+\t    _RealType __ints[2] = { __xmin, __xmin + __delta };\n>+\t    _RealType __den = __fw(__xmin * 0.5 * __delta);\n>+\t    _M_initialize2(__ints, __den);\n>+\t    return;\n>+\t  }\n>\n> \t_M_int.reserve(__n + 1);\n> \tfor (size_t __k = 0; __k <= __nw; ++__k)\n>@@ -3034,7 +3097,7 @@ namespace __detail\n> \tfor (size_t __k = 0; __k < __nw; ++__k)\n> \t  _M_den.push_back(__fw(_M_int[__k] + 0.5 * __delta));\n>\n>-\t_M_initialize();\n>+\t_M_configure();\n>       }\n>\n>   template<typename _RealType>\n>@@ -3159,19 +3222,8 @@ namespace __detail\n>   template<typename _RealType>\n>     void\n>     piecewise_linear_distribution<_RealType>::param_type::\n>-    _M_initialize()\n>+    _M_configure()\n>     {\n>-      if (_M_int.size() < 2\n>-\t  || (_M_int.size() == 2\n>-\t      && _M_int[0] == _RealType(0)\n>-\t      && _M_int[1] == _RealType(1)\n>-\t      && _M_den[0] == _M_den[1]))\n>-\t{\n>-\t  _M_int.clear();\n>-\t  _M_den.clear();\n>-\t  return;\n>-\t}\n>-\n>       double __sum = 0.0;\n>       _M_cp.reserve(_M_int.size() - 1);\n>       _M_m.reserve(_M_int.size() - 1);\n>@@ -3194,7 +3246,27 @@ namespace __detail\n>\n>       //  Make sure the last cumulative probablility is one.\n>       _M_cp[_M_cp.size() - 1] = 1.0;\n>-     }\n>+    }\n>+\n>+  template<typename _RealType>\n>+    void\n>+    piecewise_linear_distribution<_RealType>::param_type::\n>+    _M_initialize2(const _RealType* __ints, const _RealType* __dens)\n>+    {\n>+      if (__ints[0] == _RealType(0)\n>+\t  && __ints[1] == _RealType(1)\n>+\t  && __dens[0] == __dens[1])\n>+\treturn;\n>+\n>+      _M_int.reserve(2);\n>+      _M_int.push_back(__ints[0]);\n>+      _M_int.push_back(__ints[1]);\n>+\n>+      _M_den.reserve(2);\n>+      _M_den.push_back(__dens[0]);\n>+      _M_den.push_back(__dens[1]);\n>+      _M_configure();\n>+    }\n>\n>   template<typename _RealType>\n>     template<typename _InputIteratorB, typename _InputIteratorW>\n>@@ -3204,13 +3276,43 @@ namespace __detail\n> \t\t _InputIteratorW __wbegin)\n>       : _M_int(), _M_den(), _M_cp(), _M_m()\n>       {\n>-\tfor (; __bbegin != __bend; ++__bbegin, (void) ++__wbegin)\n>+\t_RealType __ints[2];\n>+\tif (!__detail::__load_first2(__bbegin, __bend, __ints))\n>+\t  return;\n>+\n>+\tif (__bbegin == __bend)\n> \t  {\n>-\t    _M_int.push_back(*__bbegin);\n>-\t    _M_den.push_back(*__wbegin);\n>+\t    _RealType __dens[2];\n>+\t    __dens[0] = *__wbegin;\n>+\t    ++__wbegin;\n>+\t    __dens[1] = *__wbegin;\n>+\t    _M_initialize2(__ints, __dens);\n>+\t    return;\n> \t  }\n>\n>-\t_M_initialize();\n>+#if __glibcxx_concepts // C++ >= C++20\n>+\tif constexpr (sized_sentinel_for<_InputIteratorB, _InputIteratorB>\n>+\t\t\t|| forward_iterator<_InputIteratorB>)\n>+\t  _M_int.reserve(2 + size_t(ranges::distance(__bbegin, __bend)));\n>+#else\n>+#pragma GCC diagnostic push\n>+#pragma GCC diagnostic ignored \"-Wc++17-extensions\" // if constexpr\n>+\tif constexpr (is_convertible<__iter_category_t<_InputIteratorB>,\n>+\t\t\t\t     forward_iterator_tag>::value)\n>+\t  _M_int.reserve(2 + size_t(std::distance(__bbegin, __bend)));\n>+#pragma GCC diagnostic pop\n>+#endif\n>+\n>+\t_M_int.push_back(__ints[0]);\n>+\t_M_int.push_back(__ints[1]);\n>+\tfor (; __bbegin != __bend; ++__bbegin)\n>+\t  _M_int.push_back(*__bbegin);\n>+\n>+\t_M_den.reserve(_M_int.size());\n>+\tfor (size_t __i = 0; __i < _M_int.size(); (void)++__i, ++__wbegin)\n>+\t  _M_den.push_back(*__wbegin);\n>+\n>+\t_M_configure();\n>       }\n>\n>   template<typename _RealType>\n>@@ -3219,15 +3321,24 @@ namespace __detail\n>       param_type(initializer_list<_RealType> __bl, _Func __fw)\n>       : _M_int(), _M_den(), _M_cp(), _M_m()\n>       {\n>-\t_M_int.reserve(__bl.size());\n>-\t_M_den.reserve(__bl.size());\n>-\tfor (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)\n>+\tif (__bl.size() < 2)\n>+\t  return;\n>+\n>+\tif (__bl.size() == 2)\n> \t  {\n>-\t    _M_int.push_back(*__biter);\n>-\t    _M_den.push_back(__fw(*__biter));\n>+\t    const _RealType *__ints = __bl.begin();\n>+\t    _RealType __den[2];\n>+\t    __den[0] = __fw(__ints[0]);\n>+\t    __den[1] = __fw(__ints[1]);\n>+\t    _M_initialize2(__ints, __den);\n>+\t    return;\n> \t  }\n>\n>-\t_M_initialize();\n>+\t_M_int = __bl;\n>+\t_M_den.reserve(__bl.size());\n>+\tfor (_RealType __b : __bl)\n>+\t  _M_den.push_back(__fw(__b));\n>+\t_M_configure();\n>       }\n>\n>   template<typename _RealType>\n>@@ -3238,6 +3349,15 @@ namespace __detail\n>       {\n> \tconst size_t __n = __nw == 0 ? 1 : __nw;\n> \tconst _RealType __delta = (__xmax - __xmin) / __n;\n>+\tif (__n == 1)\n>+\t  {\n>+\t    _RealType __ints[2] = { __xmin, __xmin + __delta };\n>+\t    _RealType __dens[2];\n>+\t    __dens[0] = __fw(__ints[0]);\n>+\t    __dens[1] = __fw(__ints[1]);\n>+\t    _M_initialize2(__ints, __dens);\n>+\t    return;\n>+\t  }\n>\n> \t_M_int.reserve(__n + 1);\n> \t_M_den.reserve(__n + 1);\n>@@ -3247,7 +3367,7 @@ namespace __detail\n> \t    _M_den.push_back(__fw(_M_int[__k] + __delta));\n> \t  }\n>\n>-\t_M_initialize();\n>+\t_M_configure();\n>       }\n>\n>   template<typename _RealType>\n>diff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc\n>new file mode 100644\n>index 00000000000..e03a1e187a7\n>--- /dev/null\n>+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/fallback.cc\n>@@ -0,0 +1,108 @@\n>+// { dg-do run { target c++11 } }\n>+// { dg-require-cstdint \"\" }\n>+\n>+#include <random>\n>+#include <testsuite_hooks.h>\n>+#include <testsuite_iterators.h>\n>+\n>+using dist = std::piecewise_linear_distribution<>;\n>+\n>+template<typename... Args>\n>+bool\n>+is_default(Args&&... args)\n>+{\n>+  std::piecewise_constant_distribution<> u(std::forward<Args>(args)...);\n>+\n>+  std::vector<double> ints = u.intervals();\n>+  if (ints.size() != 2 || ints[0] != 0.0 || ints[1] != 1.0)\n>+    return false;\n>+\n>+  std::vector<double> dens = u.densities();\n>+  if (dens.size() != 1 || dens[0] != 1.0)\n>+    return false;\n>+\n>+  return true;\n>+}\n>+\n>+template<template<typename> class Range>\n>+void\n>+test_it_pair()\n>+{\n>+  double wt[2];\n>+  double x[3];\n>+\n>+  Range<double> r0(x, x);\n>+  VERIFY( is_default(r0.begin(), r0.end(), wt) );\n>+\n>+  x[0] = 4.2;\n>+  Range<double> r1a(x, x+1);\n>+  VERIFY( is_default(r1a.begin(), r1a.end(), wt) );\n>+\n>+  x[0] = 0.0;\n>+  Range<double> r1b(x, x+1);\n>+  VERIFY( is_default(r1b.begin(), r1b.end(), wt) );\n>+\n>+  x[1] = 1.0;\n>+  wt[0] = 13.0;\n>+  Range<double> r2a(x, x+2);\n>+  VERIFY( is_default(r2a.begin(), r2a.end(), wt) );\n>+\n>+  wt[0] = 4.2;\n>+  Range<double> r2b(x, x+2);\n>+  VERIFY( is_default(r2b.begin(), r2b.end(), wt) );\n>+\n>+  x[1] = 0.5;\n>+  Range<double> r2c(x, x+2);\n>+  VERIFY( !is_default(r2c.begin(), r2c.end(), wt) );\n>+\n>+  x[2] = 1.0;\n>+  wt[1] = 2.0;\n>+  Range<double> r3(x, x+3);\n>+  VERIFY( !is_default(r3.begin(), r3.end(), wt) );\n>+}\n>+\n>+void test_init_list()\n>+{\n>+  auto c = [](double x) { return 10; };\n>+  VERIFY(  is_default(std::initializer_list<double>{}, c) );\n>+  VERIFY(  is_default(std::initializer_list<double>{4.2}, c) );\n>+  VERIFY(  is_default(std::initializer_list<double>{0.0, 1.0}, c) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, c) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, c) );\n>+\n>+  auto rel = [](double x) { return x; };\n>+  VERIFY(  is_default(std::initializer_list<double>{}, rel) );\n>+  VERIFY(  is_default(std::initializer_list<double>{4.2}, rel) );\n>+  VERIFY(  is_default(std::initializer_list<double>{0.0, 1.0}, rel) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, rel) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, rel) );\n>+}\n>+\n>+void test_xbound()\n>+{\n>+  auto c = [](double x) { return 10; };\n>+  VERIFY(  is_default(0, 0.0, 1.0, c) );\n>+  VERIFY( !is_default(0, 0.0, 0.5, c) );\n>+  VERIFY(  is_default(1, 0.0, 1.0, c) );\n>+  VERIFY( !is_default(1, 0.0, 0.5, c) );\n>+  VERIFY( !is_default(2, 0.0, 1.0, c) );\n>+\n>+  auto rel = [](double x) { return x; };\n>+  VERIFY(  is_default(0, 0.0, 1.0, rel) );\n>+  VERIFY( !is_default(0, 0.0, 0.5, rel) );\n>+  VERIFY(  is_default(1, 0.0, 1.0, rel) );\n>+  VERIFY( !is_default(1, 0.0, 0.5, rel) );\n>+  VERIFY( !is_default(2, 0.0, 1.0, rel) );\n>+}\n>+\n>+int main()\n>+{\n>+  using namespace __gnu_test;\n>+  test_it_pair<input_container>();\n>+  test_it_pair<forward_container>();\n>+  test_it_pair<random_access_container>();\n>+\n>+  test_init_list();\n>+  test_xbound();\n>+  return 0;\n>+}\n>diff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc\n>index 3714e460391..4bfa20ea3bb 100644\n>--- a/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc\n>+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_constant_distribution/cons/range.cc\n>@@ -25,13 +25,17 @@\n>\n> #include <random>\n> #include <testsuite_hooks.h>\n>+#include <testsuite_iterators.h>\n>\n>+template<template<typename> class Range>\n> void\n>-test01()\n>+test_it_pair()\n> {\n>-  std::vector<double> x = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};\n>-  std::vector<double> wt = {0.5, 1.0, 2.5, 1.5, 0.5};\n>-  std::piecewise_constant_distribution<> u(x.begin(), x.end(), wt.begin());\n>+  double x[6] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};\n>+  double wt[5] = {0.5, 1.0, 2.5, 1.5, 0.5};\n>+\n>+  Range<double> r(x, x+6);\n>+  std::piecewise_constant_distribution<> u(r.begin(), r.end(), wt);\n>   std::vector<double> interval = u.intervals();\n>   std::vector<double> density = u.densities();\n>   VERIFY( interval.size() == 6 );\n>@@ -44,6 +48,9 @@ test01()\n>\n> int main()\n> {\n>-  test01();\n>+  using namespace __gnu_test;\n>+  test_it_pair<input_container>();\n>+  test_it_pair<forward_container>();\n>+  test_it_pair<random_access_container>();\n>   return 0;\n> }\n>diff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc\n>new file mode 100644\n>index 00000000000..b2e6ca65123\n>--- /dev/null\n>+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/fallback.cc\n>@@ -0,0 +1,127 @@\n>+// { dg-do run { target c++11 } }\n>+// { dg-require-cstdint \"\" }\n>+\n>+#include <random>\n>+#include <testsuite_hooks.h>\n>+#include <testsuite_iterators.h>\n>+\n>+using dist = std::piecewise_linear_distribution<>;\n>+\n>+template<typename... Args>\n>+bool\n>+is_default(Args&&... args)\n>+{\n>+  std::piecewise_linear_distribution<> u(std::forward<Args>(args)...);\n>+\n>+  std::vector<double> ints = u.intervals();\n>+  if (ints.size() != 2 || ints[0] != 0.0 || ints[1] != 1.0)\n>+    return false;\n>+\n>+  std::vector<double> dens = u.densities();\n>+  if (dens.size() != 2 || dens[0] != 1.0 || dens[1] != 1.0)\n>+    return false;\n>+\n>+  return true;\n>+}\n>+\n>+template<template<typename> class Range>\n>+void\n>+test_it_pair()\n>+{\n>+  double wt[3];\n>+  double x[3];\n>+\n>+  Range<double> r0(x, x);\n>+  VERIFY( is_default(r0.begin(), r0.end(), wt) );\n>+\n>+  x[0] = 4.2;\n>+  Range<double> r1a(x, x+1);\n>+  VERIFY( is_default(r1a.begin(), r1a.end(), wt) );\n>+\n>+  x[0] = 0.0;\n>+  Range<double> r1b(x, x+1);\n>+  VERIFY( is_default(r1b.begin(), r1b.end(), wt) );\n>+\n>+  x[1] = 1.0;\n>+  wt[0] = wt[1] = 13.0;\n>+  Range<double> r2a(x, x+2);\n>+  VERIFY( is_default(r2a.begin(), r2a.end(), wt) );\n>+\n>+  wt[0] = wt[1] = 1.0;\n>+  Range<double> r2b(x, x+2);\n>+  VERIFY( is_default(r2b.begin(), r2b.end(), wt) );\n>+\n>+  wt[1] = 2.0;\n>+  Range<double> r2c(x, x+2);\n>+  VERIFY( !is_default(r2c.begin(), r2c.end(), wt) );\n>+\n>+  x[1] = 0.5;\n>+  wt[0] = wt[1] = 2.0;\n>+  Range<double> r2d(x, x+2);\n>+  VERIFY( !is_default(r2d.begin(), r2d.end(), wt) );\n>+\n>+  x[2] = 1.0;\n>+  wt[2] = wt[0];\n>+  Range<double> r3(x, x+3);\n>+  VERIFY( !is_default(r3.begin(), r3.end(), wt) );\n>+}\n>+\n>+void test_init_list()\n>+{\n>+  auto c1 = [](double x) { return 1.0; };\n>+  VERIFY(  is_default(std::initializer_list<double>{}, c1) );\n>+  VERIFY(  is_default(std::initializer_list<double>{4.2}, c1) );\n>+  VERIFY(  is_default(std::initializer_list<double>{0.0, 1.0}, c1) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, c1) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, c1) );\n>+\n>+  auto c2 = [](double x) { return 4.2; };\n>+  VERIFY(  is_default(std::initializer_list<double>{}, c2) );\n>+  VERIFY(  is_default(std::initializer_list<double>{4.2}, c2) );\n>+  VERIFY(  is_default(std::initializer_list<double>{0.0, 1.0}, c2) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, c2) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, c2) );\n>+\n>+  auto id = [](double x) { return x; };\n>+  VERIFY(  is_default(std::initializer_list<double>{}, id) );\n>+  VERIFY(  is_default(std::initializer_list<double>{4.2}, id) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 1.0}, id) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5}, id) );\n>+  VERIFY( !is_default(std::initializer_list<double>{0.0, 0.5, 1.0}, id) );\n>+}\n>+\n>+void test_xbound()\n>+{\n>+  auto c1 = [](double x) { return 1.0; };\n>+  VERIFY(  is_default(0, 0.0, 1.0, c1) );\n>+  VERIFY( !is_default(0, 0.0, 0.5, c1) );\n>+  VERIFY(  is_default(1, 0.0, 1.0, c1) );\n>+  VERIFY( !is_default(1, 0.0, 0.5, c1) );\n>+  VERIFY( !is_default(2, 0.0, 1.0, c1) );\n>+\n>+  auto c2 = [](double x) { return 4.2; };\n>+  VERIFY(  is_default(0, 0.0, 1.0, c2) );\n>+  VERIFY( !is_default(0, 0.0, 0.5, c2) );\n>+  VERIFY(  is_default(1, 0.0, 1.0, c2) );\n>+  VERIFY( !is_default(1, 0.0, 0.5, c2) );\n>+  VERIFY( !is_default(2, 0.0, 1.0, c2) );\n>+\n>+  auto id = [](double x) { return x; };\n>+  VERIFY( !is_default(0, 0.0, 1.0, id) );\n>+  VERIFY( !is_default(0, 0.0, 0.5, id) );\n>+  VERIFY( !is_default(1, 0.0, 1.0, id) );\n>+  VERIFY( !is_default(1, 0.0, 0.5, id) );\n>+  VERIFY( !is_default(2, 0.0, 1.0, id) );\n>+}\n>+\n>+int main()\n>+{\n>+  using namespace __gnu_test;\n>+  test_it_pair<input_container>();\n>+  test_it_pair<forward_container>();\n>+  test_it_pair<random_access_container>();\n>+\n>+  test_init_list();\n>+  test_xbound();\n>+  return 0;\n>+}\n>diff --git a/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc\n>index c698e7a66e6..af979fdac42 100644\n>--- a/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc\n>+++ b/libstdc++-v3/testsuite/26_numerics/random/piecewise_linear_distribution/cons/range.cc\n>@@ -25,13 +25,17 @@\n>\n> #include <random>\n> #include <testsuite_hooks.h>\n>+#include <testsuite_iterators.h>\n>\n>+template<template<typename> class Range>\n> void\n>-test01()\n>+test_it_pair()\n> {\n>-  std::vector<double> x = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};\n>-  std::vector<double> wt = {0.0, 1.0, 2.5, 1.5, 3.5, 0.0};\n>-  std::piecewise_linear_distribution<> u(x.begin(), x.end(), wt.begin());\n>+  double x[] = {0.0, 1.0, 2.0, 3.0, 4.0, 5.0};\n>+  double wt[] = {0.0, 1.0, 2.5, 1.5, 3.5, 0.0};\n>+\n>+  Range<double> r(x, x+6);\n>+  std::piecewise_linear_distribution<> u(r.begin(), r.end(), wt);\n>   std::vector<double> interval = u.intervals();\n>   std::vector<double> density = u.densities();\n>   VERIFY( interval.size() == 6 );\n>@@ -44,6 +48,9 @@ test01()\n>\n> int main()\n> {\n>-  test01();\n>+  using namespace __gnu_test;\n>+  test_it_pair<input_container>();\n>+  test_it_pair<forward_container>();\n>+  test_it_pair<random_access_container>();\n>   return 0;\n> }\n>-- \n>2.54.0\n>\n>","headers":{"Return-Path":"<gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org>","X-Original-To":["incoming@patchwork.ozlabs.org","gcc-patches@gcc.gnu.org"],"Delivered-To":["patchwork-incoming@legolas.ozlabs.org","gcc-patches@gcc.gnu.org"],"Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=LITV999j;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=2620:52:6:3111::32; helo=vm01.sourceware.org;\n envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org;\n receiver=patchwork.ozlabs.org)","sourceware.org;\n\tdkim=pass (1024-bit key,\n unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=LITV999j","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","sourceware.org; arc=none smtp.remote-ip=170.10.129.124"],"Received":["from vm01.sourceware.org (vm01.sourceware.org\n [IPv6:2620:52:6:3111::32])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4gFzc45Ftnz1y5L\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 14 May 2026 02:36:03 +1000 (AEST)","from vm01.sourceware.org (localhost [IPv6:::1])\n\tby sourceware.org (Postfix) with ESMTP id 7D6544BB8F45\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 13 May 2026 16:36:01 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.129.124])\n by sourceware.org (Postfix) with ESMTP id 036174BBC093\n for <gcc-patches@gcc.gnu.org>; Wed, 13 May 2026 16:32:08 +0000 (GMT)","from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com\n (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by\n relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n cipher=TLS_AES_256_GCM_SHA384) id us-mta-631-ONdGn6qoO6mOXF4USfyCkQ-1; Wed,\n 13 May 2026 12:32:07 -0400","from mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.111])\n (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n (No client certificate requested)\n by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id EFA8D18002D7; Wed, 13 May 2026 16:32:04 +0000 (UTC)","from localhost (unknown [10.44.48.10])\n by mx-prod-int-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP\n id E61981800576; Wed, 13 May 2026 16:32:03 +0000 (UTC)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org 7D6544BB8F45","OpenDKIM Filter v2.11.0 sourceware.org 036174BBC093"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org 036174BBC093","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org 036174BBC093","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778689929; cv=none;\n b=DjKYcxW820/PoOceCP+niyPeg6xtwWagls3I+hr5CiyNz38W8LhTPOvUMSEr0QTLKxtRMG74N2VPYHKANJQepHObL17TckWo3INYZ/kcmwVPFzRHofwe22Wmf/xwVfphf4VUcqFF8GsJroGMzleoO5ZsV+7WTUtzEYHI3n5AWUo=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1778689929; c=relaxed/simple;\n bh=7Z+kRhjyxIiZMbU6FLAq4bEpgR2KA2aHD2u359APvKA=;\n h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version;\n b=ctTQKGJpUcuOKSFEUef392AbB4aFxM1fQqZUTb6d5GWryBsNFinj8d8en+4myn1twJ/PXlIi5SMtbt9X7QyRJWRWTe3Oz+YndsGURMks3Bgh1URWqxj/Fyrm+kxTNB5sYOJ7iP5OcxtAFZtAAV47d0T/sPo0FRvhDGv2XFS1vTQ=","ARC-Authentication-Results":"i=1; sourceware.org;\n dkim=pass (1024-bit key, unprotected)\n header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256\n header.s=mimecast20190719 header.b=LITV999j","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1778689928;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding:\n in-reply-to:in-reply-to:references:references;\n bh=S2Welvp67o6myW6NGxAw9bpy7DI4fnJ8A72IE8Ph3Wg=;\n b=LITV999jcWkMPOXOb+5ZBjUPiY6Vfpmhdpba95jCqTlGEKO9O4BNWmICZV8tA4G9hKHYd7\n TPvqSRAEJMAWyybgUyuaLL9dp0jbm4uBd6SqCVusKWsB9XZplvl1KAz8f+limPr+v8wZoA\n C3K7SFNP9IxkfID6hDAFK/dbREd5pRk=","X-MC-Unique":"ONdGn6qoO6mOXF4USfyCkQ-1","X-Mimecast-MFC-AGG-ID":"ONdGn6qoO6mOXF4USfyCkQ_1778689925","Date":"Wed, 13 May 2026 17:32:01 +0100","From":"Jonathan Wakely <jwakely@redhat.com>","To":"Tomasz =?utf-8?b?S2FtacWEc2tp?= <tkaminsk@redhat.com>","Cc":"libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org","Subject":"Re: [PATCH v3] libstdc++: Fix reserve of size_t(-1) elements in\n piecewise_constant_distribution. [PR113761]","Message-ID":"<agSngQH7dEtzoJbA@zen.kayari.org>","References":"\n <CAKvuMXAFRPq7=z9PsMPwZnJUtBYgJ_85nCY=WWHC=+79Yc32Qw@mail.gmail.com>\n <20260513150327.910897-1-tkaminsk@redhat.com>","MIME-Version":"1.0","In-Reply-To":"<20260513150327.910897-1-tkaminsk@redhat.com>","X-Clacks-Overhead":"GNU Terry Pratchett","X-Scanned-By":"MIMEDefang 3.4.1 on 10.30.177.111","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"JtbRzWAbNXiqGUkFIAYdEKk_F7IpcVopW61yKmAWHDc_1778689925","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain; charset=utf-8; format=flowed","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","X-BeenThere":"gcc-patches@gcc.gnu.org","X-Mailman-Version":"2.1.30","Precedence":"list","List-Id":"Gcc-patches mailing list <gcc-patches.gcc.gnu.org>","List-Unsubscribe":"<https://gcc.gnu.org/mailman/options/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe>","List-Archive":"<https://gcc.gnu.org/pipermail/gcc-patches/>","List-Post":"<mailto:gcc-patches@gcc.gnu.org>","List-Help":"<mailto:gcc-patches-request@gcc.gnu.org?subject=help>","List-Subscribe":"<https://gcc.gnu.org/mailman/listinfo/gcc-patches>,\n <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe>","Errors-To":"gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org"}}]