Skip to content

Commit 8a839ea

Browse files
authored
[analyzer][NFC] Fix clang-tidy warning in Malloc and UnixApi checkers (#145719)
Mostly `else-after-return` and `else-after-continue` warnings
1 parent 205dcf7 commit 8a839ea

File tree

2 files changed

+24
-29
lines changed

2 files changed

+24
-29
lines changed

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// Depends on NewDeleteChecker.
3535
//
3636
// * MismatchedDeallocatorChecker
37-
// Enables checking whether memory is deallocated with the correspending
37+
// Enables checking whether memory is deallocated with the corresponding
3838
// allocation function in MallocChecker, such as malloc() allocated
3939
// regions are only freed by free(), new by delete, new[] by delete[].
4040
//
@@ -1372,8 +1372,8 @@ void MallocChecker::checkIfFreeNameIndex(ProgramStateRef State,
13721372
C.addTransition(State);
13731373
}
13741374

1375-
const Expr *getPlacementNewBufferArg(const CallExpr *CE,
1376-
const FunctionDecl *FD) {
1375+
static const Expr *getPlacementNewBufferArg(const CallExpr *CE,
1376+
const FunctionDecl *FD) {
13771377
// Checking for signature:
13781378
// void* operator new ( std::size_t count, void* ptr );
13791379
// void* operator new[]( std::size_t count, void* ptr );
@@ -1682,17 +1682,15 @@ ProgramStateRef MallocChecker::ProcessZeroAllocCheck(
16821682
const RefState *RS = State->get<RegionState>(Sym);
16831683
if (RS) {
16841684
if (RS->isAllocated())
1685-
return TrueState->set<RegionState>(Sym,
1686-
RefState::getAllocatedOfSizeZero(RS));
1687-
else
1688-
return State;
1689-
} else {
1690-
// Case of zero-size realloc. Historically 'realloc(ptr, 0)' is treated as
1691-
// 'free(ptr)' and the returned value from 'realloc(ptr, 0)' is not
1692-
// tracked. Add zero-reallocated Sym to the state to catch references
1693-
// to zero-allocated memory.
1694-
return TrueState->add<ReallocSizeZeroSymbols>(Sym);
1685+
return TrueState->set<RegionState>(
1686+
Sym, RefState::getAllocatedOfSizeZero(RS));
1687+
return State;
16951688
}
1689+
// Case of zero-size realloc. Historically 'realloc(ptr, 0)' is treated as
1690+
// 'free(ptr)' and the returned value from 'realloc(ptr, 0)' is not
1691+
// tracked. Add zero-reallocated Sym to the state to catch references
1692+
// to zero-allocated memory.
1693+
return TrueState->add<ReallocSizeZeroSymbols>(Sym);
16961694
}
16971695

16981696
// Assume the value is non-zero going forward.
@@ -1890,7 +1888,7 @@ void MallocChecker::reportTaintBug(StringRef Msg, ProgramStateRef State,
18901888
"Tainted Memory Allocation",
18911889
categories::TaintedData));
18921890
auto R = std::make_unique<PathSensitiveBugReport>(*BT_TaintedAlloc, Msg, N);
1893-
for (auto TaintedSym : TaintedSyms) {
1891+
for (const auto *TaintedSym : TaintedSyms) {
18941892
R->markInteresting(TaintedSym);
18951893
}
18961894
C.emitReport(std::move(R));
@@ -2277,11 +2275,12 @@ MallocChecker::FreeMemAux(CheckerContext &C, const Expr *ArgExpr,
22772275
HandleDoubleFree(C, ParentExpr->getSourceRange(), RsBase->isReleased(),
22782276
SymBase, PreviousRetStatusSymbol);
22792277
return nullptr;
2278+
}
22802279

22812280
// If the pointer is allocated or escaped, but we are now trying to free it,
22822281
// check that the call to free is proper.
2283-
} else if (RsBase->isAllocated() || RsBase->isAllocatedOfSizeZero() ||
2284-
RsBase->isEscaped()) {
2282+
if (RsBase->isAllocated() || RsBase->isAllocatedOfSizeZero() ||
2283+
RsBase->isEscaped()) {
22852284

22862285
// Check if an expected deallocation function matches the real one.
22872286
bool DeallocMatchesAlloc = RsBase->getAllocationFamily() == Family;
@@ -2857,9 +2856,7 @@ MallocChecker::ReallocMemAux(CheckerContext &C, const CallEvent &Call,
28572856

28582857
const CallExpr *CE = cast<CallExpr>(Call.getOriginExpr());
28592858

2860-
if (SuffixWithN && CE->getNumArgs() < 3)
2861-
return nullptr;
2862-
else if (CE->getNumArgs() < 2)
2859+
if ((SuffixWithN && CE->getNumArgs() < 3) || CE->getNumArgs() < 2)
28632860
return nullptr;
28642861

28652862
const Expr *arg0Expr = CE->getArg(0);

clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
223223
// All calls should at least provide arguments up to the 'flags' parameter.
224224
unsigned int MinArgCount = FlagsArgIndex + 1;
225225

226+
// The frontend should issue a warning for this case. Just return.
227+
if (Call.getNumArgs() < MinArgCount)
228+
return;
229+
226230
// If the flags has O_CREAT set then open/openat() require an additional
227231
// argument specifying the file mode (permission bits) for the created file.
228232
unsigned int CreateModeArgIndex = FlagsArgIndex + 1;
@@ -231,11 +235,7 @@ void UnixAPIMisuseChecker::CheckOpenVariant(CheckerContext &C,
231235
unsigned int MaxArgCount = CreateModeArgIndex + 1;
232236

233237
ProgramStateRef state = C.getState();
234-
235-
if (Call.getNumArgs() < MinArgCount) {
236-
// The frontend should issue a warning for this case. Just return.
237-
return;
238-
} else if (Call.getNumArgs() == MaxArgCount) {
238+
if (Call.getNumArgs() == MaxArgCount) {
239239
const Expr *Arg = Call.getArgExpr(CreateModeArgIndex);
240240
QualType QT = Arg->getType();
241241
if (!QT->isIntegerType()) {
@@ -541,17 +541,15 @@ void UnixAPIPortabilityChecker::CheckCallocZero(CheckerContext &C,
541541
if (argVal.isUnknownOrUndef()) {
542542
if (i == 0)
543543
continue;
544-
else
545-
return;
544+
return;
546545
}
547546

548547
if (IsZeroByteAllocation(state, argVal, &trueState, &falseState)) {
549548
if (ReportZeroByteAllocation(C, falseState, arg, "calloc"))
550549
return;
551-
else if (i == 0)
550+
if (i == 0)
552551
continue;
553-
else
554-
return;
552+
return;
555553
}
556554
}
557555

0 commit comments

Comments
 (0)