Skip to content

Commit 9126d4d

Browse files
Eisenwavetkoeppe
authored andcommitted
P3471R4 Standard library hardening
1 parent 103af7a commit 9126d4d

File tree

8 files changed

+158
-81
lines changed

8 files changed

+158
-81
lines changed

source/containers.tex

+50-32
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,10 @@
18611861
\result
18621862
\tcode{reference; const_reference} for constant \tcode{a}.
18631863

1864+
\pnum
1865+
\hardexpects
1866+
\tcode{a.empty()} is \tcode{false}.
1867+
18641868
\pnum
18651869
\returns
18661870
\tcode{*a.begin()}
@@ -1886,6 +1890,10 @@
18861890
\result
18871891
\tcode{reference; const_reference} for constant \tcode{a}.
18881892

1893+
\pnum
1894+
\hardexpects
1895+
\tcode{a.empty()} is \tcode{false}.
1896+
18891897
\pnum
18901898
\effects
18911899
Equivalent to:
@@ -2150,7 +2158,7 @@
21502158
\keyword{void}
21512159

21522160
\pnum
2153-
\expects
2161+
\hardexpects
21542162
\tcode{a.empty()} is \tcode{false}.
21552163

21562164
\pnum
@@ -2175,7 +2183,7 @@
21752183
\keyword{void}
21762184

21772185
\pnum
2178-
\expects
2186+
\hardexpects
21792187
\tcode{a.empty()} is \tcode{false}.
21802188

21812189
\pnum
@@ -2201,6 +2209,10 @@
22012209
\result
22022210
\tcode{reference; const_reference} for constant \tcode{a}.
22032211

2212+
\pnum
2213+
\hardexpects
2214+
\tcode{n < a.size()} is \tcode{true}.
2215+
22042216
\pnum
22052217
\effects
22062218
Equivalent to: \tcode{return *(a.begin() + n);}
@@ -19191,11 +19203,13 @@
1919119203
\begin{itemize}
1919219204
\item \range{first}{first + count} is a valid range.
1919319205
\item \tcode{It} models \libconcept{contiguous_iterator}.
19194-
\item
19195-
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19196-
then \tcode{count} is equal to \tcode{extent}.
1919719206
\end{itemize}
1919819207

19208+
\pnum
19209+
\hardexpects
19210+
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19211+
then \tcode{count == extent} is \tcode{true}.
19212+
1919919213
\pnum
1920019214
\effects
1920119215
Initializes \exposid{data_} with \tcode{to_address(first)} and
@@ -19231,14 +19245,16 @@
1923119245
\pnum
1923219246
\expects
1923319247
\begin{itemize}
19234-
\item
19235-
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19236-
then \tcode{last - first} is equal to \tcode{extent}.
1923719248
\item \range{first}{last} is a valid range.
1923819249
\item \tcode{It} models \libconcept{contiguous_iterator}.
1923919250
\item \tcode{End} models \tcode{\libconcept{sized_sentinel_for}<It>}.
1924019251
\end{itemize}
1924119252

19253+
\pnum
19254+
\hardexpects
19255+
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19256+
then \tcode{(last - first) == extent} is \tcode{true}.
19257+
1924219258
\pnum
1924319259
\effects
1924419260
Initializes \exposid{data_} with \tcode{to_address(first)} and
@@ -19309,14 +19325,17 @@
1930919325
\pnum
1931019326
\expects
1931119327
\begin{itemize}
19312-
\item If \tcode{extent} is not equal to \tcode{dynamic_extent},
19313-
then \tcode{ranges::size(r)} is equal to \tcode{extent}.
1931419328
\item \tcode{R} models \tcode{ranges::\libconcept{contiguous_range}} and
1931519329
\tcode{ranges::\libconcept{sized_range}}.
1931619330
\item If \tcode{is_const_v<element_type>} is \tcode{false},
1931719331
\tcode{R} models \tcode{ranges::\libconcept{borrowed_range}}.
1931819332
\end{itemize}
1931919333

19334+
\pnum
19335+
\hardexpects
19336+
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19337+
then \tcode{ranges::size(r) == extent} is \tcode{true}.
19338+
1932019339
\pnum
1932119340
\effects
1932219341
Initializes \exposid{data_} with \tcode{ranges::data(r)} and
@@ -19338,9 +19357,9 @@
1933819357
\tcode{is_const_v<element_type>} is \tcode{true}.
1933919358

1934019359
\pnum
19341-
\expects
19342-
If \tcode{extent} is not equal to \tcode{dynamic_extent}, then
19343-
\tcode{il.size()} is equal to \tcode{extent}.
19360+
\hardexpects
19361+
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19362+
then \tcode{il.size() == extent} is \tcode{true}.
1934419363

