Skip to content

Commit

Permalink
[analyzer] If realloc fails on an escaped region, that region doesn't…
Browse files Browse the repository at this point in the history
… leak.

When a region is realloc()ed, MallocChecker records whether it was known
to be allocated or not. If it is, and the reallocation fails, the original
region has to be freed. Previously, when an allocated region escaped,
MallocChecker completely stopped tracking it, so a failed reallocation
still (correctly) wouldn't require freeing the original region. Recently,
however, MallocChecker started tracking escaped symbols, so that if it were
freed we could check that the deallocator matched the allocator. This
broke the reallocation model for whether or not a symbol was allocated.

Now, MallocChecker will actually check if a symbol is owned, and only
require freeing after a failed reallocation if it was owned before.

PR16730

llvm-svn: 188468
  • Loading branch information
jrose-apple committed Aug 15, 2013
1 parent f661988 commit 2f8b022
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C,
}
}

ReleasedAllocated = (RsBase != 0);
ReleasedAllocated = (RsBase != 0) && RsBase->isAllocated();

// Clean out the info on previous call to free return info.
State = State->remove<FreeReturnValue>(SymBase);
Expand Down
10 changes: 10 additions & 0 deletions clang/test/Analysis/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,16 @@ void freeMemory() {
}
}

// PR16730
void testReallocEscaped(void **memory) {
*memory = malloc(47);
char *new_memory = realloc(*memory, 47);
if (new_memory != 0) {
*memory = new_memory;
}
}


// ----------------------------------------------------------------------------
// False negatives.

Expand Down

0 comments on commit 2f8b022

Please sign in to comment.