================
@@ -684,11 +687,56 @@ void WalkAST::checkCall_mkstemp(const CallExpr *CE, const 
FunctionDecl *FD) {
 // the Bounds of a Memory Buffer
 
//===----------------------------------------------------------------------===//
 
+
+void WalkAST::emitUnboundedStringDiagnostic(const CallExpr *CE, StringRef Name,
+                                            StringRef NarrowReplacement,
+                                            bool IsWide) {
+  // Construct the warning title and description.
+  SmallString<128> Buf1;
+  SmallString<512> Buf2;
+  llvm::raw_svector_ostream Out1(Buf1);
+  llvm::raw_svector_ostream Out2(Buf2);
+
+  Out1 << "Potential insecure memory buffer bounds restriction in call '"
+       << Name << "'";
+
+  Out2 << "Call to function '" << Name
+       << "' is insecure as it does not "
+          "provide bounding of the memory buffer. Replace "
+          "unbounded copy functions with analogous functions that "
+          "support length arguments";
+
+  if (IsWide)
+    Out2 << " for wide-character strings";
+  else
+    Out2 << " such as '" << NarrowReplacement << "'";
+
+  Out2 << ". CWE-119.";
+
+  // Issue a warning.
+  PathDiagnosticLocation CELoc =
+      PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), AC);
+
+  BR.EmitBasicReport(AC->getDecl(), filter.checkName_strcpy, Out1.str(),
+                     "Security", Out2.str(), CELoc,
+                     CE->getCallee()->getSourceRange());
+}
+
+
 void WalkAST::checkCall_strcpy(const CallExpr *CE, const FunctionDecl *FD) {
   if (!filter.check_strcpy)
     return;
 
-  if (!checkCall_strCommon(CE, FD))
+  // Get the function name and normalize fortified variants.
+  StringRef Name = FD->getIdentifier()->getName();
+  Name.consume_front("__builtin_");
+
----------------
NagyDonat wrote:

Yes, `CallDescription`s have a `CLibraryMaybeHardened` mode which is exactly 
intended to perform this sort of canonization (stripping `builtin` prefix and 
`_chk` suffix).

Note that `CallDescription`s are primarily intended to match `CallEvent`s 
generated in path-sensitive analysis, but they also have a method called 
`mathcAsWritten` which lets them match AST expressions. You should probably use 
that.

https://github.com/llvm/llvm-project/pull/234582
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to