Skip to content

[analyzer] Fix crash for non-pointers annotated as nonnull #1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clang/lib/StaticAnalyzer/Checkers/NonNullParamChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,18 @@ void NonNullParamChecker::checkBeginFunction(CheckerContext &Context) const {
if (!ParameterNonNullMarks.test(Parameter->getFunctionScopeIndex()))
continue;

// 2. Check that parameter is a pointer.
// Nonnull attribute can be applied to non-pointers (by default
// __attribute__(nonnull) implies "all parameters").
if (!Parameter->getType()->isPointerType())
continue;

Loc ParameterLoc = State->getLValue(Parameter, LocContext);
// We never consider top-level function parameters undefined.
auto StoredVal =
State->getSVal(ParameterLoc).castAs<DefinedOrUnknownSVal>();

// 2. Assume that it is indeed non-null
// 3. Assume that it is indeed non-null
if (ProgramStateRef NewState = State->assume(StoredVal, true)) {
State = NewState;
}
Expand Down
13 changes: 13 additions & 0 deletions clang/test/Analysis/UserNullabilityAnnotations.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_analyze_cc1 -verify -Wno-objc-root-class %s \
// RUN: -Wno-tautological-pointer-compare \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=nullability \
// RUN: -analyzer-checker=debug.ExprInspection
Expand Down Expand Up @@ -34,3 +35,15 @@ void f1(NestedNonnullMember *Root) {
clang_analyzer_eval(Grandson->Value != 0); // expected-warning{{TRUE}}
clang_analyzer_eval(foo()->Child->Value != 0); // expected-warning{{TRUE}}
}

// Check that we correctly process situations when non-pointer parameters
// get nonnul attributes.
// Original problem: rdar://problem/63150074
typedef struct {
long a;
} B;
__attribute__((nonnull)) void c(B x, int *y);

void c(B x, int *y) {
clang_analyzer_eval(y != 0); // expected-warning{{TRUE}}
}