Catch2 icon indicating copy to clipboard operation
Catch2 copied to clipboard

REQUIRE(c_string != nullptr) - buffer overrun

Open mintyc opened this issue 11 years ago • 5 comments

Using clangs address sanitiser on this simple test case TEST_CASE("Dummy") { const char *data = new char[10]; REQUIRE(data != nullptr); delete [] data; }

Complains that catch in evaluating the expression seems to be using strlen() and going outside the buffer. It seems to be trying to do a string comparison with the const char * and assuming a null terminated string rather than doing a pointer check. [Workaround: Using a reinterpret_cast<const void *> stops this occurring]

==16776==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000df3a at pc 0x0000004ae301 bp 0x7fff20c93df0 sp 0x7fff20c935a0
READ of size 11 at 0x60200000df3a thread T0
    #0 0x4ae300 in strlen /home/pjbaxter/clang/llvm/projects/compiler-rt/lib/asan/asan_interceptors.cc:609
    #1 0x7f5e057e9904 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (/lib64/libstdc++.so.6+0xbf904)
    #2 0x556686 in Catch::toString(char const*) 
    #3 0x61a675 in Catch::ResultBuilder& Catch::ExpressionLhs<char const* const&>::captureExpression<(Catch::Internal::Operator)1, decltype(nullptr)>(decltype(nullptr) const&)
    #4 0x61a30c in Catch::ResultBuilder& Catch::ExpressionLhs<char const* const&>::operator!=<decltype(nullptr)>(decltype(nullptr) const&) 
    #5 0x619e82 in ____C_A_T_C_H____T_E_S_T____37() 

mintyc avatar Oct 27 '15 09:10 mintyc

Could you surround the backtrace with triple backticks? That way, you won't end up referencing every issue up to 5! :)

refi64 avatar Oct 27 '15 14:10 refi64

The comparison itself is a pointer check. However Catch will try to convert both sides to strings for reporting purposes. The overload of the function that does this for char* assumes it is pointing to a string - and that's what would be triggering the sanitizer error you're seeing (in fact you can see Catch::toString() on the stack in your error message).

Funnily enough the treatment of char*s as strings came up somewhere else just recently - although for different reasons.

It does raise the question of how to deal with invalid strings. I'm not sure there is any easy way that doesn't make the vast majority of cases (where the char*s do point to valid strings) much more verbose and inconvenient.

As you have noted you can avoid the issue by casting the char* to something else (probably void*) to indicate that you are only interested in the pointer value. For now this would be my recommendation. If you come up with any other ideas do let me know.

philsquared avatar Oct 27 '15 19:10 philsquared

What about an alternative form of CHECK or REQUIRE just for pointer comparisons? FWIW, you might even be able to work in std::less for valid ordering comparisons for pointers.

michaelbprice avatar Oct 30 '15 20:10 michaelbprice

I think the times you actually want to do a pointer check on char* pointers is rare enough that just doing void* casts is fine. I don't think it justifies extending the framework.

philsquared avatar Nov 10 '15 08:11 philsquared

For now this is working as intended.

horenmar avatar Jan 06 '17 20:01 horenmar