mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-09 23:32:37 +00:00
a) made trap_exception work b) updated manual and examples to show how to use library to eliminate runtime penalty c) added in safe_literal d) made corrections of various types
87 lines
4.3 KiB
HTML
87 lines
4.3 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Array Index Value Can Exceed Array Limits</title>
|
|
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
|
|
<link rel="home" href="../index.html" title="Safe Numerics">
|
|
<link rel="up" href="../tutorial.html" title="Tutorial and Motivating Examples">
|
|
<link rel="prev" href="10.html" title="Mixing Data Types Can Create Subtle Errors">
|
|
<link rel="next" href="6.html" title="Checking of Input Values Can Be Easily Overlooked">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img href="index.html" height="164px" src="pre-boost.jpg" alt="Library Documentation Index"></td>
|
|
<td><h2>Safe Numerics</h2></td>
|
|
</tr></table>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="10.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="6.html"><img src="../images/next.png" alt="Next"></a>
|
|
</div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h3 class="title">
|
|
<a name="safe_numerics.tutorial.5"></a>Array Index Value Can Exceed Array Limits</h3></div></div></div>
|
|
<p>Using an intrinsic C++ array, it's very easy to exceed array limits.
|
|
This can fail to be detected when it occurs and create bugs which are hard
|
|
to find. There are several ways to address this, but one of the simplest
|
|
would be to use safe_unsigned_range;</p>
|
|
<pre class="programlisting">#include <stdexcept>
|
|
#include <iostream>
|
|
|
|
#include "../include/safe_range.hpp"
|
|
|
|
void detected_msg(bool detected){
|
|
std::cout << (detected ? "error detected!" : "error NOT detected! ") << std::endl;
|
|
}
|
|
|
|
int main(int argc, const char * argv[]){
|
|
// problem: array index values can exceed array bounds
|
|
std::cout << "example 5: ";
|
|
std::cout << "array index values can exceed array bounds" << std::endl;
|
|
std::cout << "Not using safe numerics" << std::endl;
|
|
std::array<int, 37> i_array;
|
|
|
|
// unsigned int i_index = 43;
|
|
// the following corrupts memory.
|
|
// This may or may not be detected at run time.
|
|
// i_array[i_index] = 84; // comment this out so it can be tested!
|
|
std::cout << "error NOT detected!" << std::endl;
|
|
|
|
// solution: replace unsigned array index with safe_unsigned_range
|
|
std::cout << "Using safe numerics" << std::endl;
|
|
try{
|
|
using namespace boost::numeric;
|
|
using i_index_t = safe_unsigned_range<0, i_array.size() - 1>;
|
|
i_index_t i_index;
|
|
i_index = 36; // this works fine
|
|
i_array[i_index] = 84;
|
|
i_index = 43; // throw exception here!
|
|
std::cout << "error NOT detected!" << std::endl; // so we never arrive here
|
|
}
|
|
catch(std::exception & e){
|
|
std::cout << e.what() << std::endl;
|
|
std::cout << "error detected!" << std::endl;
|
|
}
|
|
return 0;
|
|
}
|
|
</pre>
|
|
<p>Collections
|
|
like standard arrays, vectors do array index checking in some function
|
|
calls and not in others so this may not be the best example. However it
|
|
does illustrate the usage of <code class="computeroutput">safe_range<T></code> for
|
|
assigning legal range to variables. This will guarantee that under no
|
|
circumstances will the variable contain a value outside of the specified
|
|
range.</p>
|
|
</div>
|
|
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
|
<td align="left"></td>
|
|
<td align="right"><div class="copyright-footer">Copyright © 2012 Robert Ramey<p><a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">Subject to Boost
|
|
Software License</a></p>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="10.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="6.html"><img src="../images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|