-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[analyzer][NFC] Fix clang-tidy warning in Malloc and UnixApi checkers #145719
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
Open
vbvictor
wants to merge
2
commits into
llvm:main
Choose a base branch
from
vbvictor:fix-warnings-in-csa-unix-and-malloc-checkers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[analyzer][NFC] Fix clang-tidy warning in Malloc and UnixApi checkers #145719
vbvictor
wants to merge
2
commits into
llvm:main
from
vbvictor:fix-warnings-in-csa-unix-and-malloc-checkers
+24
−29
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clang-static-analyzer-1 Author: Baranov Victor (vbvictor) ChangesMostly Full diff: https://github.com/llvm/llvm-project/pull/145719.diff 2 Files Affected:
diff --git a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
index fa6e8e4146804..dd5000ea7d734 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
@@ -34,7 +34,7 @@
// Depends on NewDeleteChecker.
//
// * MismatchedDeallocatorChecker
-// Enables checking whether memory is deallocated with the correspending
+// Enables checking whether memory is deallocated with the corresponding
// allocation function in MallocChecker, such as malloc() allocated
// regions are only freed by free(), new by delete, new[] by delete[].
//
@@ -1372,8 +1372,8 @@ void MallocChecker::checkIfFreeNameIndex(ProgramStateRef State,
C.addTransition(State);
}
-const Expr *getPlacementNewBufferArg(const CallExpr *CE,
- const FunctionDecl *FD) {
+static const Expr *getPlacementNewBufferArg(const CallExpr *CE,
+ const FunctionDecl *FD) {
// Checking for signature:
// void* operator new ( std::size_t count, void* ptr );
// void* operator new[]( std::size_t count, void* ptr );
@@ -1682,17 +1682,15 @@ ProgramStateRef MallocChecker::ProcessZeroAllocCheck(
const RefState *RS = State->get<RegionState>(Sym);
if (RS) {
if (RS->isAllocated())
- return TrueState->set<RegionState>(Sym,
- RefState::getAllocatedOfSizeZero(RS));
- else
- return State;
- } else {
- // Case of zero-size realloc. Historically 'realloc(ptr, 0)' is treated as
- // 'free(ptr)' and the returned value from 'realloc(ptr, 0)' is not
- // tracked. Add zero-reallocated Sym to the state to catch references
- // to zero-allocated memory.
- return TrueState->add<ReallocSizeZeroSymbols>(Sym);
+ return TrueState->set<RegionState>(
+ Sym, RefState::getAllocatedOfSizeZero(RS));
+ return State;
}
+ // Case of zero-size realloc. Historically 'realloc(ptr, 0)' is treated as
+ // 'free(ptr)' and the returned value from 'realloc(ptr, 0)' is not
+ // tracked. Add zero-reallocated Sym to the state to catch references
+ // to zero-allocated memory.
+ return TrueState->add<ReallocSizeZeroSymbols>(Sym);
}
// Assume the value is non-zero going forward.
@@ -1890,7 +1888,7 @@ void MallocChecker::reportTaintBug(StringRef Msg, ProgramStateRef State,
"Tainted Memory Allocation",
categories::TaintedData));
auto R = std::make_unique<PathSensitiveBugReport>(*BT_TaintedAlloc, Msg, N);
- for (auto TaintedSym : TaintedSyms) {
+ for (const auto *TaintedSym : TaintedSyms) {
R->markInteresting(TaintedSym);
}
C.emitReport(std::move(R));
@@ -2280,8 +2278,9 @@ MallocChecker::FreeMemAux(CheckerContext &C, const Expr *ArgExpr,
// If the pointer is allocated or escaped, but we are now trying to free it,
// check that the call to free is proper.
- } else if (RsBase->isAllocated() || RsBase->isAllocatedOfSizeZero() ||
- RsBase->isEscaped()) {
+ }
+ if (RsBase->isAllocated() || RsBase->isAllocatedOfSizeZero() ||
+ RsBase->isEscaped()) {
// Check if an expected deallocation function matches the real one.
bool DeallocMatchesAlloc = RsBase->getAllocationFamily() == Family;
@@ -2857,9 +2856,7 @@ MallocChecker::ReallocMemAux(CheckerContext &C, const CallEvent &Call,
const CallExpr *CE = cast<CallExpr>(Call.getOriginExpr());
- if (SuffixWithN && CE->getNumArgs() < 3)
- return nullptr;
- else if (CE->getNumArgs() < 2)
+ if ((SuffixWithN && CE->getNumArgs() < 3) || CE->getNumArgs() < 2)
return nullptr;
const Expr *arg0Expr = CE->getArg(0);
diff --git a/clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp
index ce5887d56b181..e12261603757f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp
@@ -223,6 +223,10 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
// All calls should at least provide arguments up to the 'flags' parameter.
unsigned int MinArgCount = FlagsArgIndex + 1;
+ // The frontend should issue a warning for this case. Just return.
+ if (Call.getNumArgs() < MinArgCount)
+ return;
+
// If the flags has O_CREAT set then open/openat() require an additional
// argument specifying the file mode (permission bits) for the created file.
unsigned int CreateModeArgIndex = FlagsArgIndex + 1;
@@ -231,11 +235,7 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
unsigned int MaxArgCount = CreateModeArgIndex + 1;
ProgramStateRef state = C.getState();
-
- if (Call.getNumArgs() < MinArgCount) {
- // The frontend should issue a warning for this case. Just return.
- return;
- } else if (Call.getNumArgs() == MaxArgCount) {
+ if (Call.getNumArgs() == MaxArgCount) {
const Expr *Arg = Call.getArgExpr(CreateModeArgIndex);
QualType QT = Arg->getType();
if (!QT->isIntegerType()) {
@@ -541,17 +541,15 @@ void UnixAPIPortabilityChecker::CheckCallocZero(CheckerContext &C,
if (argVal.isUnknownOrUndef()) {
if (i == 0)
continue;
- else
- return;
+ return;
}
if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
if (ReportZeroByteAllocation(C, falseState, arg, "calloc"))
return;
- else if (i == 0)
+ if (i == 0)
continue;
- else
- return;
+ return;
}
}
|
steakhal
approved these changes
Jun 25, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Mostly
else-after-return
andelse-after-continue
warnings