fredrik.eriksson

Coffee and a keyboard

Self validating fixed size array

The C++ Standard Technical Report 1 introduced a class template array with fixed size, that is safer than and no less efficient than a C style array.

Even if the array is much safer it is still possible to do buffer overruns as shown in this (stupid) example bellow:

#include <tr1/array>

int main()
{
    char buf[256];
    std::tr1::array<char, 128> arr;

    std::memcpy( arr.data(), buf, 129 );
}

So I implemented a self validating array with the help of a dogtag that automatically knows if the array have been compromised.
This is the same way the kernel knows if you have overstepped you stack space.

#include <thebc/array.hpp>

int main()
{
    char buf[256];
    thebc::array<char, 128> arr;

    std::memcpy( arr.data(), buf, 128 );
    arr.validate(); // no problems

    std::memcpy( arr.data(), buf, 129 );
    arr.validate(); // assertion failure
}

And if you compile with NDEBUG defined the validation is removed so there is no performance hits.

Spec++ 0.1.0

I’m closing in on the release of Spec++ 0.2.0. (a Behavior Driven Development C++ framework)

The upcoming 0.1.0 is a another pre release that contains the new expectation API I have developed. The layout of the expectation is a little different from the earlier API.

// old string compare
value( "test" ).should.equal( "test" );
value( "test" ).should.not_equal( "string" );

// new string compare
actual( "test" ).string.should.equal( "test" );
actual( "test" ).string.should.not.equal( "string" );

// use the predicate group
actual( true ).predicate.should.be_true();
actual( false ).predicate.should.not.be_true();

I will probably fix so it is possible to write:

actual( true ).should.be_true();

where should uses template specialization to choose the right group from the type passed to the actual function.

The new design also makes it easy to add new groups to the API. I will post a detailed description on how this is done later.