Merge pull request #11 from openledger/sanitize

sanitizer issues
This commit is contained in:
Peter Conrad 2018-03-13 21:45:53 +01:00 committed by GitHub
commit 32869351c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 93 additions and 65 deletions

View file

@ -4,6 +4,8 @@
PROJECT( fc )
CMAKE_MINIMUM_REQUIRED( VERSION 2.8.12 )
add_compile_options(-std=c++14)
MESSAGE(STATUS "Configuring project fc located in: ${CMAKE_CURRENT_SOURCE_DIR}")
SET( CMAKE_AUTOMOC OFF )

View file

@ -238,6 +238,8 @@ namespace fc { namespace json_relaxed
}
} FC_RETHROW_EXCEPTIONS( warn, "while parsing string" );
return {};
}
struct CharValueTable

View file

@ -181,7 +181,8 @@ class static_variant {
static_assert(impl::type_info<Types...>::no_reference_types, "Reference types are not permitted in static_variant.");
static_assert(impl::type_info<Types...>::no_duplicates, "static_variant type arguments contain duplicate types.");
int _tag;
using tag_type = int64_t;
tag_type _tag;
char storage[impl::type_info<Types...>::size];
template<typename X>
@ -326,7 +327,7 @@ public:
}
static int count() { return impl::type_info<Types...>::count; }
void set_which( int w ) {
void set_which( tag_type w ) {
FC_ASSERT( w >= 0 );
FC_ASSERT( w < count() );
this->~static_variant();
@ -334,7 +335,7 @@ public:
impl::storage_ops<0, Types...>::con(_tag, storage);
}
int which() const {return _tag;}
tag_type which() const {return _tag;}
};
template<typename Result>

View file

