Skip to content

[analyzer] [MallocChecker] Assume functions with ownership_returns return unknown memory #110115

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 1 commit into from
Sep 26, 2024
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
4 changes: 2 additions & 2 deletions clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1811,9 +1811,9 @@ MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallEvent &Call,
if (!Att->args().empty()) {
return MallocMemAux(C, Call,
Call.getArgExpr(Att->args_begin()->getASTIndex()),
UndefinedVal(), State, Family);
UnknownVal(), State, Family);
}
return MallocMemAux(C, Call, UnknownVal(), UndefinedVal(), State, Family);
return MallocMemAux(C, Call, UnknownVal(), UnknownVal(), State, Family);
}

ProgramStateRef MallocChecker::MallocBindRetVal(CheckerContext &C,
Expand Down
14 changes: 14 additions & 0 deletions clang/test/Analysis/malloc-annotations.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// RUN: -analyzer-checker=alpha.deadcode.UnreachableCode \
// RUN: -analyzer-checker=alpha.core.CastSize \
// RUN: -analyzer-checker=unix.Malloc \
// RUN: -analyzer-checker=debug.ExprInspection \
// RUN: -analyzer-config unix.DynamicMemoryModeling:Optimistic=true %s

typedef __typeof(sizeof(int)) size_t;
Expand All @@ -23,6 +24,12 @@ void __attribute((ownership_holds(malloc, 1))) my_hold(void *);
void __attribute((ownership_holds(malloc, 1)))
__attribute((ownership_holds(malloc, 1)))
__attribute((ownership_holds(malloc, 3))) my_hold2(void *, void *, void *);

__attribute((ownership_returns(user_malloc, 1))) void *user_malloc(size_t);
__attribute((ownership_takes(user_malloc, 1))) void user_free(void *);

void clang_analyzer_dump(int);

void *my_malloc3(size_t);
void *myglobalpointer;
struct stuff {
Expand Down Expand Up @@ -273,3 +280,10 @@ void testMultipleFreeAnnotations(void) {
my_freeBoth(p, q);
}

void testNoUninitAttr(void) {
int *p = user_malloc(sizeof(int));
int read = p[0]; // no-warning
clang_analyzer_dump(p[0]); // expected-warning{{Unknown}}
user_free(p);
}

Loading