int, unsigned int and others
+ were designed to map closely to the underlying hardware. Computer hardware
+ implements these types as a fixed number of bits. When the result of
+ arithmetic operations exceeds this number of bits, the result will not be
+ arithmetically correct. The following example illustrates this
+ problem.safe_signed_range<MIN, MAX> and
+ safe_unsigned_range<MIN, MAX> which will throw an
+ exception if an attempt is made to store a result which is outside the
+ closed range [MIN, MAX].safe<int>,
+ safe<unsigned int> etc. These behave exactly like the
+ original ones EXCEPT that expressions involving these types are checked to
+ guarantee any possible arithmetic errors are trapped at compile time (if
+ possible) or at runtime. Since these types are meant to be "drop-in"
+ replacements - they function in all other ways the same as the built-in
+ types they are meant to replace. So things which are legal - such as
+ assigning an signed to unsigned value are not trapped at compile time - as
+ they are legal C/C++ code - but rather checked at runtime to trap the case
+ where this (legal) operation would lead to an arithmetically incorrect
+ result.![]() |
+Home | +Libraries | +People | +FAQ | +More | +
+ Secure + Coding in C and C++ + . 2nd Edition. Addison-Wesley Professional. April 12, 2013. 978-0321822130. Seacord
++ INT30-C. + Ensure that operations on unsigned integers do not wrap + . + Software Engineering Institute, + Carnegie Mellon University + . August 17, 2014. INT30-C
++ INT32-C. + Ensure that operations on signed integers do not result in + overflow + . + Software Engineering Institute, + Carnegie Mellon University + . August 17, 2014. INT32-C
++ Safe + Integer Operations + . + U.S. Department of + Homeland Security + . May 10, 2013. Plakosh
++ Understanding + Integer Overflow in C/C++ + . + Proceedings + of the 34th International Conference on Software Engineering (ICSE), + Zurich, Switzerland + . June 2012.
++ + C++ + Binary Fixed-Point Arithmetic + + . + JTC1/SC22/WG21 - + The C++ Standards Committee - ISOCPP + . January 15, 2012. Crowl
++ + C++ + Binary Fixed-Point Arithmetic + + . + ISO + C++ Standard Future Proposals + . Forum
++ Integer + Handling with the C++ SafeInt Class + . + Microsoft Developer Network + . January 7, 2004. LeBlanc
++ + SafeInt + code proposal + + . + Boost + Developer's List + . Katz
+| + | + |