assertion_failed.cc
#include <stdexcept>
namespace boost
{
void assertion_failed(char const * expr, char const * function, char const * file, long line){
(void)function;
(void)file;
(void)line;
throw std::runtime_error(expr);
}
void assertion_failed_msg(char const * expr, char const * msg, char const * function, char const * file, long line){
(void)msg;
(void)function;
(void)file;
(void)line;
throw std::runtime_error(expr);
}
}
prog.cc
#define BOOST_ENABLE_ASSERT_HANDLER
//#define BOOST_ASSERT_HANDLER_IS_NORETURN
#include <boost/config.hpp> // BOOST_NORETURN
namespace boost
{
#if defined(BOOST_ASSERT_HANDLER_IS_NORETURN)
BOOST_NORETURN
#endif
void assertion_failed(char const * expr, char const * function, char const * file, long line); // user-defined
#if defined(BOOST_ASSERT_HANDLER_IS_NORETURN)
BOOST_NORETURN
#endif
void assertion_failed_msg(char const * expr, char const * msg, char const * function, char const * file, long line); // user-defined
}
#include <boost/array.hpp>
#include <cstdio>
int main(int argc, char **argv){
(void)argv;
boost::array<int, 3> a;
if(argc==1)printf("%d\n", a[3]);
}
With -Wall
, this program1 raises Warray-bounds because:
- BOOST_ENABLE_ASSERT_HANDLER is defined
- assertion_failed is defined in different .cc and the compiler cannot notice this function throws
- array.hpp impl is
return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];
- assertion_failed declaration is not BOOST_NORETURN and short-circuit does not work
This can be a problem when -Werror is set.
Hence I introduced a new flag BOOST_ASSERT_HANDLER_IS_NORETURN
( https://github.com/boostorg/assert/pull/40 ) to make sure the short-circuit work.
You can confirm https://wandbox.org/permlink/iOhTvSL4wutMEROa does not raise Warray-bounds any longer.
-
I understand this is extreme example, but can be reproduced by
boost::heap::fibonacci_heap<int> hp; hp.push(1); hp.pop();
as well, which is normal example. https://wandbox.org/permlink/1UtnWOMXIVSL3I62 ↩