1934519364
\pnum
1934619365
\effects
@@ -19378,9 +19397,9 @@
1937819397
\end{itemize}
1937919398

1938019399
\pnum
19381-
\expects
19400+
\hardexpects
1938219401
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19383-
then \tcode{s.size()} is equal to \tcode{extent}.
19402+
then \tcode{s.size() == extent} is \tcode{true}.
1938419403

1938519404
\pnum
1938619405
\effects
@@ -19450,7 +19469,7 @@
1945019469
\tcode{Count <= Extent} is \tcode{true}.
1945119470

1945219471
\pnum
19453-
\expects
19472+
\hardexpects
1945419473
\tcode{Count <= size()} is \tcode{true}.
1945519474

1945619475
\pnum
@@ -19470,7 +19489,7 @@
1947019489
\tcode{Count <= Extent} is \tcode{true}.
1947119490

1947219491
\pnum
19473-
\expects
19492+
\hardexpects
1947419493
\tcode{Count <= size()} is \tcode{true}.
1947519494

1947619495
\pnum
@@ -19494,7 +19513,7 @@
1949419513
is \tcode{true}.
1949519514

1949619515
\pnum
19497-
\expects
19516+
\hardexpects
1949819517
\begin{codeblock}
1949919518
Offset <= size() && (Count == dynamic_extent || Count <= size() - Offset)
1950019519
\end{codeblock}
@@ -19525,7 +19544,7 @@
1952519544

1952619545
\begin{itemdescr}
1952719546
\pnum
19528-
\expects
19547+
\hardexpects
1952919548
\tcode{count <= size()} is \tcode{true}.
1953019549

1953119550
\pnum
@@ -19540,7 +19559,7 @@
1954019559

1954119560
\begin{itemdescr}
1954219561
\pnum
19543-
\expects
19562+
\hardexpects
1954419563
\tcode{count <= size()} is \tcode{true}.
1954519564

1954619565
\pnum
@@ -19556,7 +19575,7 @@
1955619575

1955719576
\begin{itemdescr}
1955819577
\pnum
19559-
\expects
19578+
\hardexpects
1956019579
\begin{codeblock}
1956119580
offset <= size() && (count == dynamic_extent || count <= size() - offset)
1956219581
\end{codeblock}
@@ -19614,7 +19633,7 @@
1961419633

1961519634
\begin{itemdescr}
1961619635
\pnum
19617-
\expects
19636+
\hardexpects
1961819637
\tcode{idx < size()} is \tcode{true}.
1961919638

1962019639
\pnum
@@ -19648,7 +19667,7 @@
1964819667

1964919668
\begin{itemdescr}
1965019669
\pnum
19651-
\expects
19670+
\hardexpects
1965219671
\tcode{empty()} is \tcode{false}.
1965319672

1965419673
\pnum
@@ -19667,7 +19686,7 @@
1966719686

1966819687
\begin{itemdescr}
1966919688
\pnum
19670-
\expects
19689+
\hardexpects
1967119690
\tcode{empty()} is \tcode{false}.
1967219691

1967319692
\pnum
@@ -23834,17 +23853,16 @@
2383423853

2383523854
\pnum
2383623855
\expects
23837-
\begin{itemize}
23838-
\item
23839-
For each rank index \tcode{r} of \tcode{extents_type},
23840-
\tcode{static_extent(r) == dynamic_extent || static_extent(r) == other.extent(r)}
23841-
is \tcode{true}.
23842-
\item
2384323856
$[0, \tcode{\exposid{map_}.required_span_size()})$ is
2384423857
an accessible range of \exposid{ptr_} and \exposid{acc_}
2384523858
for values of \exposid{ptr_}, \exposid{map_}, and \exposid{acc_}
2384623859
after the invocation of this constructor.
23847-
\end{itemize}
23860+
23861+
\pnum
23862+
\hardexpects
23863+
For each rank index \tcode{r} of \tcode{extents_type},
23864+
\tcode{static_extent(r) == dynamic_extent || static_extent(r) == other.extent(r)}
23865+
is \tcode{true}.
2384823866

2384923867
\pnum
2385023868
\effects
@@ -23890,7 +23908,7 @@
2389023908
Let \tcode{I} be \tcode{extents_type::\exposid{index-cast}(std::move(indices))}.
2389123909

2389223910
\pnum
23893-
\expects
23911+
\hardexpects
2389423912
\tcode{I} is a multidimensional index in \tcode{extents()}.
2389523913
\begin{note}
2389623914
This implies that

source/intro.tex

