Skip to content

Commit

Permalink
Merge pull request #11 from openledger/sanitize
Browse files Browse the repository at this point in the history
sanitizer issues
  • Loading branch information
pmconrad authored Mar 13, 2018
2 parents 67e6036 + fca5058 commit 3286935
Show file tree
Hide file tree
Showing 24 changed files with 93 additions and 65 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 )

Expand Down
2 changes: 2 additions & 0 deletions include/fc/io/json_relaxed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ namespace fc { namespace json_relaxed
}

} FC_RETHROW_EXCEPTIONS( warn, "while parsing string" );

return {};
}

struct CharValueTable
Expand Down
7 changes: 4 additions & 3 deletions include/fc/static_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -326,15 +327,15 @@ class static_variant {
}

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();
_tag = w;
impl::storage_ops<0, Types...>::con(_tag, storage);
}

int which() const {return _tag;}
tag_type which() const {return _tag;}
};

template<typename Result>
Expand Down
1 change: 1 addition & 0 deletions include/fc/thread/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();


/**
Expand Down
8 changes: 8 additions & 0 deletions src/asio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/compress/miniz.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 6 additions & 4 deletions src/crypto/aes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct aes_encoder::impl
aes_encoder::aes_encoder()
{
static int init = init_openssl();
(void)init;
}

aes_encoder::~aes_encoder()
Expand Down Expand Up @@ -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
Expand All @@ -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 )
{
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/crypto/base36.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion src/crypto/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/elliptic_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion src/crypto/elliptic_secp256k1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/rand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/rpc/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/thread/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 13 additions & 7 deletions src/thread/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand Down Expand Up @@ -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...
Expand Down
1 change: 0 additions & 1 deletion src/utf8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}

Expand Down
2 changes: 1 addition & 1 deletion tests/bloom_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
13 changes: 5 additions & 8 deletions tests/crypto/base_n_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

}
Expand Down
Loading

0 comments on commit 3286935

Please sign in to comment.