Skip to content

Commit 5b1921e

Browse files
Fix BOOST_REQUIRE false positives (#7341)
The **BOOST_REQUIRE** macros would not exit early and in case of a possible null pointer assignment in the condition, it would not get caught in the macro definition. This would show false positives in next usages of the condition and show a `nullPointerOutOfMemory` warning, whereas the **BOOST_REQUIRE** would have thrown an exception. Mimic **BOOST_REQUIRE** behavior in the macro. Added a test for this, also the warning would not show when using `std::malloc`, but does show up when using `malloc`.
1 parent 03aae03 commit 5b1921e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

cfg/boost.cfg

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<define name="BOOST_AUTO_TEST_SUITE_END()" value="}" />
3434
<define name="BOOST_ASSERT(condition)" value="assert(condition)"/>
3535
<define name="BOOST_TEST(condition, ...)" value="static_cast&lt;void&gt;(static_cast&lt;bool&gt;(condition))" />
36-
<define name="BOOST_TEST_REQUIRE(condition, ...)" value="static_cast&lt;void&gt;(static_cast&lt;bool&gt;(condition))" />
36+
<define name="BOOST_TEST_REQUIRE(condition, ...)" value='do { if (!(condition)) { throw false; }} while(0)' />
3737
<define name="BOOST_WARN(condition)" value="static_cast&lt;void&gt;(static_cast&lt;bool&gt;(condition))" />
3838
<define name="BOOST_WARN_MESSAGE(condition, msg)" value="static_cast&lt;void&gt;(static_cast&lt;bool&gt;(condition)); static_cast&lt;void&gt;(msg)" />
3939
<define name="BOOST_WARN_EQUAL(a, b)" value="static_cast&lt;void&gt;((a) == (b))" />
@@ -50,8 +50,8 @@
5050
<define name="BOOST_CHECK_GE(a, b)" value="static_cast&lt;void&gt;((a) &gt;= (b))" />
5151
<define name="BOOST_CHECK_LT(a, b)" value="static_cast&lt;void&gt;((a) &lt; (b))" />
5252
<define name="BOOST_CHECK_LE(a, b)" value="static_cast&lt;void&gt;((a) &lt;= (b))" />
53-
<define name="BOOST_REQUIRE(condition)" value="static_cast&lt;void&gt;(static_cast&lt;bool&gt;(condition))" />
54-
<define name="BOOST_REQUIRE_MESSAGE(condition, msg)" value="static_cast&lt;void&gt;(static_cast&lt;bool&gt;(condition)); static_cast&lt;void&gt;(msg)" />
53+
<define name="BOOST_REQUIRE(condition)" value='do { if (!(condition)) { throw false; }} while(0)' />
54+
<define name="BOOST_REQUIRE_MESSAGE(condition, msg)" value='do { if (!(condition)) { throw false; }} while(0); static_cast&lt;void&gt;(msg)' />
5555
<define name="BOOST_REQUIRE_EQUAL(a, b)" value="static_cast&lt;void&gt;(static_cast&lt;bool&gt;((a) == (b)))" />
5656
<define name="BOOST_REQUIRE_NE(a, b)" value="static_cast&lt;void&gt;((a) != (b))" />
5757
<define name="BOOST_REQUIRE_GT(a, b)" value="static_cast&lt;void&gt;((a) &gt; (b))" />

test/cfg/boost.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <boost/core/scoped_enum.hpp>
2222
#include <boost/foreach.hpp>
2323

24+
#include <cstdlib>
2425
#include <set>
2526
#include <vector>
2627

@@ -162,6 +163,18 @@ void test_BOOST_FOREACH_6(std::vector<int> data)
162163
}
163164
}
164165

166+
void test_require()
167+
{
168+
int *some_int = static_cast<int*>(std::malloc(sizeof(int)));
169+
int *some_other_int = static_cast<int*>(malloc(sizeof(int)));
170+
BOOST_REQUIRE(some_int);
171+
BOOST_TEST_REQUIRE(some_other_int);
172+
*some_int = 42;
173+
*some_other_int = 42;
174+
std::free(some_int);
175+
free(some_other_int);
176+
}
177+
165178
BOOST_AUTO_TEST_SUITE(my_auto_test_suite)
166179

167180
BOOST_AUTO_TEST_CASE(test_message_macros)

0 commit comments

Comments
 (0)