<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3237: LWG 3038 and 3190 have inconsistent PRs</title>
<meta property="og:title" content="Issue 3237: LWG 3038 and 3190 have inconsistent PRs">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3237.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++20">C++20</a> status.</em></p>
<h3 id="3237"><a href="lwg-defects.html#3237">3237</a>. LWG 3038 and 3190 have inconsistent PRs</h3>
<p><b>Section:</b> 20.5.3.3 <a href="https://wg21.link/mem.poly.allocator.mem">[mem.poly.allocator.mem]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2019-07-18 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#mem.poly.allocator.mem">issues</a> in [mem.poly.allocator.mem].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Both LWG <a href="lwg-defects.html#3038" title="polymorphic_allocator::allocate should not allow integer overflow to create vulnerabilities (Status: C++20)">3038</a><sup><a href="https://cplusplus.github.io/LWG/issue3038" title="Latest snapshot">(i)</a></sup> and LWG <a href="lwg-defects.html#3190" title="std::allocator::allocate sometimes returns too little storage (Status: C++20)">3190</a><sup><a href="https://cplusplus.github.io/LWG/issue3190" title="Latest snapshot">(i)</a></sup> deal with how to respond to requests to allocate 
"<code>n * sizeof(T)</code>" bytes of memory when <code>n * sizeof(T)</code> is not sufficient storage for 
<code>n</code> objects of type <code>T</code>, i.e., when <code>n &gt; SIZE_MAX / sizeof(T)</code>. LWG 
<a href="lwg-defects.html#3038" title="polymorphic_allocator::allocate should not allow integer overflow to create vulnerabilities (Status: C++20)">3038</a><sup><a href="https://cplusplus.github.io/LWG/issue3038" title="Latest snapshot">(i)</a></sup> changed <code>polymorphic_allocator::allocate</code> to throw <code>length_error</code> upon 
detecting this condition, whereas LWG <a href="lwg-defects.html#3190" title="std::allocator::allocate sometimes returns too little storage (Status: C++20)">3190</a><sup><a href="https://cplusplus.github.io/LWG/issue3190" title="Latest snapshot">(i)</a></sup> changed <code>allocator::allocate</code> to 
throw <code>bad_array_new_length</code>. It's peculiar that two standard library components which allocate 
memory both detect this condition but handle it by throwing different exception types; for consistency, 
the two should be harmonized.
<p/>
Reflector discussion of 3190 seemed to achieve consensus that <code>bad_array_new_length</code> was 
the better option. Unlike <code>length_error</code>, <code>bad_array_new_length</code> derives from 
<code>bad_alloc</code> so we can make this change without altering the invariant that allocation functions 
either succeed or throw an exception derived from <code>bad_alloc</code>.
<p/>
Further, <a href="https://wg21.link/p0339r6">P0339R6</a> "<code>polymorphic_allocator&lt;&gt;</code> as a 
vocabulary type" recently added the function template "<code>template&lt;class T&gt; T* 
allocate_object(size_t n = 1);</code>" to <code>std::pmr::polymorphic_allocator</code>, which is another 
instance of the "allocate memory for <code>n</code> objects of type <code>T</code>" pattern. 
20.5.3.3 <a href="https://wg21.link/mem.poly.allocator.mem">[mem.poly.allocator.mem]</a> paragraph 8.1 specifies that <code>allocate_object</code> throws 
<code>length_error</code> when <code>SIZE_MAX / sizeof(T) &lt; n</code>, presumably for consistency with <code>std::pmr::polymorphic_allocator::allocate</code> specified in paragraph 1. <code>allocate_object</code>'s 
behavior should be consistent with <code>allocator::allocate</code> and 
<code>polymorphic_allocator::allocate</code> so we have a single means of communicating "request for 
allocation of unrepresentable size" errors in the Standard Library.
</p>

<p><i>[2020-02 Moved to Immediate on Thursday afternoon in Prague.]</i></p>



<p id="res-3237"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4820">N4820</a>.</p>

<ol>
<li><p>Modify 20.5.3.3 <a href="https://wg21.link/mem.poly.allocator.mem">[mem.poly.allocator.mem]</a> as indicated:</p>

<blockquote>
<pre>
[[nodiscard]] Tp* allocate(size_t n);
</pre><blockquote>
<p>
-1- <i>Effects:</i> If <code>SIZE_MAX / sizeof(Tp) &lt; n</code>, throws 
<code><del>length_error</del><ins>bad_array_new_length</ins></code>. 
Otherwise equivalent to:
</p>
<blockquote><pre>
return static_cast&lt;Tp*&gt;(memory_rsrc-&gt;allocate(n * sizeof(Tp), alignof(Tp)));
</pre>
</blockquote>
</blockquote>
[&hellip;]
<pre>
template&lt;class T&gt;
  T* allocate_object(size_t n = 1);
</pre><blockquote>
<p>
-8- <i>Effects:</i> Allocates memory suitable for holding an array of <code>n</code> objects of type 
<code>T</code>, as follows:
</p>
<ol style="list-style-type: none">
<li><p>(8.1) &mdash; if <code>SIZE_MAX / sizeof(T) &lt; n</code>, throws 
<code><del>length_error</del><ins>bad_array_new_length</ins></code>,</p></li>
<li><p>(8.2) &mdash; otherwise equivalent to:</p>
<blockquote><pre>
return static_cast&lt;T*&gt;(allocate_bytes(n*sizeof(T), alignof(T)));
</pre></blockquote>
</li>
</ol>
</blockquote>
</blockquote>
</li>
</ol>




</body>
</html>
