Skip to content

Commit 855f0ce

Browse files
[analyzer] Fix crash for non-pointers annotated as nonnull
Summary: Nonnull attribute can be applied to non-pointers. This caused assertion failures in NonNullParamChecker when we tried to *assume* such parameters to be non-zero. rdar://problem/63150074 Differential Revision: https://reviews.llvm.org/D79843
1 parent e25a260 commit 855f0ce

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

clang/lib/StaticAnalyzer/Checkers/NonNullParamChecker.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,18 @@ void NonNullParamChecker::checkBeginFunction(CheckerContext &Context) const {
254254
if (!ParameterNonNullMarks.test(Parameter->getFunctionScopeIndex()))
255255
continue;
256256

257+
// 2. Check that parameter is a pointer.
258+
// Nonnull attribute can be applied to non-pointers (by default
259+
// __attribute__(nonnull) implies "all parameters").
260+
if (!Parameter->getType()->isPointerType())
261+
continue;
262+
257263
Loc ParameterLoc = State->getLValue(Parameter, LocContext);
258264
// We never consider top-level function parameters undefined.
259265
auto StoredVal =
260266
State->getSVal(ParameterLoc).castAs<DefinedOrUnknownSVal>();
261267

262-
// 2. Assume that it is indeed non-null
268+
// 3. Assume that it is indeed non-null
263269
if (ProgramStateRef NewState = State->assume(StoredVal, true)) {
264270
State = NewState;
265271
}

clang/test/Analysis/UserNullabilityAnnotations.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %clang_analyze_cc1 -verify -Wno-objc-root-class %s \
2+
// RUN: -Wno-tautological-pointer-compare \
23
// RUN: -analyzer-checker=core \
34
// RUN: -analyzer-checker=nullability \
45
// RUN: -analyzer-checker=debug.ExprInspection
@@ -34,3 +35,15 @@ void f1(NestedNonnullMember *Root) {
3435
clang_analyzer_eval(Grandson->Value != 0); // expected-warning{{TRUE}}
3536
clang_analyzer_eval(foo()->Child->Value != 0); // expected-warning{{TRUE}}
3637
}
38+
39+
// Check that we correctly process situations when non-pointer parameters
40+
// get nonnul attributes.
41+
// Original problem: rdar://problem/63150074
42+
typedef struct {
43+
long a;
44+
} B;
45+
__attribute__((nonnull)) void c(B x, int *y);
46+
47+
void c(B x, int *y) {
48+
clang_analyzer_eval(y != 0); // expected-warning{{TRUE}}
49+
}

0 commit comments

Comments
 (0)