{"id":2231098,"url":"http://patchwork.ozlabs.org/api/patches/2231098/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/patch/20260430110413.546515-1-tkaminsk@redhat.com/","project":{"id":17,"url":"http://patchwork.ozlabs.org/api/projects/17/?format=json","name":"GNU Compiler Collection","link_name":"gcc","list_id":"gcc-patches.gcc.gnu.org","list_email":"gcc-patches@gcc.gnu.org","web_url":null,"scm_url":null,"webscm_url":null,"list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20260430110413.546515-1-tkaminsk@redhat.com>","list_archive_url":null,"date":"2026-04-30T11:03:26","name":"libstdc++: Fix reserve of size_t(-1) elements in piecewise_constant_distribution. [PR113761]","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"11c44cbe64db886485ada727ec8e66a9e7202595","submitter":{"id":90409,"url":"http://patchwork.ozlabs.org/api/people/90409/?format=json","name":"Tomasz Kamiński","email":"tkaminsk@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/gcc/patch/20260430110413.546515-1-tkaminsk@redhat.com/mbox/","series":[{"id":502274,"url":"http://patchwork.ozlabs.org/api/series/502274/?format=json","web_url":"http://patchwork.ozlabs.org/project/gcc/list/?series=502274","date":"2026-04-30T11:03:26","name":"libstdc++: Fix reserve of size_t(-1) elements in piecewise_constant_distribution. [PR113761]","version":1,"mbox":"http://patchwork.ozlabs.org/series/502274/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2231098/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2231098/checks/","tags":{},"related":[],"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=R89EKGbF;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org\n (client-ip=38.145.34.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=R89EKGbF","sourceware.org; dmarc=pass (p=quarantine dis=none)\n header.from=redhat.com","sourceware.org; spf=pass smtp.mailfrom=redhat.com","server2.sourceware.org;\n arc=none smtp.remote-ip=170.10.133.124"],"Received":["from vm01.sourceware.org (vm01.sourceware.org [38.145.34.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 4g5rt863tgz1yHZ\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 21:05:04 +1000 (AEST)","from vm01.sourceware.org (localhost [127.0.0.1])\n\tby sourceware.org (Postfix) with ESMTP id BFC6F436F3DD\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 30 Apr 2026 11:05:01 +0000 (GMT)","from us-smtp-delivery-124.mimecast.com\n (us-smtp-delivery-124.mimecast.com [170.10.133.124])\n by sourceware.org (Postfix) with ESMTP id CEB10436F7DB\n for <gcc-patches@gcc.gnu.org>; Thu, 30 Apr 2026 11:04:20 +0000 (GMT)","from mx-prod-mc-06.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-532-yFl3doYuO4WlX7wBjQi2Wg-1; Thu,\n 30 Apr 2026 07:04:17 -0400","from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com\n (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4])\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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS\n id E17F5180065A; Thu, 30 Apr 2026 11:04:15 +0000 (UTC)","from localhost (unknown [10.44.32.8])\n by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP\n id 682F5300019F; Thu, 30 Apr 2026 11:04:15 +0000 (UTC)"],"DKIM-Filter":["OpenDKIM Filter v2.11.0 sourceware.org BFC6F436F3DD","OpenDKIM Filter v2.11.0 sourceware.org CEB10436F7DB"],"DMARC-Filter":"OpenDMARC Filter v1.4.2 sourceware.org CEB10436F7DB","ARC-Filter":"OpenARC Filter v1.0.0 sourceware.org CEB10436F7DB","ARC-Seal":"i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777547061; cv=none;\n b=hkgrHKNG68btK7CXdhp1dFuO+3V5pQURTGRTpXp+2ZwKhOVDbchrhOVPxRy2hcWEOcj6WLcdgP4j+GZ815VW7UFGLZo7g/lprgW/6BbHifM6DxWy17mrzgXdQVMNsjqG29+s1D8/0joqs8sbbmFfn/tB6SNVwuapknO1GWIxo4M=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=sourceware.org; s=key;\n t=1777547061; c=relaxed/simple;\n bh=P4hbKlU0/3OJBbv6cUoXj+sNYh4PSIoVKyTJlPRiDoU=;\n h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version;\n b=WHzCsDaQ0dJPl6qioZJZt5vX8Jfqk29iSRvn6Ad+qwm58BHwPL3fUoDs4EIHX+2quVuPpDDkVsprRswPjra71l8NZQjid95C8sl+U3++MAGYo7REeVZd9LTr0cSqdNlf6P6llxBS1CSTvCDZnCMM0dbd5ZXrlWizp+bsJpPL6UM=","ARC-Authentication-Results":"i=1; server2.sourceware.org","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n s=mimecast20190719; t=1777547060;\n h=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n to:to:cc:mime-version:mime-version:content-type:content-type:\n content-transfer-encoding:content-transfer-encoding;\n bh=Wk4g+qHbFjmd4f6z8cHWCfe9UYe/VN2v7+WqJUig5rU=;\n b=R89EKGbFlTc6nk1peB5sbix3tkm33DEw09AHcmoIh98M2JkCKbbp+j59unjWquTl7ba3YX\n fgUrfLcw2MCY/JXGYEO19AYIo6SieMflEcmuzOxjZUagsk/x463lUhM8pjWlulZlFg0yE5\n pCT5b2d8bXEQfWERWBEIrr40b4Lus34=","X-MC-Unique":"yFl3doYuO4WlX7wBjQi2Wg-1","X-Mimecast-MFC-AGG-ID":"yFl3doYuO4WlX7wBjQi2Wg_1777547056","From":"=?utf-8?q?Tomasz_Kami=C5=84ski?= <tkaminsk@redhat.com>","To":"libstdc++@gcc.gnu.org,\n\tgcc-patches@gcc.gnu.org","Subject":"[PATCH] libstdc++: Fix reserve of size_t(-1) elements in\n piecewise_constant_distribution. [PR113761]","Date":"Thu, 30 Apr 2026 13:03:26 +0200","Message-ID":"<20260430110413.546515-1-tkaminsk@redhat.com>","MIME-Version":"1.0","X-Scanned-By":"MIMEDefang 3.4.1 on 10.30.177.4","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"FQ8hMy69sXd-cLAk8ots9kapnbR3tKfj3LVuGpz-IE8_1777547056","X-Mimecast-Originator":"redhat.com","Content-Transfer-Encoding":"8bit","content-type":"text/plain; charset=\"US-ASCII\"; x-default=true","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"},"content":"The piecewise_constant_distribution constructor from std::initializer_list il,\nunconditionally _M_den reserved il.size()-1 elements. In case when the il.size()\nwas zero, this led to unsigned overflow, and attempt to allocate size_t(-1)\nelements.\n\nThis patch addresses above, by refactoring the constructors of param_type for\nboth piecewise_constant_distribution and piecewise_linear_distribution, to\nexit early (and do not populate internal vectors) if number of intervals range is\nsmaller than two. For the constructor accepting pair of iterators, this is done\nby checking result of __detail::__load_first2, that extracts up to two elements,\nand returns false, if less than two is found.\n\nFuthremore, we if the number of intervals is equal to two (for iterator __bbegin\nis at __bend after __load_first2), we store densities value on stack, and call\nnewly introduced an _M_initialize2 helper, that does not popluate internal vector\nif __ints and __dens values correspond to default configuration.\n\nWith both of above changes, we no longer populate _M_int and _M_den with default\nvalues, and corresponding code that clears them in _M_initialize is not necessary.\nThis avoids any uncessary memory allocatos.\n\nFinally, for constructor accepting two iterators, we reserve required space\nin _M_int vector, if the iterators are forward (or model sized_sentinel in C++20).\nThe _M_den initialization is performed afterwards, so _M_ints size is already\ndetermined (even for input iterators) and it can be used for call to reserve.\n\n\tPR libstdc++/113761\n\nlibstdc++-v3/ChangeLog:\n\n\t* include/bits/random.h\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\tRemove checks for default 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---\nTested on x86_64-linux locally. *piecewise* tests additionally tested\nwith all standard modes and modules. OK for trunk?\n\n libstdc++-v3/include/bits/random.h            |   6 +\n libstdc++-v3/include/bits/random.tcc          | 208 ++++++++++++++----\n .../cons/fallback.cc                          | 108 +++++++++\n .../cons/range.cc                             |  17 +-\n .../cons/fallback.cc                          | 127 +++++++++++\n .../cons/range.cc                             |  17 +-\n 6 files changed, 429 insertions(+), 54 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","diff":"diff --git a/libstdc++-v3/include/bits/random.h b/libstdc++-v3/include/bits/random.h\nindex 3fda69c4399..d8125bb0f62 100644\n--- a/libstdc++-v3/include/bits/random.h\n+++ b/libstdc++-v3/include/bits/random.h\n@@ -6468,6 +6468,9 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)\n \tvoid\n \t_M_initialize();\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 \tstd::vector<double> _M_cp;\n@@ -6748,6 +6751,9 @@ _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)\n \tvoid\n \t_M_initialize();\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 \tstd::vector<double> _M_cp;\ndiff --git a/libstdc++-v3/include/bits/random.tcc b/libstdc++-v3/include/bits/random.tcc\nindex 4475e365c2d..8d4e46c5168 100644\n--- a/libstdc++-v3/include/bits/random.tcc\n+++ b/libstdc++-v3/include/bits/random.tcc\n@@ -2947,16 +2947,6 @@ namespace __detail\n     piecewise_constant_distribution<_RealType>::param_type::\n     _M_initialize()\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_initialize();\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,20 +3013,38 @@ 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+#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_initialize();\n       }\n \n@@ -3006,10 +3054,18 @@ 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@@ -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@@ -3161,17 +3224,6 @@ namespace __detail\n     piecewise_linear_distribution<_RealType>::param_type::\n     _M_initialize()\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_initialize();\n+    }\n \n   template<typename _RealType>\n     template<typename _InputIteratorB, typename _InputIteratorW>\n@@ -3204,12 +3276,42 @@ 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+#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_initialize();\n       }\n \n@@ -3219,14 +3321,23 @@ 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_int = __bl;\n+\t_M_den.reserve(__bl.size());\n+\tfor (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)\n+\t    _M_den.push_back(__fw(*__biter));\n \t_M_initialize();\n       }\n \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);\ndiff --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\nnew file mode 100644\nindex 00000000000..1917e69972a\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 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 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 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+}\ndiff --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\nindex 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 }\ndiff --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\nnew file mode 100644\nindex 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+}\ndiff --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\nindex 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","prefixes":[]}