[clang] [clang-tools-extra] Add function check for windows platform (PR #106581)

via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 30 07:05:02 PDT 2024


https://github.com/fawdlstty updated https://github.com/llvm/llvm-project/pull/106581

>From f169f3c57a0a55c1a0dbb8f965bc17a87ceb98d7 Mon Sep 17 00:00:00 2001
From: fawdlstty <f at fawdlstty.com>
Date: Fri, 30 Aug 2024 00:23:39 +0800
Subject: [PATCH 1/2] add check for windows platforms api

---
 .../bugprone/NotNullTerminatedResultCheck.cpp |  2 +-
 clang/docs/analyzer/checkers.rst              |  2 +-
 clang/include/clang/Basic/Builtins.td         | 22 +++++++++++++++++++
 .../Checkers/CStringChecker.cpp               |  6 +++++
 4 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
index 977241e91b9a93..e2cf96c88b90bd 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NotNullTerminatedResultCheck.cpp
@@ -106,7 +106,7 @@ static const CallExpr *getStrlenExpr(const MatchFinder::MatchResult &Result) {
     if (const Decl *D = StrlenExpr->getCalleeDecl())
       if (const FunctionDecl *FD = D->getAsFunction())
         if (const IdentifierInfo *II = FD->getIdentifier())
-          if (II->isStr("strlen") || II->isStr("wcslen"))
+          if (II->isStr("strlen") || II->isStr("lstrlen") || II->isStr("wcslen"))
             return StrlenExpr;
 
   return nullptr;
diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index 89a1018e14c0e6..ca675ae37929fe 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1582,7 +1582,7 @@ Check the size argument passed into C string functions for common erroneous patt
 unix.cstring.NullArg (C)
 """"""""""""""""""""""""
 Check for null pointers being passed as arguments to C string functions:
-``strlen, strnlen, strcpy, strncpy, strcat, strncat, strcmp, strncmp, strcasecmp, strncasecmp, wcslen, wcsnlen``.
+``strlen, lstrlen, strnlen, strcpy, lstrcpy, strncpy, strcat, lstrcat, strncat, strcmp, strncmp, strcasecmp, strncasecmp, wcslen, wcsnlen``.
 
 .. code-block:: c
 
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 8668b25661dec8..cbc5fc52326ad2 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4788,3 +4788,25 @@ def ArithmeticFence : LangBuiltin<"ALL_LANGUAGES"> {
   let Attributes = [CustomTypeChecking, Constexpr];
   let Prototype = "void(...)";
 }
+
+// Windows - WinBase.h
+def LStrLen : LibBuiltin<"WinBase.h"> {
+  let Spellings = ["lstrlen"];
+  let Attributes = [NoThrow, Constexpr];
+  let Prototype = "int(LPCTSTR)";
+  let AddBuiltinPrefixedAlias = 1;
+}
+
+def LStrCpy : LibBuiltin<"WinBase.h"> {
+  let Spellings = ["lstrcpy"];
+  let Attributes = [NoThrow];
+  let Prototype = "LPCTSTR(LPTSTR, LPCTSTR)";
+  let AddBuiltinPrefixedAlias = 1;
+}
+
+def LStrCat : LibBuiltin<"WinBase.h"> {
+  let Spellings = ["lstrcat"];
+  let Attributes = [NoThrow];
+  let Prototype = "LPTSTR(LPTSTR, LPCTSTR)";
+  let AddBuiltinPrefixedAlias = 1;
+}
diff --git a/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
index 8dd08f14b2728b..2adf538486176d 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
@@ -150,6 +150,8 @@ class CStringChecker : public Checker< eval::Call,
       // FIXME: C23 introduces 'memset_explicit', maybe also model that
       {{CDM::CLibraryMaybeHardened, {"strcpy"}, 2},
        &CStringChecker::evalStrcpy},
+      {{CDM::CLibraryMaybeHardened, {"lstrcpy"}, 2},
+       &CStringChecker::evalStrcpy},
       {{CDM::CLibraryMaybeHardened, {"strncpy"}, 3},
        &CStringChecker::evalStrncpy},
       {{CDM::CLibraryMaybeHardened, {"stpcpy"}, 2},
@@ -158,12 +160,16 @@ class CStringChecker : public Checker< eval::Call,
        &CStringChecker::evalStrlcpy},
       {{CDM::CLibraryMaybeHardened, {"strcat"}, 2},
        &CStringChecker::evalStrcat},
+      {{CDM::CLibraryMaybeHardened, {"lstrcat"}, 2},
+       &CStringChecker::evalStrcat},
       {{CDM::CLibraryMaybeHardened, {"strncat"}, 3},
        &CStringChecker::evalStrncat},
       {{CDM::CLibraryMaybeHardened, {"strlcat"}, 3},
        &CStringChecker::evalStrlcat},
       {{CDM::CLibraryMaybeHardened, {"strlen"}, 1},
        &CStringChecker::evalstrLength},
+      {{CDM::CLibraryMaybeHardened, {"lstrlen"}, 1},
+       &CStringChecker::evalstrLength},
       {{CDM::CLibrary, {"wcslen"}, 1}, &CStringChecker::evalstrLength},
       {{CDM::CLibraryMaybeHardened, {"strnlen"}, 2},
        &CStringChecker::evalstrnLength},

>From 52d40b558e14948c38c8c5f75245c5ba08bd3fea Mon Sep 17 00:00:00 2001
From: fawdlstty <f at fawdlstty.com>
Date: Fri, 30 Aug 2024 00:33:22 +0800
Subject: [PATCH 2/2] add to insecureAPI

---
 clang/docs/analyzer/checkers.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/analyzer/checkers.rst b/clang/docs/analyzer/checkers.rst
index ca675ae37929fe..7a7b1926da17b4 100644
--- a/clang/docs/analyzer/checkers.rst
+++ b/clang/docs/analyzer/checkers.rst
@@ -1256,7 +1256,7 @@ Warn on uses of inferior random number generating functions (only if arc4random
 
 security.insecureAPI.strcpy (C)
 """""""""""""""""""""""""""""""
-Warn on uses of the ``strcpy`` and ``strcat`` functions.
+Warn on uses of the ``strcpy, lstrcpy, strcat, lstrcat`` functions.
 
 .. code-block:: c
 



More information about the cfe-commits mailing list