+11-1
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,8 @@
833833
\indextext{conformance requirements!library|)}
834834

835835
\pnum
836-
Two kinds of implementations are defined: a \defnadj{hosted}{implementation} and a
836+
An implementation is either a
837+
\defnadj{hosted}{implementation} or a
837838
\defnadj{freestanding}{implementation}.
838839
A freestanding
839840
implementation is one in which execution may take place without the benefit of
@@ -845,6 +846,15 @@
845846
described in \ref{lex} through \ref{\lastcorechapter} and
846847
the subset of the library facilities described in \ref{compliance}.
847848

849+
\pnum
850+
It is
851+
\impldef{whether the implementation is a hardened implementation}
852+
whether the implementation is a
853+
\defnadj{hardened}{implementation}.
854+
If it is a hardened implementation,
855+
violating a hardened precondition
856+
results in a contract violation\iref{structure.specifications}.
857+
848858
\pnum
849859
An implementation is encouraged to document its limitations in
850860
the size or complexity of the programs it can successfully process,

source/lib-intro.tex

+32-5
Original file line numberDiff line numberDiff line change
@@ -370,15 +370,33 @@
370370

371371
\item
372372
\expects
373-
the conditions
374-
that the function assumes to hold whenever it is called;
373+
conditions that the function assumes to hold whenever it is called;
375374
violation of any preconditions results in undefined behavior.
376375
\begin{example}
377376
An implementation can express some such conditions
378377
via the use of a contract assertion,
379378
such as a precondition assertion\iref{dcl.contract.func}.
380379
\end{example}
381380

381+
\item
382+
\hardexpects
383+
conditions that the function assumes to hold whenever it is called.
384+
\begin{itemize}
385+
\item
386+
When invoking the function in a hardened implementation,
387+
prior to any other observable side effects of the function,
388+
one or more contract assertions
389+
whose predicates are as described in the hardened precondition
390+
are evaluated with a checking semantic\iref{basic.contract.eval}.
391+
If any of these assertions is evaluated with a non-terminating semantic
392+
and the contract-violation handler returns,
393+
the program has undefined behavior.
394+
\item
395+
When invoking the function in a non-hardened implementation,
396+
if any hardened precondition is violated,
397+
the program has undefined behavior.
398+
\end{itemize}
399+
382400
\item
383401
\effects
384402
the actions performed by the function.
@@ -434,9 +452,18 @@
434452
If \tcode{F}'s semantics specifies any \Fundescx{Constraints} or \Fundescx{Mandates} elements,
435453
then those requirements are logically imposed prior to the \term{equivalent-to} semantics.
436454
Next, the semantics of the code sequence are determined by the
437-
\Fundescx{Constraints}, \Fundescx{Mandates}, \Fundescx{Preconditions}, \Fundescx{Effects},
438-
\Fundescx{Synchronization}, \Fundescx{Postconditions}, \Fundescx{Returns}, \Fundescx{Throws},
439-
\Fundescx{Complexity}, \Fundescx{Remarks}, and \Fundescx{Error conditions}
455+
\Fundescx{Constraints},
456+
\Fundescx{Mandates},
457+
\Fundescx{Preconditions},
458+
\Fundescx{Hardened preconditions},
459+
\Fundescx{Effects},
460+
\Fundescx{Synchronization},
461+
\Fundescx{Postconditions},
462+
\Fundescx{Returns},
463+
\Fundescx{Throws},
464+
\Fundescx{Complexity},
465+
\Fundescx{Remarks}, and
466+
\Fundescx{Error conditions}
440467
specified for the function invocations contained in the code sequence.
441468
The value returned from \tcode{F} is specified by \tcode{F}'s \Fundescx{Returns} element,
442469
or if \tcode{F} has no \Fundescx{Returns} element,

source/macros.tex

+1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@
366366
\newcommand{\constraints}{\Fundesc{Constraints}}
367367
\newcommand{\mandates}{\Fundesc{Mandates}}
368368
\newcommand{\expects}{\Fundesc{Preconditions}}
369+
\newcommand{\hardexpects}{\Fundesc{Hardened preconditions}}
369370
\newcommand{\effects}{\Fundesc{Effects}}
370371
\newcommand{\ensures}{\Fundesc{Postconditions}}
371372
\newcommand{\returns}{\Fundesc{Returns}}

source/numerics.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -7581,7 +7581,7 @@
75817581

75827582
\begin{itemdescr}
75837583
\pnum
7584-
\expects
7584+
\hardexpects
75857585
\tcode{n < size()} is \tcode{true}.
75867586

75877587
\pnum

0 commit comments

Comments
 (0)