Skip to content

Commit 38a9c90

Browse files
committed
P3471R4 Standard library hardening
1 parent 458b16a commit 38a9c90

File tree

8 files changed

+158
-81
lines changed

8 files changed

+158
-81
lines changed

source/containers.tex

Lines changed: 50 additions & 32 deletions
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);}
@@ -19105,11 +19117,13 @@
1910519117
\begin{itemize}
1910619118
\item \range{first}{first + count} is a valid range.
1910719119
\item \tcode{It} models \libconcept{contiguous_iterator}.
19108-
\item
19109-
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19110-
then \tcode{count} is equal to \tcode{extent}.
1911119120
\end{itemize}
1911219121

19122+
\pnum
19123+
\hardexpects
19124+
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19125+
then \tcode{count == extent} is \tcode{true}.
19126+
1911319127
\pnum
1911419128
\effects
1911519129
Initializes \exposid{data_} with \tcode{to_address(first)} and
@@ -19145,14 +19159,16 @@
1914519159
\pnum
1914619160
\expects
1914719161
\begin{itemize}
19148-
\item
19149-
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19150-
then \tcode{last - first} is equal to \tcode{extent}.
1915119162
\item \range{first}{last} is a valid range.
1915219163
\item \tcode{It} models \libconcept{contiguous_iterator}.
1915319164
\item \tcode{End} models \tcode{\libconcept{sized_sentinel_for}<It>}.
1915419165
\end{itemize}
1915519166

19167+
\pnum
19168+
\hardexpects
19169+
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19170+
then \tcode{(last - first) == extent} is \tcode{true}.
19171+
1915619172
\pnum
1915719173
\effects
1915819174
Initializes \exposid{data_} with \tcode{to_address(first)} and
@@ -19223,14 +19239,17 @@
1922319239
\pnum
1922419240
\expects
1922519241
\begin{itemize}
19226-
\item If \tcode{extent} is not equal to \tcode{dynamic_extent},
19227-
then \tcode{ranges::size(r)} is equal to \tcode{extent}.
1922819242
\item \tcode{R} models \tcode{ranges::\libconcept{contiguous_range}} and
1922919243
\tcode{ranges::\libconcept{sized_range}}.
1923019244
\item If \tcode{is_const_v<element_type>} is \tcode{false},
1923119245
\tcode{R} models \tcode{ranges::\libconcept{borrowed_range}}.
1923219246
\end{itemize}
1923319247

19248+
\pnum
19249+
\hardexpects
19250+
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19251+
then \tcode{ranges::size(r) == extent} is \tcode{true}.
19252+
1923419253
\pnum
1923519254
\effects
1923619255
Initializes \exposid{data_} with \tcode{ranges::data(r)} and
@@ -19252,9 +19271,9 @@
1925219271
\tcode{is_const_v<element_type>} is \tcode{true}.
1925319272

1925419273
\pnum
19255-
\expects
19256-
If \tcode{extent} is not equal to \tcode{dynamic_extent}, then
19257-
\tcode{il.size()} is equal to \tcode{extent}.
19274+
\hardexpects
19275+
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19276+
then \tcode{il.size() == extent} is \tcode{true}.
1925819277

1925919278
\pnum
1926019279
\effects
@@ -19292,9 +19311,9 @@
1929219311
\end{itemize}
1929319312

1929419313
\pnum
19295-
\expects
19314+
\hardexpects
1929619315
If \tcode{extent} is not equal to \tcode{dynamic_extent},
19297-
then \tcode{s.size()} is equal to \tcode{extent}.
19316+
then \tcode{s.size() == extent} is \tcode{true}.
1929819317

1929919318
\pnum
1930019319
\effects
@@ -19364,7 +19383,7 @@
1936419383
\tcode{Count <= Extent} is \tcode{true}.
1936519384

1936619385
\pnum
19367-
\expects
19386+
\hardexpects
1936819387
\tcode{Count <= size()} is \tcode{true}.
1936919388

1937019389
\pnum
@@ -19384,7 +19403,7 @@
1938419403
\tcode{Count <= Extent} is \tcode{true}.
1938519404

1938619405
\pnum
19387-
\expects
19406+
\hardexpects
1938819407
\tcode{Count <= size()} is \tcode{true}.
1938919408

1939019409
\pnum
@@ -19408,7 +19427,7 @@
1940819427
is \tcode{true}.
1940919428

1941019429
\pnum
19411-
\expects
19430+
\hardexpects
1941219431
\begin{codeblock}
1941319432
Offset <= size() && (Count == dynamic_extent || Count <= size() - Offset)
1941419433
\end{codeblock}
@@ -19439,7 +19458,7 @@
1943919458

1944019459
\begin{itemdescr}
1944119460
\pnum
19442-
\expects
19461+
\hardexpects
1944319462
\tcode{count <= size()} is \tcode{true}.
1944419463

1944519464
\pnum
@@ -19454,7 +19473,7 @@
1945419473

1945519474
\begin{itemdescr}
1945619475
\pnum
19457-
\expects
19476+
\hardexpects
1945819477
\tcode{count <= size()} is \tcode{true}.
1945919478

1946019479
\pnum
@@ -19470,7 +19489,7 @@
1947019489

1947119490
\begin{itemdescr}
1947219491
\pnum
19473-
\expects
19492+
\hardexpects
1947419493
\begin{codeblock}
1947519494
offset <= size() && (count == dynamic_extent || count <= size() - offset)
1947619495
\end{codeblock}
@@ -19528,7 +19547,7 @@
1952819547

1952919548
\begin{itemdescr}
1953019549
\pnum
19531-
\expects
19550+
\hardexpects
1953219551
\tcode{idx < size()} is \tcode{true}.
1953319552

