[clang] [analyzer] Avoid fuzzy builtin matching for predefined library functions (PR #244425)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 19 14:49:06 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-static-analyzer-1
Author: Venkatesh Srinivasan (venk-ks)
<details>
<summary>Changes</summary>
`CheckerContext::isCLibraryFunction` performs `_`-delimited fuzzy substring matching whenever `FD->getBuiltinID() != 0` so that compiler-prefixed builtin variants such as `__builtin_memcpy` or `__builtin_memcpy_inline` match `"memcpy"`.
However, predefined C/POSIX library functions registered via `LibBuiltin` / `GNULibBuiltin` also have non-zero builtin IDs. Applying fuzzy matching to them causes distinct library functions whose names contain another library function name separated by `_` to falsely match the base name — for example, `explicit_bzero` matches `"bzero"`, `memset_explicit` matches `"memset"`, and `objc_read_weak` matches `"read"`. When adding further builtins such as `getlogin_r` or `ttyname_r`, this also causes `GenericTaintChecker` to misclassify them as `getlogin`/`ttyname`.
Restrict fuzzy builtin matching to non-library builtins (`!BuiltinInfo.isPredefinedLibFunction(BId)`), falling through to exact matching for predefined library builtins while preserving fuzzy matching for `__builtin_*` variants.
Assisted-by: Gemini
---
Full diff: https://github.com/llvm/llvm-project/pull/244425.diff
3 Files Affected:
- (modified) clang/lib/StaticAnalyzer/Core/CheckerContext.cpp (+29-23)
- (modified) clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp (+30)
- (modified) clang/unittests/StaticAnalyzer/IsCLibraryFunctionTest.cpp (+20)
``````````diff
diff --git a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
index 4f3887aa967c37..cce8f05112f0cd 100644
--- a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
@@ -55,29 +55,35 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
if (BId != 0) {
if (Name.empty())
return true;
- std::string BName = FD->getASTContext().BuiltinInfo.getName(BId);
- size_t start = BName.find(Name);
- if (start != StringRef::npos) {
- // Accept exact match.
- if (BName.size() == Name.size())
- return true;
-
- // v-- match starts here
- // ...xxxxx...
- // _xxxxx_
- // ^ ^ lookbehind and lookahead characters
-
- const auto MatchPredecessor = [&]() -> bool {
- return start <= 0 || !llvm::isAlpha(BName[start - 1]);
- };
- const auto MatchSuccessor = [&]() -> bool {
- std::size_t LookbehindPlace = start + Name.size();
- return LookbehindPlace >= BName.size() ||
- !llvm::isAlpha(BName[LookbehindPlace]);
- };
-
- if (MatchPredecessor() && MatchSuccessor())
- return true;
+ const Builtin::Context &BuiltinInfo = FD->getASTContext().BuiltinInfo;
+ std::string BName = BuiltinInfo.getName(BId);
+ // Accept exact match.
+ if (BName == Name)
+ return true;
+ // Only perform fuzzy name matching for non-library builtins (e.g.
+ // __builtin_memcpy, __builtin_memcpy_inline), not for distinct predefined
+ // library functions (e.g. explicit_bzero vs bzero, memset_explicit vs
+ // memset).
+ if (!BuiltinInfo.isPredefinedLibFunction(BId)) {
+ size_t start = BName.find(Name);
+ if (start != StringRef::npos) {
+ // v-- match starts here
+ // ...xxxxx...
+ // _xxxxx_
+ // ^ ^ lookbehind and lookahead characters
+
+ const auto MatchPredecessor = [&]() -> bool {
+ return start <= 0 || !llvm::isAlpha(BName[start - 1]);
+ };
+ const auto MatchSuccessor = [&]() -> bool {
+ std::size_t LookbehindPlace = start + Name.size();
+ return LookbehindPlace >= BName.size() ||
+ !llvm::isAlpha(BName[LookbehindPlace]);
+ };
+
+ if (MatchPredecessor() && MatchSuccessor())
+ return true;
+ }
}
}
diff --git a/clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp b/clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp
index bdc0699c5cc67c..0e6fce0877fcb3 100644
--- a/clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp
+++ b/clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp
@@ -577,6 +577,36 @@ TEST(CallDescription, MatchBuiltins) {
XXX_func_XXX();
})"));
}
+ {
+ SCOPED_TRACE("predefined lib builtins do not fuzzy match other lib names");
+ EXPECT_TRUE(tooling::runToolOnCode(
+ std::unique_ptr<FrontendAction>(new CallDescriptionAction<>(
+ {{{CDM::CLibrary, {"bzero"}, 2}, false},
+ {{CDM::CLibrary, {"explicit_bzero"}, 2}, true}})),
+ R"(typedef __typeof(sizeof(int)) size_t;
+ extern "C" void explicit_bzero(void *, size_t);
+ void test(void *p) {
+ explicit_bzero(p, 4);
+ })"));
+ EXPECT_TRUE(tooling::runToolOnCode(
+ std::unique_ptr<FrontendAction>(new CallDescriptionAction<>(
+ {{{CDM::CLibrary, {"memset"}, 3}, false},
+ {{CDM::CLibrary, {"memset_explicit"}, 3}, true}})),
+ R"(typedef __typeof(sizeof(int)) size_t;
+ extern "C" void *memset_explicit(void *, int, size_t);
+ void test(void *p) {
+ memset_explicit(p, 0, 4);
+ })"));
+ }
+ {
+ SCOPED_TRACE("compiler-prefixed inline builtin still fuzzy matches");
+ EXPECT_TRUE(tooling::runToolOnCode(
+ std::unique_ptr<FrontendAction>(new CallDescriptionAction<>(
+ {{{CDM::CLibrary, {"memcpy"}, 3}, true}})),
+ R"(void test(void *dst, const void *src) {
+ __builtin_memcpy_inline(dst, src, 4);
+ })"));
+ }
}
//===----------------------------------------------------------------------===//
diff --git a/clang/unittests/StaticAnalyzer/IsCLibraryFunctionTest.cpp b/clang/unittests/StaticAnalyzer/IsCLibraryFunctionTest.cpp
index d6dfcaac6f3bdc..83637e33d94e56 100644
--- a/clang/unittests/StaticAnalyzer/IsCLibraryFunctionTest.cpp
+++ b/clang/unittests/StaticAnalyzer/IsCLibraryFunctionTest.cpp
@@ -82,3 +82,23 @@ TEST_F(IsCLibraryFunctionTest, RejectsClassMember) {
ASSERT_TRUE(buildAST(R"cpp(class A { void fun(); };)cpp"));
EXPECT_FALSE(CheckerContext::isCLibraryFunction(getFunctionDecl()));
}
+
+TEST_F(IsCLibraryFunctionTest, ExactMatchForPredefinedLibBuiltins) {
+ ASSERT_TRUE(buildAST(R"cpp(
+ typedef __typeof(sizeof(int)) size_t;
+ extern "C" void explicit_bzero(void *, size_t);
+ )cpp"));
+ ASSERT_NE(getFunctionDecl()->getBuiltinID(), 0u);
+ EXPECT_TRUE(
+ CheckerContext::isCLibraryFunction(getFunctionDecl(), "explicit_bzero"));
+ EXPECT_FALSE(CheckerContext::isCLibraryFunction(getFunctionDecl(), "bzero"));
+
+ ASSERT_TRUE(buildAST(R"cpp(
+ typedef __typeof(sizeof(int)) size_t;
+ extern "C" void *memset_explicit(void *, int, size_t);
+ )cpp"));
+ ASSERT_NE(getFunctionDecl()->getBuiltinID(), 0u);
+ EXPECT_TRUE(
+ CheckerContext::isCLibraryFunction(getFunctionDecl(), "memset_explicit"));
+ EXPECT_FALSE(CheckerContext::isCLibraryFunction(getFunctionDecl(), "memset"));
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/244425
More information about the cfe-commits
mailing list