JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
This section uses a template header example to illustrate the process of creating and incorporating a new source file patch.
This technique can be applied to any C or C++ source file.
Suppose we have some code that makes use of various Boost functions and types for random number generation.
#include <iostream>
#include <ctime>
#include <boost/random/uniform_int.hpp>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/variate_generator.hpp>
int main(int argc, char *argv[])
{
int buf[256];
boost::mt19937 gen(static_cast<unsigned int>(std::time(0)));
boost::uniform_int<> dist(0, 256);
boost::variate_generator<boost::mt19937&,
boost::uniform_int<> > ugen(gen, dist);
// Potential overrun - random number range is off-by-one
buf[ugen()];
return 0;
}
CodeSonar cannot determine the value that ugen() will return. In particular, it cannot determine that ugen may return 256, and so it does not issue a Buffer Overrun warning. This is a false negative.
We need to create a patch that models Boost's random number generation behavior so that CodeSonar can understand it. The most straightforward approach is to model the generated random numbers as bounded adversarial values, using cs_untrusted_value().
The patch is created as follows. (In this example, Boost is located at /my_project/boost_1_48_0/).
result_type rv = (result_type)cs_untrusted_value();
csonar_assert(rv >= _min && rv <= _max);
return rv;
SOURCE_PATCH_DIRECTORIES += /my_project/header_patches/
To report problems with this documentation, please visit https://support.codesecure.com/.