1953419553
\pnum
@@ -19562,7 +19581,7 @@
1956219581

1956319582
\begin{itemdescr}
1956419583
\pnum
19565-
\expects
19584+
\hardexpects
1956619585
\tcode{empty()} is \tcode{false}.
1956719586

1956819587
\pnum
@@ -19581,7 +19600,7 @@
1958119600

1958219601
\begin{itemdescr}
1958319602
\pnum
19584-
\expects
19603+
\hardexpects
1958519604
\tcode{empty()} is \tcode{false}.
1958619605

1958719606
\pnum
@@ -23748,17 +23767,16 @@
2374823767

2374923768
\pnum
2375023769
\expects
23751-
\begin{itemize}
23752-
\item
23753-
For each rank index \tcode{r} of \tcode{extents_type},
23754-
\tcode{static_extent(r) == dynamic_extent || static_extent(r) == other.extent(r)}
23755-
is \tcode{true}.
23756-
\item
2375723770
$[0, \tcode{\exposid{map_}.required_span_size()})$ is
2375823771
an accessible range of \exposid{ptr_} and \exposid{acc_}
2375923772
for values of \exposid{ptr_}, \exposid{map_}, and \exposid{acc_}
2376023773
after the invocation of this constructor.
23761-
\end{itemize}
23774+
23775+
\pnum
23776+
\hardexpects
23777+
For each rank index \tcode{r} of \tcode{extents_type},
23778+
\tcode{static_extent(r) == dynamic_extent || static_extent(r) == other.extent(r)}
23779+
is \tcode{true}.
2376223780

2376323781
\pnum
2376423782
\effects
@@ -23804,7 +23822,7 @@
2380423822
Let \tcode{I} be \tcode{extents_type::\exposid{index-cast}(std::move(indices))}.
2380523823

2380623824
\pnum
23807-
\expects
23825+
\hardexpects
2380823826
\tcode{I} is a multidimensional index in \tcode{extents()}.
2380923827
\begin{note}
2381023828
This implies that

source/intro.tex

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,8 @@
822822
\indextext{conformance requirements!library|)}
823823

824824
\pnum
825-
Two kinds of implementations are defined: a \defnadj{hosted}{implementation} and a
825+
An implementation is either a
826+
\defnadj{hosted}{implementation} or a
826827
\defnadj{freestanding}{implementation}.
827828
A freestanding
828829
implementation is one in which execution may take place without the benefit of
@@ -834,6 +835,15 @@
834835
described in \ref{lex} through \ref{\lastcorechapter} and
835836
the subset of the library facilities described in \ref{compliance}.
836837

838+
\pnum
839+
It is
840+
\impldef{whether the implementation is a hardened implementation}
841+
whether the implementation is a
842+
\defnadj{hardened}{implementation}.
843+
If it is a hardened implementation,
844+
violating a hardened precondition
845+
results in a contract violation\iref{structure.specifications}.
846+
837847
\pnum
838848
An implementation is encouraged to document its limitations in
839849
the size or complexity of the programs it can successfully process,

source/lib-intro.tex

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,10 +370,28 @@
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

376+
\item
377+
\hardexpects
378+
conditions that the function assumes to hold whenever it is called.
379+
\begin{itemize}
380+
\item
381+
When invoking the function in a hardened implementation,
382+
prior to any other observable side effects of the function,
383+
one or more contract assertions
384+
whose predicates are as described in the hardened precondition
385+
are evaluated with a checking semantic\iref{basic.contract.eval}.
386+
If any of these assertions is evaluated with a non-terminating semantic
387+
and the contract-violation handler returns,
388+
the program has undefined behavior.
389+
\item
390+
When invoking the function in a non-hardened implementation,
391+
if any hardened precondition is violated,
392+
the program has undefined behavior.
393+
\end{itemize}
394+
377395
\item
378396
\effects
379397
the actions performed by the function.
@@ -424,9 +442,18 @@
424442
If \tcode{F}'s semantics specifies any \Fundescx{Constraints} or \Fundescx{Mandates} elements,
425443
then those requirements are logically imposed prior to the \term{equivalent-to} semantics.
426444
Next, the semantics of the code sequence are determined by the
427-
\Fundescx{Constraints}, \Fundescx{Mandates}, \Fundescx{Preconditions}, \Fundescx{Effects},
428-
\Fundescx{Synchronization}, \Fundescx{Postconditions}, \Fundescx{Returns}, \Fundescx{Throws},
429-
\Fundescx{Complexity}, \Fundescx{Remarks}, and \Fundescx{Error conditions}
445+
\Fundescx{Constraints},
446+
\Fundescx{Mandates},
447+
\Fundescx{Preconditions},
448+
\Fundescx{Hardened preconditions},
449+
\Fundescx{Effects},
450+
\Fundescx{Synchronization},
451+
\Fundescx{Postconditions},
452+
\Fundescx{Returns},
453+
\Fundescx{Throws},
454+
\Fundescx{Complexity},
455+
\Fundescx{Remarks}, and
456+
\Fundescx{Error conditions}
430457
specified for the function invocations contained in the code sequence.
431458
The value returned from \tcode{F} is specified by \tcode{F}'s \Fundescx{Returns} element,
432459
or if \tcode{F} has no \Fundescx{Returns} element,

source/macros.tex

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7494,7 +7494,7 @@
74947494

74957495
\begin{itemdescr}
74967496
\pnum
7497-
\expects
7497+
\hardexpects
74987498
\tcode{n < size()} is \tcode{true}.
74997499

75007500
\pnum

0 commit comments

Comments
 (0)