Document number |
P3810R0 |
Date |
2025-08-14 |
Reply-to |
Jarrad J. Waterloo <descender76 at gmail dot com>
|
Audience |
SG23 Safety and Security |
hardened memory safety guarantees
Table of contents
Abstract
The C++
standard could say that all of its hardening is itself free of memory safety related undefined behavior if it required that STL implementations make the following functions free of memory safety related undefined behavior: size()
, empty()
, iterator difference, static_extent()
, extent()
, has_value()
, valueless_by_exception()
, holds_alternative<I>()
.
Following is hopefully a unique list of the hardened contract assertions currently proposed for the C++26
standard.
Standard Library Hardening
Minor additions to C++26 standard library hardening
Is it unreasonable to require a trivial to write member function to be written free of memory safety related undefined behavior? Consider for example the two most common ways to write a size member function.
Both of these common implementations are free of uninitialized, range access, null pointer dereference and use after free memory errors. Neither is their type safety errors. So requiring something that STL implementors are already doing does not seem unreasonable. It is similar for the other value semantic functions that simply return copies of members of the class in question.
Motivation
There has been sustained interest in contracts being free of undefined behavior. This was a part of the Contracts — Use Cases
back in 2020. There has been a lot of interest since.
2022 Contracts for C++: Prioritizing Safety
2024 P2900R6 May Be Minimal, but It Is Not Viable
2024 Contracts: Protecting The Protector
2024 Static analysis and 'safety' of Contracts, P2900 vs. P2680/P3285
2025 Contract concerns
Most every day programmers are more specifically concerned about the memory unsafety subset of undefined behavior. While we can't at this moment mandate that contracts are free of memory unsafety, can we at least offer the guarantee that the standard by default won't deliberately create hardened contracts with memory safety related undefined behavior without a very good reason for doing so.
Impact on the standard
Safety not only requires safety to be in the language but also a part of the libraries. This proposal provides safety guarantees in some portions of the standard by requiring STL implementers to make some of their functions safe.
References
Jarrad J. Waterloo <descender76 at gmail dot com>
hardened memory safety guarantees
Table of contents
Abstract
The
C++
standard could say that all of its hardening is itself free of memory safety related undefined behavior if it required that STL implementations make the following functions free of memory safety related undefined behavior:size()
,empty()
, iterator difference,static_extent()
,extent()
,has_value()
,valueless_by_exception()
,holds_alternative<I>()
.Following is hopefully a unique list of the hardened contract assertions currently proposed for the
C++26
standard.Standard Library Hardening
[1]Minor additions to C++26 standard library hardening
[2]Is it unreasonable to require a trivial to write member function to be written free of memory safety related undefined behavior? Consider for example the two most common ways to write a size member function.
Both of these common implementations are free of uninitialized, range access, null pointer dereference and use after free memory errors. Neither is their type safety errors. So requiring something that STL implementors are already doing does not seem unreasonable. It is similar for the other value semantic functions that simply return copies of members of the class in question.
Motivation
There has been sustained interest in contracts being free of undefined behavior. This was a part of the
Contracts — Use Cases
[3] [4] back in 2020. There has been a lot of interest since.2022
Contracts for C++: Prioritizing Safety
[5]2024
P2900R6 May Be Minimal, but It Is Not Viable
[6]2024
Contracts: Protecting The Protector
[7]2024
Static analysis and 'safety' of Contracts, P2900 vs. P2680/P3285
[8]2025
Contract concerns
[9]Most every day programmers are more specifically concerned about the memory unsafety subset of undefined behavior. While we can't at this moment mandate that contracts are free of memory unsafety, can we at least offer the guarantee that the standard by default won't deliberately create hardened contracts with memory safety related undefined behavior without a very good reason for doing so.
Impact on the standard
Safety not only requires safety to be in the language but also a part of the libraries. This proposal provides safety guarantees in some portions of the standard by requiring STL implementers to make some of their functions safe.
References
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3471r4.html ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3697r0.html ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1995r1.html#crit.noundef ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1995r1.html#crit.noassume ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2680r1.pdf ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3173r0.pdf ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3285r0.pdf ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3362r0.html ↩︎
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p3573r0.pdf ↩︎