<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3652: Can we relax the preconditions of longjmp?</title>
<meta property="og:title" content="Issue 3652: Can we relax the preconditions of longjmp?">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3652.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#NAD">NAD</a> status.</em></p>
<h3 id="3652"><a href="lwg-closed.html#3652">3652</a>. Can we relax the preconditions of <code>longjmp</code>?</h3>
<p><b>Section:</b> 17.14.3 <a href="https://wg21.link/csetjmp.syn">[csetjmp.syn]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Jiang An <b>Opened:</b> 2021-12-15 <b>Last modified:</b> 2022-01-30</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
IMO the UB in 17.14.3 <a href="https://wg21.link/csetjmp.syn">[csetjmp.syn]</a>/2 is because of the design that "every destructor call 
for an object shall be in effect when the lifetime of that object ends". A similar requirement is 
in 6.8.4 <a href="https://wg21.link/basic.life">[basic.life]</a>/5, but it's more relaxed than the requirement for <code>setjmp</code>/<code>longjmp</code>.
It seems better to harmonize the requirements in 6.8.4 <a href="https://wg21.link/basic.life">[basic.life]</a>/5 and 
17.14.3 <a href="https://wg21.link/csetjmp.syn">[csetjmp.syn]</a>/2, which allows <code>longjmp</code> to skip non-trivial but no-op destructor calls. 
Given that Microsoft UCRT's <code>longjmp</code> can sometimes (but not always) call destructors, 
IMO we should say that it's unspecified whether <code>longjmp</code> calls destructor for any object 
whose lifetime ends.
</p>
<p><i>[2022-01-30
Duplicate of <a href="https://wg21.link/cwg2361">CWG 2361</a>.
Status changed: New &rarr; NAD.]</i></p>



<p id="res-3652"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/N4901" title=" Working Draft, Standard for Programming Language C++">N4901</a>. 
</p>

<ol>
<li><p>Modify 17.14.3 <a href="https://wg21.link/csetjmp.syn">[csetjmp.syn]</a> as indicated:</p>

<blockquote>
<p>
-2- The function signature <code>longjmp(jmp_buf jbuf, int val)</code> has more restricted behavior 
in this document. <del>A <code>setjmp</code>/<code>longjmp</code> call pair has undefined behavior 
i</del><ins>I</ins>f replacing the <code>setjmp</code> and <code>longjmp</code> <ins>in a 
<code>setjmp</code>/<code>longjmp</code> call pair</ins> by <code>catch</code> and <code>throw</code> would invoke any 
<del>non-trivial</del> destructors for any objects with automatic storage duration<ins>, it is 
unspecified whether the destructor is invoked for each such object. The behavior is undefined if the 
program depends on the side effects produced by any such unspecified destructor calls</ins>. A 
call to <code>setjmp</code> or <code>longjmp</code> has undefined behavior if invoked in a suspension 
context of a coroutine (7.6.2.4 <a href="https://wg21.link/expr.await">[expr.await]</a>).
</p>
</blockquote>
</li>
</ol>





</body>
</html>
