1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> 4<title>Array Index Value Can Exceed Array Limits</title> 5<link rel="stylesheet" href="../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../index.html" title="Safe Numerics"> 8<link rel="up" href="../tutorial.html" title="Tutorial and Motivating Examples"> 9<link rel="prev" href="5.html" title="Mixing Data Types Can Create Subtle Errors"> 10<link rel="next" href="7.html" title="Checking of Input Values Can Be Easily Overlooked"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img href="index.html" height="164px" src="pre-boost.jpg" alt="Library Documentation Index"></td> 15<td><h2>Safe Numerics</h2></td> 16</tr></table> 17<div class="spirit-nav"> 18<a accesskey="p" href="5.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="7.html"><img src="../images/next.png" alt="Next"></a> 19</div> 20<div class="section"> 21<div class="titlepage"><div><div><h3 class="title"> 22<a name="safe_numerics.tutorial.6"></a>Array Index Value Can Exceed Array Limits</h3></div></div></div> 23<p>Using an intrinsic C++ array, it's very easy to exceed array limits. 24 This can fail to be detected when it occurs and create bugs which are hard 25 to find. There are several ways to address this, but one of the simplest 26 would be to use safe_unsigned_range;</p> 27<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">stdexcept</span><span class="special">></span> 28<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">iostream</span><span class="special">></span> 29<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">array</span><span class="special">></span> 30 31<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">safe_numerics</span><span class="special">/</span><span class="identifier">safe_integer_range</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span> 32 33<span class="keyword">void</span> <span class="identifier">detected_msg</span><span class="special">(</span><span class="keyword">bool</span> <span class="identifier">detected</span><span class="special">)</span><span class="special">{</span> 34 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="special">(</span><span class="identifier">detected</span> <span class="special">?</span> <span class="string">"error detected!"</span> <span class="special">:</span> <span class="string">"error NOT detected! "</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 35<span class="special">}</span> 36 37<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span> <span class="special">*</span><span class="special">[</span><span class="special">]</span><span class="special">)</span><span class="special">{</span> 38 <span class="comment">// problem: array index values can exceed array bounds</span> 39 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"example 5: "</span><span class="special">;</span> 40 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"array index values can exceed array bounds"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 41 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Not using safe numerics"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 42 <span class="identifier">std</span><span class="special">::</span><span class="identifier">array</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="number">37</span><span class="special">></span> <span class="identifier">i_array</span><span class="special">;</span> 43 44 <span class="comment">// unsigned int i_index = 43;</span> 45 <span class="comment">// the following corrupts memory.</span> 46 <span class="comment">// This may or may not be detected at run time.</span> 47 <span class="comment">// i_array[i_index] = 84; // comment this out so it can be tested!</span> 48 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"error NOT detected!"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 49 50 <span class="comment">// solution: replace unsigned array index with safe_unsigned_range</span> 51 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"Using safe numerics"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 52 <span class="keyword">try</span><span class="special">{</span> 53 <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">safe_numerics</span><span class="special">;</span> 54 <span class="keyword">using</span> <span class="identifier">i_index_t</span> <span class="special">=</span> <span class="identifier">safe_unsigned_range</span><span class="special"><</span><span class="number">0</span><span class="special">,</span> <span class="identifier">i_array</span><span class="special">.</span><span class="identifier">size</span><span class="special">(</span><span class="special">)</span> <span class="special">-</span> <span class="number">1</span><span class="special">></span><span class="special">;</span> 55 <span class="identifier">i_index_t</span> <span class="identifier">i_index</span><span class="special">;</span> 56 <span class="identifier">i_index</span> <span class="special">=</span> <span class="number">36</span><span class="special">;</span> <span class="comment">// this works fine</span> 57 <span class="identifier">i_array</span><span class="special">[</span><span class="identifier">i_index</span><span class="special">]</span> <span class="special">=</span> <span class="number">84</span><span class="special">;</span> 58 <span class="identifier">i_index</span> <span class="special">=</span> <span class="number">43</span><span class="special">;</span> <span class="comment">// throw exception here!</span> 59 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"error NOT detected!"</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> <span class="comment">// so we never arrive here</span> 60 <span class="special">}</span> 61 <span class="keyword">catch</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">exception</span> <span class="special">&</span> <span class="identifier">e</span><span class="special">)</span><span class="special">{</span> 62 <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="string">"error detected:"</span> <span class="special"><<</span> <span class="identifier">e</span><span class="special">.</span><span class="identifier">what</span><span class="special">(</span><span class="special">)</span> <span class="special"><<</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span> 63 <span class="special">}</span> 64 <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span> 65<span class="special">}</span> 66</pre> 67<pre class="screen">example 5: array index values can exceed array bounds 68Not using safe numerics 69error NOT detected! 70Using safe numerics 71error detected:Value out of range for this safe type: domain error 72</pre> 73<p>Collections like standard arrays and vectors do array index checking 74 in some function calls and not in others so this may not be the best 75 example. However it does illustrate the usage of 76 <code class="computeroutput">safe_range<T></code> for assigning legal ranges to variables. 77 This will guarantee that under no circumstances will the variable contain 78 a value outside of the specified range.</p> 79</div> 80<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 81<td align="left"></td> 82<td align="right"><div class="copyright-footer">Copyright © 2012-2018 Robert Ramey<p><a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">Subject to Boost 83 Software License</a></p> 84</div></td> 85</tr></table> 86<hr> 87<div class="spirit-nav"> 88<a accesskey="p" href="5.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorial.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="7.html"><img src="../images/next.png" alt="Next"></a> 89</div> 90</body> 91</html> 92