@ -32,6 +32,7 @@ namespace fc {
an existing "unknown" boost thread). In such cases, thread_d doesn't have access boost::thread object.
*/
static thread& current();
static void cleanup();
/**

View file

@ -3,6 +3,7 @@
#include <boost/thread.hpp>
#include <fc/log/logger.hpp>
#include <fc/exception/exception.hpp>
#include <boost/scope_exit.hpp>
namespace fc {
namespace asio {
@ -104,6 +105,13 @@ namespace fc {
asio_threads.push_back( new boost::thread( [=]()
{
fc::thread::current().set_name("asio");
BOOST_SCOPE_EXIT(void)
{
fc::thread::cleanup();
}
BOOST_SCOPE_EXIT_END
while (!io->stopped())
{
try

View file

@ -1497,7 +1497,7 @@ tinfl_status tinfl_decompress(tinfl_decompressor *r, const mz_uint8 *pIn_buf_nex
{
mz_uint8 *p = r->m_tables[0].m_code_size; mz_uint i;
r->m_table_sizes[0] = 288; r->m_table_sizes[1] = 32; TINFL_MEMSET(r->m_tables[1].m_code_size, 5, 32);
for ( i = 0; i <= 143; ++i) *p++ = 8; for ( ; i <= 255; ++i) *p++ = 9; for ( ; i <= 279; ++i) *p++ = 7; for ( ; i <= 287; ++i) *p++ = 8;
for (i = 0; i <= 143; ++i) {*p++ = 8;} for (; i <= 255; ++i) {*p++ = 9;} for (; i <= 279; ++i) {*p++ = 7;} for (; i <= 287; ++i) {*p++ = 8;}
}
else
{
@ -2281,7 +2281,7 @@ static MZ_FORCEINLINE void tdefl_find_match(tdefl_compressor *d, mz_uint lookahe
if (TDEFL_READ_UNALIGNED_WORD(&d->m_dict[probe_pos + match_len - 1]) == c01) break;
TDEFL_PROBE; TDEFL_PROBE; TDEFL_PROBE;
}
if (!dist) break; q = (const mz_uint16*)(d->m_dict + probe_pos); if (TDEFL_READ_UNALIGNED_WORD(q) != s01) continue; p = s; probe_len = 32;
if (!dist) {break;} q = (const mz_uint16*)(d->m_dict + probe_pos); if (TDEFL_READ_UNALIGNED_WORD(q) != s01) {continue;} p = s; probe_len = 32;
do { } while ( (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) &&
(TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (TDEFL_READ_UNALIGNED_WORD(++p) == TDEFL_READ_UNALIGNED_WORD(++q)) && (--probe_len > 0) );
if (!probe_len)
@ -2848,7 +2848,7 @@ void *tdefl_write_image_to_png_file_in_memory(const void *pImage, int w, int h,
#include <stdio.h>
#include <sys/stat.h>
#if defined(_MSC_VER)
#if defined(_MSC_VER)
static FILE *mz_fopen(const char *pFilename, const char *pMode)
{
FILE* pFile = NULL;

View file

@ -30,6 +30,7 @@ struct aes_encoder::impl
aes_encoder::aes_encoder()
{
static int init = init_openssl();
(void)init;
}
aes_encoder::~aes_encoder()
@ -70,7 +71,7 @@ uint32_t aes_encoder::encode( const char* plaintxt, uint32_t plaintext_len, char
FC_THROW_EXCEPTION( aes_exception, "error during aes 256 cbc encryption update",
("s", ERR_error_string( ERR_get_error(), nullptr) ) );
}
FC_ASSERT( ciphertext_len == plaintext_len, "", ("ciphertext_len",ciphertext_len)("plaintext_len",plaintext_len) );
FC_ASSERT( (uint32_t) ciphertext_len == plaintext_len, "", ("ciphertext_len",ciphertext_len)("plaintext_len",plaintext_len) );
return ciphertext_len;
}
#if 0
@ -96,9 +97,10 @@ struct aes_decoder::impl
};
aes_decoder::aes_decoder()
{
{
static int init = init_openssl();
}
(void)init;
}
void aes_decoder::init( const fc::sha256& key, const fc::uint128& init_value )
{
@ -137,7 +139,7 @@ uint32_t aes_decoder::decode( const char* ciphertxt, uint32_t ciphertxt_len, cha
FC_THROW_EXCEPTION( aes_exception, "error during aes 256 cbc decryption update",
("s", ERR_error_string( ERR_get_error(), nullptr) ) );
}
FC_ASSERT( ciphertxt_len == plaintext_len, "", ("ciphertxt_len",ciphertxt_len)("plaintext_len",plaintext_len) );
FC_ASSERT( ciphertxt_len == (uint32_t)plaintext_len, "", ("ciphertxt_len",ciphertxt_len)("plaintext_len",plaintext_len) );
return plaintext_len;
}
#if 0

View file

@ -76,7 +76,10 @@ namespace fc
while (len > 0 && *first == 0) { first++; len--; }
std::vector<char> result;
result.resize(leading_zeros + len, 0);
memcpy( result.data() + leading_zeros, first, len );
if (len)
{
memcpy( result.data() + leading_zeros, first, len );
}
return result;
}
}

View file

@ -632,7 +632,9 @@ size_t from_base58( const std::string& base58_str, char* out_data, size_t out_da
FC_THROW_EXCEPTION( parse_error_exception, "Unable to decode base58 string ${base58_str}", ("base58_str",base58_str) );
}
FC_ASSERT( out.size() <= out_data_len );
memcpy( out_data, out.data(), out.size() );
if (!out.empty()) {
memcpy( out_data, out.data(), out.size() );
}
return out.size();
}
}

View file

@ -85,8 +85,8 @@ namespace fc { namespace ecc {
ssl_bignum order;
FC_ASSERT( EC_GROUP_get_order( group, order, ctx ) );
private_key_secret bin;
FC_ASSERT( BN_num_bytes( order ) == bin.data_size() );
FC_ASSERT( BN_bn2bin( order, (unsigned char*) bin.data() ) == bin.data_size() );
FC_ASSERT( (size_t) BN_num_bytes( order ) == bin.data_size() );
FC_ASSERT( (size_t) BN_bn2bin( order, (unsigned char*) bin.data() ) == bin.data_size() );
return bin;
}
@ -104,8 +104,8 @@ namespace fc { namespace ecc {
FC_ASSERT( EC_GROUP_get_order( group, order, ctx ) );
BN_rshift1( order, order );
private_key_secret bin;
FC_ASSERT( BN_num_bytes( order ) == bin.data_size() );
FC_ASSERT( BN_bn2bin( order, (unsigned char*) bin.data() ) == bin.data_size() );
FC_ASSERT( (size_t) BN_num_bytes( order ) == bin.data_size() );
FC_ASSERT( (size_t) BN_bn2bin( order, (unsigned char*) bin.data() ) == bin.data_size() );
return bin;
}

View file

@ -30,8 +30,9 @@ namespace fc { namespace ecc {
void _init_lib() {
static const secp256k1_context_t* ctx = _get_context();
static int init_o = init_openssl();
(void)ctx;
static int init_o = init_openssl();
(void)init_o;
}
class public_key_impl

View file

@ -9,6 +9,7 @@ namespace fc {
void rand_bytes(char* buf, int count)
{
static int init = init_openssl();
(void)init;
int result = RAND_bytes((unsigned char*)buf, count);
if (result != 1)
@ -18,6 +19,7 @@ void rand_bytes(char* buf, int count)
void rand_pseudo_bytes(char* buf, int count)
{
static int init = init_openssl();
(void)init;
int result = RAND_pseudo_bytes((unsigned char*)buf, count);
if (result == -1)

View file

@ -123,7 +123,7 @@ static char *my_rl_complete(char *token, int *match)
std::string method_name;
auto& cmd = cli_commands();
int partlen = strlen (token); /* Part of token */
const size_t partlen = strlen (token); /* Part of token */
for (const std::string& it : cmd)
{
@ -170,7 +170,7 @@ static int cli_completion(char *token, char ***array)
}
int total_matches = 0;
int partlen = strlen(token);
const size_t partlen = strlen(token);
for (const std::string& it : cmd)
{

View file

@ -15,9 +15,9 @@ namespace fc {
mutex::~mutex() {
if( m_blist )
{
context* c = m_blist;
fc::thread::current().debug("~mutex");
#if 0
context* c = m_blist;
while( c ) {
// elog( "still blocking on context %p (%s)", m_blist, (m_blist->cur_task ? m_blist->cur_task->get_desc() : "no current task") );
c = c->next_blocked_mutex;

View file

@ -113,8 +113,10 @@ namespace fc {
if( my )
{
// wlog( "calling quit() on ${n}",("n",my->name) );
quit(); // deletes `my`
quit();
}
delete my;
}
thread& thread::current() {
@ -123,6 +125,11 @@ namespace fc {
return *current_thread();
}
void thread::cleanup() {
delete current_thread();
current_thread() = nullptr;
}
const string& thread::name()const
{
return my->name;
@ -152,19 +159,18 @@ namespace fc {
{
//if quitting from a different thread, start quit task on thread.
//If we have and know our attached boost thread, wait for it to finish, then return.
if( &current() != this )
if( !is_current() )
{
auto t = my->boost_thread;
async( [=](){quit();}, "thread::quit" );//.wait();
if( my->boost_thread )
if( t )
{
//wlog("destroying boost thread ${tid}",("tid",(uintptr_t)my->boost_thread->native_handle()));
my->boost_thread->join();
delete my;
my = nullptr;
t->join();
}
return;
}
my->done = true;
// wlog( "${s}", ("s",name()) );
// We are quiting from our own thread...

View file

@ -13,7 +13,6 @@ namespace fc {
bool is_utf8( const std::string& str )
{
auto itr = utf8::find_invalid(str.begin(), str.end());
return utf8::is_valid( str.begin(), str.end() );
}

View file

@ -48,7 +48,7 @@ BOOST_AUTO_TEST_CASE(bloom_test_1)
std::string line;
std::ifstream in("README.md");
std::ofstream words("words.txt");
while( !in.eof() && count < 100000 )
while( in.good() && count < 100000 )
{
std::getline(in, line);
// std::cout << "'"<<line<<"'\n";

View file

@ -23,7 +23,7 @@ static void test_16( const std::string& test, const std::string& expected )
BOOST_CHECK_EQUAL( expected, enc2 );
char out[32];
int len = fc::from_hex( enc1, out, 32 );
size_t len = fc::from_hex( enc1, out, 32 );
BOOST_CHECK_EQUAL( test.size(), len );
BOOST_CHECK( !memcmp( test.c_str(), out, len ) );
if (len > 10) {
@ -53,7 +53,7 @@ static void test_36( const std::string& test, const std::string& expected )
std::vector<char> dec = fc::from_base36( enc1 );
BOOST_CHECK_EQUAL( vec.size(), dec.size() );
BOOST_CHECK( !memcmp( vec.data(), dec.data(), vec.size() ) );
BOOST_CHECK( vec == dec );
}
BOOST_AUTO_TEST_CASE(base36_test)
@ -76,17 +76,14 @@ static void test_58( const std::string& test, const std::string& expected )
std::vector<char> dec = fc::from_base58( enc1 );
BOOST_CHECK_EQUAL( vec.size(), dec.size() );
BOOST_CHECK( !memcmp( vec.data(), dec.data(), vec.size() ) );
BOOST_CHECK( vec == dec );
char buffer[64];
size_t len = fc::from_base58( enc1, buffer, 64 );
BOOST_CHECK( len <= 64 );
BOOST_CHECK( !memcmp( vec.data(), buffer, len ) );
BOOST_CHECK( vec.empty() || !memcmp( vec.data(), buffer, len ) );
if ( len > 10 ) {
try {
len = fc::from_base58( enc1, buffer, 10 );
BOOST_CHECK( len <= 10 );
} catch ( fc::exception expected ) {}
BOOST_CHECK_THROW(fc::from_base58( enc1, buffer, 10 ), fc::exception);
}
}

View file

@ -75,9 +75,9 @@ const char key_test_ciphers[][17] = {
const std::string chain_test_key = "0123456789ABCDEFF0E1D2C3B4A59687";
const std::string chain_test_iv = "FEDCBA9876543210";
const std::string chain_test_plain = "7654321 Now is the time for \0\0\0\0";
const std::string chain_test_cbc = "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC";
const std::string chain_test_cfb = "E73214A2822139CAF26ECF6D2EB9E76E3DA3DE04D1517200519D57A6C3";
const char* chain_test_plain = "7654321 Now is the time for \0\0\0\0";
BOOST_AUTO_TEST_SUITE(fc_crypto)
@ -85,9 +85,9 @@ BOOST_AUTO_TEST_CASE(blowfish_ecb_test)
{
for ( int i = 0; i < 34; i++ ) {
unsigned char key[8], plain[8], cipher[8], out[8];
BOOST_CHECK_EQUAL( 8, fc::from_hex( ecb_tests[i].key, (char*) key, sizeof(key) ) );
BOOST_CHECK_EQUAL( 8, fc::from_hex( ecb_tests[i].plain, (char*) plain, sizeof(plain) ) );
BOOST_CHECK_EQUAL( 8, fc::from_hex( ecb_tests[i].cipher, (char*) cipher, sizeof(cipher) ) );
BOOST_CHECK_EQUAL( 8u, fc::from_hex( ecb_tests[i].key, (char*) key, sizeof(key) ) );
BOOST_CHECK_EQUAL( 8u, fc::from_hex( ecb_tests[i].plain, (char*) plain, sizeof(plain) ) );
BOOST_CHECK_EQUAL( 8u, fc::from_hex( ecb_tests[i].cipher, (char*) cipher, sizeof(cipher) ) );
fc::blowfish fish;
fish.start( key, 8 );
@ -105,11 +105,11 @@ BOOST_AUTO_TEST_CASE(blowfish_ecb_test)
BOOST_AUTO_TEST_CASE(blowfish_key_test)
{
unsigned char key[24], plain[8], cipher[8], out[8];
BOOST_CHECK_EQUAL( 24, fc::from_hex( key_test_key.c_str(), (char*) key, sizeof(key) ) );
BOOST_CHECK_EQUAL( 8, fc::from_hex( key_test_plain.c_str(), (char*) plain, sizeof(plain) ) );
BOOST_CHECK_EQUAL( 24u, fc::from_hex( key_test_key.c_str(), (char*) key, sizeof(key) ) );
BOOST_CHECK_EQUAL( 8u, fc::from_hex( key_test_plain.c_str(), (char*) plain, sizeof(plain) ) );
for ( unsigned int i = 0; i < sizeof(key); i++ ) {
BOOST_CHECK_EQUAL( 8, fc::from_hex( key_test_ciphers[i], (char*) cipher, sizeof(cipher) ) );
BOOST_CHECK_EQUAL( 8u, fc::from_hex( key_test_ciphers[i], (char*) cipher, sizeof(cipher) ) );
fc::blowfish fish;
fish.start( key, i + 1 );
fish.encrypt( plain, out, 8, fc::blowfish::ECB );
@ -133,37 +133,37 @@ static unsigned int from_bytes( const unsigned char* p ) {
BOOST_AUTO_TEST_CASE(blowfish_chain_test)
{
unsigned char key[16], iv[8], cipher[32], out[32];
BOOST_CHECK_EQUAL( 16, fc::from_hex( chain_test_key.c_str(), (char*) key, sizeof(key) ) );
BOOST_CHECK_EQUAL( 8, fc::from_hex( chain_test_iv.c_str(), (char*) iv, sizeof(iv) ) );
BOOST_CHECK_EQUAL( 16u, fc::from_hex( chain_test_key.c_str(), (char*) key, sizeof(key) ) );
BOOST_CHECK_EQUAL( 8u, fc::from_hex( chain_test_iv.c_str(), (char*) iv, sizeof(iv) ) );
BOOST_CHECK_EQUAL( 32, fc::from_hex( chain_test_cbc.c_str(), (char*) cipher, sizeof(cipher) ) );
BOOST_CHECK_EQUAL( 32u, fc::from_hex( chain_test_cbc.c_str(), (char*) cipher, sizeof(cipher) ) );
fc::blowfish fish;
fish.start( key, sizeof(key), fc::sblock( from_bytes( iv ), from_bytes( iv + 4 ) ) );
fish.encrypt( (unsigned char*) chain_test_plain.c_str(), out, sizeof(out), fc::blowfish::CBC );
fish.encrypt( (unsigned char*) chain_test_plain, out, sizeof(out), fc::blowfish::CBC );
BOOST_CHECK( !memcmp( cipher, out, sizeof(cipher) ) );
fish.reset_chain();
fish.decrypt( out, sizeof(out), fc::blowfish::CBC );
BOOST_CHECK( !memcmp( chain_test_plain.c_str(), out, 29 ) );
BOOST_CHECK( !memcmp( chain_test_plain, out, 29 ) );
fish.reset_chain();
fish.encrypt( out, sizeof(out), fc::blowfish::CBC );
BOOST_CHECK( !memcmp( cipher, out, sizeof(cipher) ) );
fish.reset_chain();
fish.decrypt( cipher, out, sizeof(cipher), fc::blowfish::CBC );
BOOST_CHECK( !memcmp( chain_test_plain.c_str(), out, 29 ) );
BOOST_CHECK( !memcmp( chain_test_plain, out, 29 ) );
BOOST_CHECK_EQUAL( 29, fc::from_hex( chain_test_cfb.c_str(), (char*) cipher, sizeof(cipher) ) );
BOOST_CHECK_EQUAL( 29u, fc::from_hex( chain_test_cfb.c_str(), (char*) cipher, sizeof(cipher) ) );
fish.reset_chain();
fish.encrypt( (unsigned char*) chain_test_plain.c_str(), out, sizeof(out), fc::blowfish::CFB );
fish.encrypt( (unsigned char*) chain_test_plain, out, sizeof(out), fc::blowfish::CFB );
BOOST_CHECK( !memcmp( cipher, out, 29 ) );
fish.reset_chain(); memset( out + 29, 0, 3 );
fish.decrypt( out, sizeof(out), fc::blowfish::CFB );
BOOST_CHECK( !memcmp( chain_test_plain.c_str(), out, 29 ) );
BOOST_CHECK( !memcmp( chain_test_plain, out, 29 ) );
fish.reset_chain(); memset( out + 29, 0, 3 );
fish.encrypt( out, sizeof(out), fc::blowfish::CFB );
BOOST_CHECK( !memcmp( cipher, out, 29 ) );
fish.reset_chain(); memset( out + 29, 0, 3 );
fish.decrypt( cipher, out, sizeof(cipher), fc::blowfish::CFB );
BOOST_CHECK( !memcmp( chain_test_plain.c_str(), out, 29 ) );
BOOST_CHECK( !memcmp( chain_test_plain, out, 29 ) );
}
BOOST_AUTO_TEST_SUITE_END()

View file

@ -21,7 +21,9 @@ static void check_randomness( const char* buffer, size_t len ) {
double E = 1 + (zc + oc) / 2.0;
double variance = (E - 1) * (E - 2) / (oc + zc - 1);
double sigma = sqrt(variance);
BOOST_CHECK( rc > E - sigma && rc < E + sigma);
BOOST_CHECK_GT(rc, E - sigma);
BOOST_CHECK_LT(rc, E + sigma);
}
BOOST_AUTO_TEST_SUITE(fc_crypto)

View file

@ -75,7 +75,7 @@ void test_big( const std::string& expected ) {
template<typename H>
void test_stream( ) {
H hash( TEST1 );
H hash = H::hash( TEST1 );
std::stringstream stream;
stream << hash;

View file

@ -72,7 +72,7 @@ static fc::hmac<fc::sha224> mac_224;
static fc::hmac<fc::sha256> mac_256;
static fc::hmac<fc::sha512> mac_512;
template<int N,int M>
template<size_t N,size_t M>
static void run_test( const fc::string& key, const fc::string& data, const fc::string& expect_224,
const fc::string& expect_256, const fc::string& expect_512 )
{

View file

@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(websocket_test)
try {
c_conn->send_message( "again" );
BOOST_FAIL("expected assertion failure");
} catch (const fc::assert_exception& e) {
} catch (const fc::exception& e) {
//std::cerr << e.to_string() << "\n";
}
@ -55,14 +55,14 @@ BOOST_AUTO_TEST_CASE(websocket_test)
try {
c_conn->send_message( "again" );
BOOST_FAIL("expected assertion failure");
} catch (const fc::assert_exception& e) {
} catch (const fc::exception& e) {
std::cerr << e.to_string() << "\n";
}
try {
c_conn = client.connect( "ws://localhost:8090" );
BOOST_FAIL("expected assertion failure");
} catch (const fc::assert_exception& e) {
} catch (const fc::exception& e) {
std::cerr << e.to_string() << "\n";
}
}

View file

@ -11,9 +11,9 @@ BOOST_AUTO_TEST_CASE(real128_test)
{
BOOST_CHECK_EQUAL(string(real128()), string("0."));
BOOST_CHECK_EQUAL(string(real128(0)), string("0."));
BOOST_CHECK_EQUAL(real128(8).to_uint64(), 8);
BOOST_CHECK_EQUAL(real128(6789).to_uint64(), 6789);
BOOST_CHECK_EQUAL(real128(10000).to_uint64(), 10000);
BOOST_CHECK_EQUAL(real128(8).to_uint64(), 8u);
BOOST_CHECK_EQUAL(real128(6789).to_uint64(), 6789u);
BOOST_CHECK_EQUAL(real128(10000).to_uint64(), 10000u);
BOOST_CHECK_EQUAL(string(real128(1)), string("1."));
BOOST_CHECK_EQUAL(string(real128(5)), string("5."));
BOOST_CHECK_EQUAL(string(real128(12345)), string("12345."));
@ -41,8 +41,8 @@ BOOST_AUTO_TEST_CASE(real128_test)
BOOST_CHECK_EQUAL( string(pi*1), "3.1415926535" );
BOOST_CHECK_EQUAL( string(pi*0), "0." );
BOOST_CHECK_EQUAL(real128("12345.6789").to_uint64(), 12345);
BOOST_CHECK_EQUAL((real128("12345.6789")*10000).to_uint64(), 123456789);
BOOST_CHECK_EQUAL(real128("12345.6789").to_uint64(), 12345u);
BOOST_CHECK_EQUAL((real128("12345.6789")*10000).to_uint64(), 123456789u);
BOOST_CHECK_EQUAL(string(real128("12345.6789")), string("12345.6789"));
BOOST_CHECK_EQUAL( real128(uint64_t(-1)).to_uint64(), uint64_t(-1) );