0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

About a new Boost.Assert flag "BOOST_ASSERT_HANDLER_IS_NORETURN" (available from Boost 1.88)

Posted at
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:

  1. BOOST_ENABLE_ASSERT_HANDLER is defined
  2. assertion_failed is defined in different .cc and the compiler cannot notice this function throws
  3. array.hpp impl is return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];
  4. 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.

  1. 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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?