mirror of
https://github.com/boostorg/safe_numerics.git
synced 2026-02-12 00:13:20 +00:00
97 lines
4.3 KiB
HTML
97 lines
4.3 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Checking of initialization values can be easily overlooked</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="5.html" title="Array index value can exceed array limits">
|
|
<link rel="next" href="../notes.html" title="Notes">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="../pre-boost.jpg"></td>
|
|
<td align="center"><a href="../../../index.html">Home</a></td>
|
|
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
|
<td align="center"><a href="../../../more/index.htm">More</a></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<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="../notes.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.6"></a>Checking of initialization values can be easily overlooked</h3></div></div></div>
|
|
<p>It's way too easy to overlook the checking of parameters received
|
|
from outside the current program.</p>
|
|
<pre class="programlisting">#include <cassert>
|
|
#include <stdexcept>
|
|
#include <sstream>
|
|
#include <iostream>
|
|
|
|
#include "../include/safe_integer.hpp"
|
|
|
|
void detected_msg(bool detected){
|
|
std::cout << (detected ? "error detected!" : "error NOT detected! ") << std::endl;
|
|
}
|
|
|
|
int main(int argc, const char * argv[]){
|
|
// problem: checking of externally produced value can be overlooked
|
|
std::cout << "example 6: ";
|
|
std::cout << "checking of externally produced value can be overlooked" << std::endl;
|
|
std::cout << "Not using safe numerics" << std::endl;
|
|
|
|
std::istringstream is("12317289372189 1231287389217389217893");
|
|
|
|
int x, y, z;
|
|
is >> x >> y; // get integer values from the user
|
|
z = x + y;
|
|
std::cout << z << std::endl; // display sum of the values
|
|
detected_msg(false);
|
|
|
|
// solution: asign externally retrieved values to safe equivalents
|
|
std::cout << "Using safe numerics" << std::endl;
|
|
{
|
|
using namespace boost::numeric;
|
|
safe<int> x, y, z;
|
|
is.seekg(0);
|
|
try{
|
|
is >> x >> y; // get integer values from the user
|
|
detected_msg(false);
|
|
}
|
|
catch(std::range_error & e){
|
|
std::cout << e.what() << std::endl;
|
|
detected_msg(true);
|
|
}
|
|
try{
|
|
z = x + y;
|
|
detected_msg(false);
|
|
}
|
|
catch(std::exception e){
|
|
std::cout << z << std::endl; // display sum of the values
|
|
detected_msg(true);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
</pre>
|
|
<p>Without
|
|
safe integer, one will have to insert new code every time an integer
|
|
variable is retrieved. This is a tedious and error prone procedure.</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="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="../notes.html"><img src="../images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|