Skip to content

Commit 49885b1

Browse files
author
Csaba Dabis
committed
[analyzer] ExprEngine: Escape pointers in bitwise operations
Summary: After evaluation it would be an Unknown value and tracking would be lost. Reviewers: NoQ, xazax.hun, ravikandhadai, baloghadamsoftware, Szelethus Reviewed By: NoQ Subscribers: szepet, rnkovacs, a.sidorin, mikhail.ramalho, donat.nagy, dkrupp, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D63720 llvm-svn: 364259
1 parent 2cc3b38 commit 49885b1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

clang/lib/StaticAnalyzer/Core/ExprEngineC.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
100100
SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType());
101101
if (!Result.isUnknown()) {
102102
state = state->BindExpr(B, LCtx, Result);
103+
} else {
104+
// If we cannot evaluate the operation escape the operands.
105+
state = escapeValue(state, LeftV, PSK_EscapeOther);
106+
state = escapeValue(state, RightV, PSK_EscapeOther);
103107
}
104108

105109
Bldr.generateNode(B, *it, state);

clang/test/Analysis/symbol-escape.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %clang_analyze_cc1 \
2+
// RUN: -analyzer-checker=core,cplusplus.NewDeleteLeaks \
3+
// RUN: -verify %s
4+
5+
// expected-no-diagnostics: Whenever we cannot evaluate an operation we escape
6+
// the operands. After the evaluation it would be an
7+
// Unknown value and the tracking would be lost.
8+
9+
typedef unsigned __INTPTR_TYPE__ uintptr_t;
10+
11+
class C {};
12+
13+
C *simple_escape_in_bitwise_op(C *Foo) {
14+
C *Bar = new C();
15+
Bar = reinterpret_cast<C *>(reinterpret_cast<uintptr_t>(Bar) & 0x1);
16+
(void)Bar;
17+
// no-warning: "Potential leak of memory pointed to by 'Bar'" was here.
18+
19+
return Bar;
20+
}
21+
22+
C **indirect_escape_in_bitwise_op() {
23+
C *Qux = new C();
24+
C **Baz = &Qux;
25+
Baz = reinterpret_cast<C **>(reinterpret_cast<uintptr_t>(Baz) | 0x1);
26+
Baz = reinterpret_cast<C **>(reinterpret_cast<uintptr_t>(Baz) &
27+
~static_cast<uintptr_t>(0x1));
28+
// no-warning: "Potential leak of memory pointed to by 'Qux'" was here.
29+
30+
delete *Baz;
31+
return Baz;
32+
}
33+

0 commit comments

Comments
 (0)