https://github.com/balazske updated https://github.com/llvm/llvm-project/pull/234672
From 1d70b7fba2ef7ba37d4987db018e0b9b060febc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <[email protected]> Date: Thu, 22 Oct 2026 17:57:03 +0200 Subject: [PATCH 1/3] [clang][analyzer] Add checker 'unix.fsetpos' --- clang/docs/analyzer/checkers.md | 35 ++++ .../clang/StaticAnalyzer/Checkers/Checkers.td | 5 + .../StaticAnalyzer/Checkers/CMakeLists.txt | 1 + .../Checkers/FsetposChecker.cpp | 172 ++++++++++++++++++ .../test/Analysis/analyzer-enabled-checkers.c | 1 + clang/test/Analysis/fsetpos.c | 125 +++++++++++++ ...c-library-functions-arg-enabled-checkers.c | 1 + 7 files changed, 340 insertions(+) create mode 100644 clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp create mode 100644 clang/test/Analysis/fsetpos.c diff --git a/clang/docs/analyzer/checkers.md b/clang/docs/analyzer/checkers.md index e2bbf52943bb87..fc35d18519c012 100644 --- a/clang/docs/analyzer/checkers.md +++ b/clang/docs/analyzer/checkers.md @@ -2207,6 +2207,41 @@ for possible later error handling. > To avoid possible false-positives `errno` is allowed to be used in this > case. +(unix-fsetpos)= + +#### unix.fsetpos (C) + +Check for calls to `fsetpos` that do not take a "`pos`" argument that was obtained from a previous call to `fgetpos` (on the same file). +This includes cases where the "`pos`" argument is set to a fixed value, or was returned from a `fgetpos` call on a different file, or the value is undefined. +This corresponds to CERT rule [FIO44-C Only use values for fsetpos() that are returned from fgetpos()](https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/rules/input-output-fio/fio44-c/). + +```c +void set_pos_1() { + FILE *F = tmpfile(); + if (!F) + return; + fpos_t Pos; + memset(&Pos, 0, sizeof(Pos)); + fsetpos(F, &Pos); // warn +} + +void set_pos_2() { + FILE *F1 = tmpfile(); + if (!F1) + return; + FILE *F2 = tmpfile(); + if (!F2) { + fclose(F1); + return; + } + fpos_t Pos1, Pos2; + fgetpos(F1, &Pos1); + fgetpos(F2, &Pos2); + fsetpos(F1, &Pos1); + fsetpos(F2, &Pos1); // warn +} +``` + (unix-malloc)= #### unix.Malloc (C) diff --git a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td index e1a1046efab430..f6bc120c318eab 100644 --- a/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td +++ b/clang/include/clang/StaticAnalyzer/Checkers/Checkers.td @@ -553,6 +553,11 @@ def ErrnoChecker : Checker<"Errno">, ]>, Documentation<HasDocumentation>; +def FsetposChecker: Checker<"fsetpos">, + HelpText<"Check for 'fsetpos' calls that do not take a 'pos' argument" + " returned from 'fgetpos'">, + Documentation<HasDocumentation>; + def MallocChecker: Checker<"Malloc">, HelpText<"Check for memory leaks, double free, and use-after-free problems. " "Traces memory managed by malloc()/free().">, diff --git a/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt b/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt index f136bd5434113b..a2973d6bf3714d 100644 --- a/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt +++ b/clang/lib/StaticAnalyzer/Checkers/CMakeLists.txt @@ -46,6 +46,7 @@ add_clang_library(clangStaticAnalyzerCheckers ErrnoModeling.cpp ErrnoTesterChecker.cpp ExprInspectionChecker.cpp + FsetposChecker.cpp FuchsiaHandleChecker.cpp GCDAntipatternChecker.cpp GenericTaintChecker.cpp diff --git a/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp new file mode 100644 index 00000000000000..cec3442eeef88b --- /dev/null +++ b/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp @@ -0,0 +1,172 @@ +//== FsetposChecker.cpp ---------------------------------------- -*- C++ -*--=// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file defines FsetposChecker which finds 'fsetpos' calls that do not take +// a 'pos' argument returned from 'fgetpos'. +// CERT SEI Rule FIO44-C +// +//===----------------------------------------------------------------------===// + +#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" +#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" +#include "clang/StaticAnalyzer/Core/Checker.h" +#include "clang/StaticAnalyzer/Core/CheckerManager.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CallDescription.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" +#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" + +using namespace clang; +using namespace ento; + +namespace { + +class FsetposChecker + : public Checker<check::Location, check::PreCall, check::PostCall, + check::PointerEscape, check::DeadSymbols> { +private: + BugType BT_InvalidFsetposArg{this, + "Call of 'fsetpos' with position argument that " + "was not obtained by 'fgetpos'", + categories::UnixAPI}; + + CallDescription FgetposCall{CDM::CLibrary, {"fgetpos"}, 2}; + CallDescription FsetposCall{CDM::CLibrary, {"fsetpos"}, 2}; + CallDescriptionSet OpenSet{ + {CDM::CLibrary, {"fopen"}, 2}, + {CDM::CLibrary, {"fdopen"}, 2}, + {CDM::CLibrary, {"freopen"}, 3}, + {CDM::CLibrary, {"tmpfile"}, 0}, + }; + +public: + void checkPreCall(const CallEvent &Call, CheckerContext &C) const; + void checkPostCall(const CallEvent &Call, CheckerContext &C) const; + void checkBind(SVal Loc, SVal Val, const Stmt *S, bool AtDeclInit, + CheckerContext &C) const; + void checkLocation(SVal L, bool IsLoad, const Stmt *S, + CheckerContext &C) const; + ProgramStateRef checkPointerEscape(ProgramStateRef State, + const InvalidatedSymbols &Escaped, + const CallEvent *Call, + PointerEscapeKind Kind) const; + void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const; +}; + +} // namespace + +/// Map a 'FILE *' value to a 'fpos_t *' value and also to the SVal at this +/// location. The 'fpos_t *' is used because the 'fpos_t' type can be a struct, +/// in this case it may not have a permanent SVal. +REGISTER_MAP_WITH_PROGRAMSTATE(FPosFile, SVal, SVal) + +void FsetposChecker::checkPreCall(const CallEvent &Call, + CheckerContext &C) const { + ProgramStateRef State = C.getState(); + + if (FsetposCall.matches(Call)) { + SVal FileVal = Call.getArgSVal(0); + if (FileVal.isUnknownOrUndef()) + return; + SVal FPosLocVal = Call.getArgSVal(1); + auto FPosLoc = FPosLocVal.getAs<Loc>(); + SVal FPosVal = FPosLoc ? State->getSVal(*FPosLoc) : UnknownVal{}; + SVal FPosFileVal = [&]() -> SVal { + if (FPosLoc) + if (const SVal *V = State->get<FPosFile>(*FPosLoc)) + return *V; + if (!FPosVal.isUnknownOrUndef()) + if (const SVal *V = State->get<FPosFile>(FPosVal)) + return *V; + return UnknownVal{}; + }(); + if ((!FPosFileVal.isUnknown() && FPosFileVal != FileVal) || + FPosLocVal.isUndef() || FPosVal.isUndef() || FPosVal.getAsInteger()) { + if (ExplodedNode *N = C.generateErrorNode()) { + SmallString<100> Str; + llvm::raw_svector_ostream Os(Str); + Os << "Position argument to 'fsetpos' "; + if (FPosLocVal.isUndef() || FPosVal.isUndef()) + Os << "(which is an undefined value) "; + else if (FPosVal.getAsInteger()) + Os << "(which is a constant value) "; + Os << "was not obained from previous call to 'fgetpos' on the same " + "file"; + auto R = std::make_unique<PathSensitiveBugReport>(BT_InvalidFsetposArg, + Str, N); + R->addRange(Call.getArgSourceRange(1)); + C.emitReport(std::move(R)); + } + return; + } + } +} + +void FsetposChecker::checkPostCall(const CallEvent &Call, + CheckerContext &C) const { + + ProgramStateRef State = C.getState(); + + if (FgetposCall.matches(Call)) { + SVal FileVal = Call.getArgSVal(0); + auto FPosLoc = Call.getArgSVal(1).getAs<Loc>(); + if (!FPosLoc) + return; + if (FileVal.isUnknownOrUndef()) + return; + + State = State->set<FPosFile>(*FPosLoc, FileVal); + if (SVal FPosVal = State->getSVal(*FPosLoc); !FPosVal.isUnknownOrUndef()) + State = State->set<FPosFile>(FPosVal, FileVal); + + C.addTransition(State); + return; + } +} + +void FsetposChecker::checkLocation(SVal L, bool IsLoad, const Stmt *S, + CheckerContext &C) const { + if (IsLoad) + return; + ProgramStateRef State = C.getState(); + if (State->get<FPosFile>(L)) { + State = State->remove<FPosFile>(L); + C.addTransition(State); + } +} + +ProgramStateRef FsetposChecker::checkPointerEscape( + ProgramStateRef State, const InvalidatedSymbols &Escaped, + const CallEvent *Call, PointerEscapeKind Kind) const { + FPosFileTy FPF = State->get<FPosFile>(); + for (auto [Loc, F] : FPF) { + if (Escaped.contains(Loc.getAsSymbol()) || + Escaped.contains(F.getAsSymbol())) + State = State->remove<FPosFile>(Loc); + } + return State; +} + +void FsetposChecker::checkDeadSymbols(SymbolReaper &SymReaper, + CheckerContext &C) const { + ProgramStateRef State = C.getState(); + FPosFileTy FPF = State->get<FPosFile>(); + for (auto [Loc, F] : FPF) { + SymbolRef LocSym = Loc.getAsSymbol(); + SymbolRef FSym = F.getAsSymbol(); + if ((LocSym && SymReaper.isDead(LocSym)) || + (FSym && SymReaper.isDead(FSym))) + State = State->remove<FPosFile>(Loc); + } + C.addTransition(State); +} + +void ento::registerFsetposChecker(CheckerManager &Mgr) { + Mgr.registerChecker<FsetposChecker>(); +} + +bool ento::shouldRegisterFsetposChecker(const CheckerManager &) { return true; } diff --git a/clang/test/Analysis/analyzer-enabled-checkers.c b/clang/test/Analysis/analyzer-enabled-checkers.c index 8371b0e7a410a8..8f4c6e4bc4796d 100644 --- a/clang/test/Analysis/analyzer-enabled-checkers.c +++ b/clang/test/Analysis/analyzer-enabled-checkers.c @@ -56,6 +56,7 @@ // CHECK-NEXT: unix.cstring.NotNullTerminated // CHECK-NEXT: unix.cstring.NullArg // CHECK-NEXT: unix.cstring.UninitializedRead +// CHECK-NEXT: unix.fsetpos int main() { int i; diff --git a/clang/test/Analysis/fsetpos.c b/clang/test/Analysis/fsetpos.c new file mode 100644 index 00000000000000..9c5ddd00da2fce --- /dev/null +++ b/clang/test/Analysis/fsetpos.c @@ -0,0 +1,125 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=unix -verify %s + +#include "Inputs/system-header-simulator.h" + +void set_pos_memset() { + FILE *F = tmpfile(); + if (!F) + return; + fpos_t Pos; + memset(&Pos, 0, sizeof(Pos)); + fsetpos(F, &Pos); // expected-warning {{Position argument to 'fsetpos' (which is a constant value) was not obained from previous call to 'fgetpos' on the same file}} + fclose(F); +} + +void set_pos_fixed(FILE *F) { + fpos_t Pos; + *(int*)(&Pos) = 0; + fsetpos(F, &Pos); // expected-warning {{Position argument to 'fsetpos' (which is a constant value) was not obained from previous call to 'fgetpos' on the same file}} +} + +void set_pos_uninit(FILE *F) { + fpos_t Pos; + fsetpos(F, &Pos); // expected-warning {{Position argument to 'fsetpos' (which is an undefined value) was not obained from previous call to 'fgetpos' on the same file}} +} + +void set_pos_unknown_param(FILE *F, const fpos_t *Pos) { + fsetpos(F, Pos); +} + +extern const fpos_t *get_pos(FILE *F); + +void set_pos_unknown_func() { + FILE *F = tmpfile(); + if (!F) + return; + const fpos_t *Pos = get_pos(F); + fsetpos(F, Pos); +} + +void set_pos_different_file() { + FILE *F1 = tmpfile(); + if (!F1) + return; + FILE *F2 = tmpfile(); + if (!F2) { + fclose(F1); + return; + } + fpos_t Pos1, Pos2; + if (fgetpos(F1, &Pos1) != 0 || fgetpos(F2, &Pos2) != 0) { + fclose(F1); + fclose(F2); + return; + } + fsetpos(F1, &Pos1); + fsetpos(F2, &Pos1); // expected-warning {{Position argument to 'fsetpos' was not obained from previous call to 'fgetpos' on the same file}} + fclose(F1); + fclose(F2); +} + +void set_pos_fixed_assign(FILE *F) { + fpos_t Pos; + *(int*)(&Pos) = 0; + fpos_t Pos1 = Pos; + fsetpos(F, &Pos1); // expected-warning {{Position argument to 'fsetpos' (which is a constant value) was not obained from previous call to 'fgetpos' on the same file}} +} + +void set_pos_assign() { + FILE *F = tmpfile(); + if (!F) + return; + fpos_t Pos1, Pos2; + if (fgetpos(F, &Pos1) != 0) { + fclose(F); + return; + } + Pos2 = Pos1; + fsetpos(F, &Pos2); + fclose(F); +} + +extern void may_change_pos(FILE *, fpos_t *); + +void set_pos_invalidate_1() { + FILE *F = tmpfile(); + if (!F) + return; + fpos_t Pos; + if (fgetpos(F, &Pos) != 0) { + fclose(F); + return; + } + may_change_pos(F, &Pos); + fsetpos(F, &Pos); + fclose(F); +} + +void set_pos_invalidate_2(FILE *F) { + fpos_t Pos; + may_change_pos(F, &Pos); + fsetpos(F, &Pos); +} + +void set_pos_different_file_assign() { + FILE *F1 = tmpfile(); + if (!F1) + return; + FILE *F2 = tmpfile(); + if (!F2) { + fclose(F1); + return; + } + fpos_t Pos1, Pos2, Pos3; + if (fgetpos(F1, &Pos1) != 0 || fgetpos(F2, &Pos2) != 0) { + fclose(F1); + fclose(F2); + return; + } + Pos3 = Pos1; + fsetpos(F1, &Pos1); + // if the 'fpos_t' type is a struct (not a simple value) this test does not pass + fsetpos(F2, &Pos3); // expected-warning {{Position argument to 'fsetpos' was not obained from previous call to 'fgetpos' on the same file}} + fclose(F1); + fclose(F2); +} diff --git a/clang/test/Analysis/std-c-library-functions-arg-enabled-checkers.c b/clang/test/Analysis/std-c-library-functions-arg-enabled-checkers.c index e1f365cdfbcf63..8e57e82706bd77 100644 --- a/clang/test/Analysis/std-c-library-functions-arg-enabled-checkers.c +++ b/clang/test/Analysis/std-c-library-functions-arg-enabled-checkers.c @@ -64,6 +64,7 @@ // CHECK-NEXT: unix.cstring.NotNullTerminated // CHECK-NEXT: unix.cstring.NullArg // CHECK-NEXT: unix.cstring.UninitializedRead +// CHECK-NEXT: unix.fsetpos int main() { int i; From cb9d3bc0858443b1cb58f7f6bfa83152063c61bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <[email protected]> Date: Mon, 26 Oct 2026 14:35:20 +0100 Subject: [PATCH 2/3] code review update --- .../Checkers/FsetposChecker.cpp | 66 +++++++++++-------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp index cec3442eeef88b..2da2750d6c24c7 100644 --- a/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp @@ -36,18 +36,10 @@ class FsetposChecker CallDescription FgetposCall{CDM::CLibrary, {"fgetpos"}, 2}; CallDescription FsetposCall{CDM::CLibrary, {"fsetpos"}, 2}; - CallDescriptionSet OpenSet{ - {CDM::CLibrary, {"fopen"}, 2}, - {CDM::CLibrary, {"fdopen"}, 2}, - {CDM::CLibrary, {"freopen"}, 3}, - {CDM::CLibrary, {"tmpfile"}, 0}, - }; public: void checkPreCall(const CallEvent &Call, CheckerContext &C) const; void checkPostCall(const CallEvent &Call, CheckerContext &C) const; - void checkBind(SVal Loc, SVal Val, const Stmt *S, bool AtDeclInit, - CheckerContext &C) const; void checkLocation(SVal L, bool IsLoad, const Stmt *S, CheckerContext &C) const; ProgramStateRef checkPointerEscape(ProgramStateRef State, @@ -59,11 +51,38 @@ class FsetposChecker } // namespace -/// Map a 'FILE *' value to a 'fpos_t *' value and also to the SVal at this -/// location. The 'fpos_t *' is used because the 'fpos_t' type can be a struct, -/// in this case it may not have a permanent SVal. +/// Used to get the 'FILE *' value from a 'fpos_t *' value or the SVal at this +/// location. +/// For example at 'fsetpos(F, &P)' the 'F' value can be get by using '&P' +/// (pointer) or the 'P' (a value of 'fpos_t' type) as key. The 'fpos_t *' is +/// used as key because the 'fpos_t' type can be a struct, in this case it may +/// not have a permanent SVal. (But if it is an integral type tracking the +/// value works even at assignments of 'fpos_t' type.) REGISTER_MAP_WITH_PROGRAMSTATE(FPosFile, SVal, SVal) +namespace { + +SVal getFileVal(ProgramStateRef State, SVal LocVal, bool *IsInteger) { + if (LocVal.isUndef()) + return UndefinedVal{}; + auto FPosLoc = LocVal.getAs<Loc>(); + if (!FPosLoc) + return UnknownVal{}; + SVal FPosVal = State->getSVal(*FPosLoc); + if (FPosVal.getAsInteger()) + *IsInteger = true; + if (FPosVal.getAsInteger() || FPosVal.isUndef()) + return UndefinedVal{}; + if (const SVal *V = State->get<FPosFile>(*FPosLoc)) + return *V; + if (!FPosVal.isUnknownOrUndef()) + if (const SVal *V = State->get<FPosFile>(FPosVal)) + return *V; + return UnknownVal{}; +} + +} // namespace + void FsetposChecker::checkPreCall(const CallEvent &Call, CheckerContext &C) const { ProgramStateRef State = C.getState(); @@ -73,27 +92,19 @@ void FsetposChecker::checkPreCall(const CallEvent &Call, if (FileVal.isUnknownOrUndef()) return; SVal FPosLocVal = Call.getArgSVal(1); - auto FPosLoc = FPosLocVal.getAs<Loc>(); - SVal FPosVal = FPosLoc ? State->getSVal(*FPosLoc) : UnknownVal{}; - SVal FPosFileVal = [&]() -> SVal { - if (FPosLoc) - if (const SVal *V = State->get<FPosFile>(*FPosLoc)) - return *V; - if (!FPosVal.isUnknownOrUndef()) - if (const SVal *V = State->get<FPosFile>(FPosVal)) - return *V; - return UnknownVal{}; - }(); - if ((!FPosFileVal.isUnknown() && FPosFileVal != FileVal) || - FPosLocVal.isUndef() || FPosVal.isUndef() || FPosVal.getAsInteger()) { + bool IsInteger = false; + SVal FPosFileVal = getFileVal(State, FPosLocVal, &IsInteger); + if (FPosFileVal.isUnknown()) + return; + if (FPosFileVal.isUndef() || FPosFileVal != FileVal) { if (ExplodedNode *N = C.generateErrorNode()) { SmallString<100> Str; llvm::raw_svector_ostream Os(Str); Os << "Position argument to 'fsetpos' "; - if (FPosLocVal.isUndef() || FPosVal.isUndef()) - Os << "(which is an undefined value) "; - else if (FPosVal.getAsInteger()) + if (IsInteger) Os << "(which is a constant value) "; + else if (FPosFileVal.isUndef()) + Os << "(which is an undefined value) "; Os << "was not obained from previous call to 'fgetpos' on the same " "file"; auto R = std::make_unique<PathSensitiveBugReport>(BT_InvalidFsetposArg, @@ -101,7 +112,6 @@ void FsetposChecker::checkPreCall(const CallEvent &Call, R->addRange(Call.getArgSourceRange(1)); C.emitReport(std::move(R)); } - return; } } } From 6e68fb1653abd86bcb8dffd351ab8fad8f87ff4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20K=C3=A9ri?= <[email protected]> Date: Mon, 9 Nov 2026 11:19:45 +0100 Subject: [PATCH 3/3] fixed other 2 small issues --- clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp index 2da2750d6c24c7..4301a22e4cc43a 100644 --- a/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/FsetposChecker.cpp @@ -7,7 +7,7 @@ //===----------------------------------------------------------------------===// // // This file defines FsetposChecker which finds 'fsetpos' calls that do not take -// a 'pos' argument returned from 'fgetpos'. +// a 'pos' argument returned from 'fgetpos' on the same file. // CERT SEI Rule FIO44-C // //===----------------------------------------------------------------------===// @@ -60,9 +60,7 @@ class FsetposChecker /// value works even at assignments of 'fpos_t' type.) REGISTER_MAP_WITH_PROGRAMSTATE(FPosFile, SVal, SVal) -namespace { - -SVal getFileVal(ProgramStateRef State, SVal LocVal, bool *IsInteger) { +static SVal getFileVal(ProgramStateRef State, SVal LocVal, bool *IsInteger) { if (LocVal.isUndef()) return UndefinedVal{}; auto FPosLoc = LocVal.getAs<Loc>(); @@ -81,8 +79,6 @@ SVal getFileVal(ProgramStateRef State, SVal LocVal, bool *IsInteger) { return UnknownVal{}; } -} // namespace - void FsetposChecker::checkPreCall(const CallEvent &Call, CheckerContext &C) const { ProgramStateRef State = C.getState(); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
