Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit db33053

Browse files
committed
C++: Add test for macro exclusions
1 parent 8d79634 commit db33053

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
| test.cpp:6:12:6:33 | ... < ... | Range check relying on pointer overflow. |
22
| test.cpp:33:9:33:21 | ... < ... | Range check relying on pointer overflow. |
3+
| test.cpp:49:36:49:48 | ... < ... | Range check relying on pointer overflow. |
4+
| test.cpp:51:14:51:26 | ... < ... | Range check relying on pointer overflow. |

cpp/ql/test/query-tests/Likely Bugs/Memory Management/PointerOverflow/test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,19 @@ bool not_in_range_good(Q *ptr, Q *ptr_end, size_t a) {
4040
bool in_range(Q *ptr, Q *ptr_end, size_t a) {
4141
return a < ptr_end - ptr; // GOOD
4242
}
43+
44+
extern "C" void abort(void);
45+
46+
#define MYASSERT(cond) if (cond) abort()
47+
48+
void assert_not_in_range_bad(Q *ptr, Q *ptr_end, size_t a) {
49+
MYASSERT(ptr + a >= ptr_end || ptr + a < ptr); // BAD
50+
MYASSERT(ptr + a >= ptr_end); // GOOD (for the purpose of this test)
51+
MYASSERT(ptr + a < ptr); // BAD
52+
}
53+
54+
#define IS_LESS_THAN(lhs, rhs) ((lhs) < (rhs))
55+
56+
bool not_in_range_macro(Q *ptr, Q *ptr_end, size_t a) {
57+
return ptr + a >= ptr_end || IS_LESS_THAN(ptr + a, ptr); // GOOD (meant to be excluded)
58+
}

0 commit comments

Comments
 (0)