proposal.xml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd">
  4. <article>
  5. <articleinfo>
  6. <abstract>
  7. <para><informaltable>
  8. <tgroup cols="2">
  9. <colspec colwidth="1*"/>
  10. <colspec colwidth="2*"/>
  11. <tbody>
  12. <row>
  13. <entry>Document number:</entry>
  14. <entry>P0228R2</entry>
  15. </row>
  16. <row>
  17. <entry>Project:</entry>
  18. <entry>Programming Language C++</entry>
  19. </row>
  20. <row>
  21. <entry>Audience:</entry>
  22. <entry>SG-6</entry>
  23. </row>
  24. <row>
  25. <entry>Author:</entry>
  26. <entry>Robert Ramey</entry>
  27. </row>
  28. <row>
  29. <entry>Contact:</entry>
  30. <entry>ramey@rrsd.com</entry>
  31. </row>
  32. <row>
  33. <entry>Date:</entry>
  34. <entry>2016-02-16</entry>
  35. </row>
  36. </tbody>
  37. </tgroup>
  38. </informaltable></para>
  39. </abstract>
  40. <title>A Proposal to Add Safe Integer Types to the Standard
  41. Library</title>
  42. </articleinfo>
  43. <section>
  44. <title>Motivation</title>
  45. <para>Arithmetic operations in C++ are NOT guaranteed to yield a correct
  46. mathematical result. This feature is inherited from the early days of C.
  47. The behavior of <code>int</code>, <code>unsigned int</code> and others
  48. were designed to map closely to the underlying hardware. Computer hardware
  49. implements these types as a fixed number of bits. When the result of
  50. arithmetic operations exceeds this number of bits, the result will not be
  51. arithmetically correct. The following example illustrates this
  52. problem.</para>
  53. <programlisting>int f(int x, int y){
  54. // this returns an invalid result for some legal values of x and y !
  55. return x + y;
  56. }
  57. </programlisting>
  58. </section>
  59. <section>
  60. <title>Impact On the Standard</title>
  61. <para>This proposal is a pure library extension. It does not require
  62. changes to any standard classes, functions or headers. It might benefit
  63. from relaxing some of the conditions on aggregate types. It has been
  64. implemented in and requires standard C++/14.</para>
  65. </section>
  66. <section>
  67. <title>Design Decisions</title>
  68. <para>The template class is designed to function as closely as possible as
  69. a drop-in replacement for corresponding built-in integer types.</para>
  70. <orderedlist>
  71. <listitem>
  72. <para>"Drop In Replacement for Any Integer Type"</para>
  73. <para>The template class is designed to function as closely as
  74. possible as a drop-in replacement for corresponding built-in integer
  75. types. Ideally, one should be able to just substitute safe&lt;T&gt;
  76. for all instances of T in any program and expect it compile and
  77. execute as before with no other changes.</para>
  78. <para>Since C++ permits freely mixing signed and unsigned integer
  79. types in expressions, safe versions of these types can also be. This
  80. complicates the implementation of the library to significant
  81. degree.</para>
  82. </listitem>
  83. <listitem>
  84. <para>"Return No Incorrect Results"</para>
  85. <para>Usage of a safe type in a binary expression is guaranteed to
  86. either return an arithmetically correct result or throw a standard
  87. exception.</para>
  88. </listitem>
  89. <listitem>
  90. <para>"Automatically Inter operate with built-in integer types"</para>
  91. <para>The usage of a safe type in binary expression "infects" the
  92. expression by returning another safe type. This is designed to avoid
  93. accidentally losing the safety of the expression.</para>
  94. </listitem>
  95. <listitem>
  96. <para>"Uses &lt;limits&gt; instead of type traits"</para>
  97. <para>Implementation of a library such as this necessarily keeps track
  98. of the types of data objects. The most common way to do this is using
  99. type_traits such as <code>std::is_integral</code>,
  100. <code>std::is_unsigned</code>, <code>std::is_arithmetic</code>, etc.
  101. This doesn't work very well for a few reasons:<simplelist>
  102. <member>These are defined by the standard to apply only to
  103. built-in types. Specializing these traits for new types such as
  104. safe&lt;int&gt; would conflict with the standard.</member>
  105. </simplelist></para>
  106. <para><simplelist>
  107. <member>We are allowed to create specialization of
  108. std::numeric_limits for our own types - including safe&lt;T&gt;.
  109. So this works well for us.</member>
  110. </simplelist></para>
  111. <para><simplelist>
  112. <member>safe&lt;T&gt; might be implemented in such as way that it
  113. would work for unforeseen integer-like types such as "money".
  114. Numeric limits has more complete information about these types
  115. which might make it easier to extend the library.</member>
  116. </simplelist></para>
  117. </listitem>
  118. <listitem>
  119. <para>"Performance"</para>
  120. <para>Performance will depend on the implementation and subject to the
  121. constraints above. This design will permit the usage of template
  122. meta-programming to eliminate runtime performance penalties in some
  123. cases. In the following example, there is no runtime penalty required
  124. to guarantee that incorrect results will never be generated.</para>
  125. <programlisting>#include &lt;cstdint&gt;
  126. #include &lt;safe&gt;
  127. using namespace std;
  128. int f(safe&lt;int8_t&gt; i){
  129. // C++ promotion rules make overflow on multiplication impossible!
  130. // cannot fail on return
  131. // zero performance penalty
  132. return i * i;
  133. }
  134. int8_t f(safe&lt;int8_t&gt; i){
  135. // C++ promotion rules make overflow on multiplication impossible!
  136. // but result could be truncated on return
  137. // so result must be checked at runtime incurring a runtime penalty
  138. return i * i; // cannot overflow on multiplication,
  139. }</programlisting>
  140. <para>Some processors have the ability to detect erroneous results but
  141. the C++ language doesn't include the ability to exploit these
  142. features. Implementor's of this library will have the option to
  143. exploit these features to diminish or eliminate runtime costs.</para>
  144. <para>If all else fails and the runtime cost is deemed too large for
  145. the program to bear, users will have the option of creating their own
  146. aliases for the types the program uses and assign them according to
  147. the whether they are building a "Debug" or "Release" version. This is
  148. not ideal, but would still be preferable to the current approach which
  149. generally consists of ignoring the possibility that C++ numeric
  150. operations may produce arithmetically incorrect results.</para>
  151. </listitem>
  152. <listitem>
  153. <para>"No Extra Parameters"</para>
  154. <para>An alternative to this proposal would be a policy based design
  155. which would permit users to select or define actions to be taken in
  156. the case of errors. This is quite possible and likely useful. However,
  157. the simplicity usage of the current proposal is an important feature.
  158. So I decided not to include it.</para>
  159. </listitem>
  160. <listitem>
  161. <para>"No other safe types"</para>
  162. <para>Other ideas come to mind such as <code>safe&lt;Min,
  163. Max&gt;</code>, <code>safe_literal&lt;Value&gt;</code>, and others. I
  164. excluded these in the spirit of following the controlling purpose of
  165. making a "drop in replacement". Once one included these types into a
  166. program, they change the semantics of the program so that it's not
  167. really C++ any more. There is a place for these ideas, (see below),
  168. but I don't think the standard library is that place.</para>
  169. </listitem>
  170. </orderedlist>
  171. </section>
  172. <section>
  173. <title>Existing Implementations</title>
  174. <para>This proposal is a simpler version / subset of the Safe Numerics
  175. library in development by Robert Ramey on the <ulink
  176. url="http://rrsd.com/blincubator.com/bi_library/safe-numerics/?gform_post_id=426?">Boost
  177. Library Incubator</ulink>. It is compatible with this proposal but it also
  178. includes:<simplelist>
  179. <member>Policy classes for error handling</member>
  180. </simplelist></para>
  181. <para><simplelist>
  182. <member>Policy classes for type promotion. These permit substitution
  183. of C++ standard type promotion rules with other ones which can reduce
  184. or eliminate the need for runtime error checking code.</member>
  185. </simplelist></para>
  186. <para><simplelist>
  187. <member>Other safe types such as safe_integer_range&lt;Min,
  188. Max&gt;.</member>
  189. </simplelist></para>
  190. <para><simplelist>
  191. <member>Complete documentation including internal operation</member>
  192. </simplelist></para>
  193. <para>Without comment, here are implementations of libraries which are in
  194. some way similar to this proposal:<itemizedlist>
  195. <listitem>
  196. <para><ulink url="https://github.com/RobertLeahy/Safe">Robert Leahy,
  197. Safe integer utilities for C++11</ulink></para>
  198. </listitem>
  199. <listitem>
  200. <para><ulink url="http://safeint.codeplex.com">David LeBlanc,
  201. SafeInt</ulink></para>
  202. </listitem>
  203. <listitem>
  204. <para><ulink url="http://safeint.codeplex.com">David Stone, Bounded
  205. Integer</ulink></para>
  206. </listitem>
  207. </itemizedlist></para>
  208. </section>
  209. <section>
  210. <title>Technical Specifications</title>
  211. <section>
  212. <title>Type Requirements</title>
  213. <xi:include href="numeric_concept.xml" xpointer="element(/1)"
  214. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  215. <xi:include href="integer_concept.xml" xpointer="element(/1)"
  216. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  217. <xi:include href="safe_numeric_concept.xml" xpointer="element(/1)"
  218. xmlns:xi="http://www.w3.org/2001/XInclude"/>
  219. </section>
  220. </section>
  221. <section>
  222. <title>Types</title>
  223. <section id="safe_numerics.safe">
  224. <title>safe&lt;T&gt;</title>
  225. <section>
  226. <title>Description</title>
  227. <para>A <code>safe&lt;T&gt;</code> can be used anywhere a type T can
  228. be used. Any expression which uses this type is guaranteed to return
  229. an arithmetically correct value or trap in some way.</para>
  230. </section>
  231. <section>
  232. <title>Notation</title>
  233. <informaltable>
  234. <tgroup cols="2">
  235. <colspec align="left" colwidth="1*"/>
  236. <colspec align="left" colwidth="10*"/>
  237. <thead>
  238. <row>
  239. <entry align="left">Symbol</entry>
  240. <entry align="left">Description</entry>
  241. </row>
  242. </thead>
  243. <tbody>
  244. <row>
  245. <entry><code>T</code></entry>
  246. <entry>Underlying type from which a safe type is being
  247. derived</entry>
  248. </row>
  249. </tbody>
  250. </tgroup>
  251. </informaltable>
  252. </section>
  253. <section>
  254. <title>Template Parameters</title>
  255. <informaltable>
  256. <tgroup cols="3">
  257. <colspec colwidth="1*"/>
  258. <colspec align="left" colwidth="3*"/>
  259. <colspec align="left" colwidth="7*"/>
  260. <thead>
  261. <row>
  262. <entry align="left">Parameter</entry>
  263. <entry align="left">Type Requirements</entry>
  264. <entry>Description</entry>
  265. </row>
  266. </thead>
  267. <tbody>
  268. <row>
  269. <entry><code>T</code></entry>
  270. <entry><ulink
  271. url="http://en.cppreference.com/w/cpp/types/is_integral">Integer</ulink></entry>
  272. <entry><para>The underlying type. Currently only integer types
  273. supported</para></entry>
  274. </row>
  275. </tbody>
  276. </tgroup>
  277. </informaltable>
  278. <para>See examples below.</para>
  279. </section>
  280. <section>
  281. <title>Model of</title>
  282. <para><link linkend="safe_numerics.numeric">Integer</link></para>
  283. <para><link
  284. linkend="safe_numerics.safe_numeric_concept">SafeNumeric</link></para>
  285. </section>
  286. <section>
  287. <title>Valid Expressions</title>
  288. <para>Implements all expressions defined by the <link
  289. linkend="safe_numerics.safe_numeric_concept">SafeNumeric</link> type
  290. requirements.</para>
  291. <para><code>safe&lt;T&gt;</code> is meant to be a "drop-in"
  292. replacement of the intrinsic integer types.</para>
  293. <para>The type of an expression of type safe&lt;T&gt; op safe&lt;U&gt;
  294. will be safe&lt;R&gt; where R would be the same as the type of the
  295. expression T op U.That is, expressions involving these types will be
  296. evaluated into result types which reflect the standard rules for
  297. evaluation of C++ expressions. Should it occur that such evaluation
  298. cannot return a correct result, an std::exception will be
  299. thrown.</para>
  300. </section>
  301. <section>
  302. <title>Header</title>
  303. <para><filename><ulink url="../../include/safe_integer.hpp">#include
  304. &lt;safe&gt;</ulink></filename></para>
  305. </section>
  306. <section>
  307. <title>Example of use</title>
  308. <para><code>safe&lt;T&gt;</code> is meant to be a "drop-in"
  309. replacement of the intrinsic integer types. That is, expressions
  310. involving these types will be evaluated into result types which
  311. reflect the standard rules for evaluation of C++ expressions. Should
  312. it occur that such evaluation cannot return a correct result, an
  313. exception will be thrown.The following program will throw an exception
  314. and emit a error message at runtime if any of several events result in
  315. an incorrect arithmetic type. Behavior of this program could vary
  316. according to the machine architecture in question.</para>
  317. <programlisting>#include &lt;exception&gt;
  318. #include &lt;iostream&gt;
  319. #include &lt;safe&gt;
  320. void f(){
  321. using namespace std;
  322. safe&lt;int&gt; j;
  323. try {
  324. safe&lt;int&gt; i;
  325. cin &gt;&gt; i; // could throw overflow !
  326. j = i * i; // could throw overflow
  327. }
  328. catch(std::exception &amp; e){
  329. std::cout &lt;&lt; e.what() &lt;&lt; endl;
  330. }
  331. std::cout &lt;&lt; j;
  332. }</programlisting>
  333. </section>
  334. </section>
  335. </section>
  336. <section>
  337. <title>Acknowledgements</title>
  338. <para>This proposal is a simplified version of Safe Numeics library
  339. proposed for Boost. This effort was inspired by <ulink
  340. url="http://safeint.codeplex.com">David LeBlanc's SafeInt Library</ulink>
  341. .</para>
  342. </section>
  343. <section id="safe_numerics.bibliography">
  344. <title>References</title>
  345. <biblioentry>
  346. <author>
  347. <surname>Omer Katz</surname>
  348. </author>
  349. <title>
  350. <ulink url="http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?">
  351. <ulink
  352. url="http://boost.2283326.n4.nabble.com/SafeInt-code-proposal-td2663669.html">SafeInt
  353. code proposal</ulink>
  354. </ulink>
  355. </title>
  356. <publishername>
  357. <ulink
  358. url="https://groups.google.com/a/isocpp.org/forum/?fromgroups#!forum/std-proposals">Boost
  359. Developer's List</ulink>
  360. </publishername>
  361. <abbrev>Katz</abbrev>
  362. <abstract>
  363. <para>Posts of various authors regarding a proposed SafeInt library
  364. for boost</para>
  365. </abstract>
  366. </biblioentry>
  367. <biblioentry>
  368. <author>
  369. <surname>David LeBlanc</surname>
  370. </author>
  371. <title>
  372. <ulink
  373. url="https://msdn.microsoft.com/en-us/library/ms972705.aspx">Integer
  374. Handling with the C++ SafeInt Class</ulink>
  375. </title>
  376. <publishername>
  377. <ulink url="https://www.cert.org">Microsoft Developer Network</ulink>
  378. </publishername>
  379. <date>January 7, 2004</date>
  380. <abbrev>LeBlanc</abbrev>
  381. </biblioentry>
  382. <biblioentry>
  383. <author>
  384. <surname>David LeBlanc</surname>
  385. </author>
  386. <title>
  387. <ulink url="https://safeint.codeplex.com">SafeInt</ulink>
  388. </title>
  389. <publishername>
  390. <ulink url="https://www.cert.org">CodePlex</ulink>
  391. </publishername>
  392. <date>Dec 3, 2014</date>
  393. <abbrev>LeBlanc</abbrev>
  394. </biblioentry>
  395. <biblioentry>
  396. <author>
  397. <surname>Jacques-Louis Lions</surname>
  398. </author>
  399. <title>
  400. <ulink
  401. url="https://en.wikisource.org/wiki/Ariane_501_Inquiry_Board_report">Ariane
  402. 501 Inquiry Board report</ulink>
  403. </title>
  404. <publishername>
  405. <ulink
  406. url="https://en.wikisource.org/wiki/Main_Page">Wikisource</ulink>
  407. </publishername>
  408. <date>July 19, 1996</date>
  409. <abbrev>Lions</abbrev>
  410. </biblioentry>
  411. <biblioentry>
  412. <author>
  413. <surname>Daniel Plakosh</surname>
  414. </author>
  415. <title>
  416. <ulink
  417. url="https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/312-BSI.html">Safe
  418. Integer Operations</ulink>
  419. </title>
  420. <publishername>
  421. <ulink url="https://buildsecurityin.us-cert.gov">U.S. Department of
  422. Homeland Security</ulink>
  423. </publishername>
  424. <date>May 10, 2013</date>
  425. <abbrev>Plakosh</abbrev>
  426. </biblioentry>
  427. <biblioentry id="Seacord">
  428. <author>
  429. <surname>Robert C. Seacord</surname>
  430. </author>
  431. <title>
  432. <ulink
  433. url="http://www.cert.org/secure-coding/publications/books/secure-coding-c-c-second-edition.cfm?">Secure
  434. Coding in C and C++</ulink>
  435. </title>
  436. <edition>2nd Edition</edition>
  437. <publishername>Addison-Wesley Professional</publishername>
  438. <date>April 12, 2013</date>
  439. <isbn>978-0321822130</isbn>
  440. <abbrev>Seacord</abbrev>
  441. </biblioentry>
  442. <biblioentry>
  443. <author>
  444. <surname>Robert C. Seacord</surname>
  445. </author>
  446. <title>
  447. <ulink
  448. url="https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow?showComments=false">INT30-C.
  449. Ensure that operations on unsigned integers do not wrap</ulink>
  450. </title>
  451. <publishername>
  452. <ulink url="https://www.cert.org">Software Engineering Institute,
  453. Carnegie Mellon University</ulink>
  454. </publishername>
  455. <date>August 17, 2014</date>
  456. <abbrev>INT30-C</abbrev>
  457. </biblioentry>
  458. <biblioentry>
  459. <author>
  460. <surname id="INT32-C">Robert C. Seacord</surname>
  461. </author>
  462. <title>
  463. <ulink
  464. url="https://www.securecoding.cert.org/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow">INT32-C.
  465. Ensure that operations on signed integers do not result in
  466. overflow</ulink>
  467. </title>
  468. <publishername>
  469. <ulink url="https://www.cert.org">Software Engineering Institute,
  470. Carnegie Mellon University</ulink>
  471. </publishername>
  472. <date>August 17, 2014</date>
  473. <abbrev>INT32-C</abbrev>
  474. </biblioentry>
  475. </section>
  476. </article>