[clang] [Wunsafe-buffer-usage] Fix false positives in handling string literals. (PR #115552)

via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 8 14:11:11 PST 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff 5b697ef5dd6b3e29e257e6099014bf8d8e77ac9a 3be112ec1f0b2e6e2948db082a7141d91b873a17 --extensions cpp -- clang/lib/Analysis/UnsafeBufferUsage.cpp clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index 650d51bebd..6a72c7212f 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -436,13 +436,14 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
 
   const auto *BaseDRE =
       dyn_cast<DeclRefExpr>(Node.getBase()->IgnoreParenImpCasts());
-  const auto *SLiteral = dyn_cast<StringLiteral>(Node.getBase()->IgnoreParenImpCasts());
+  const auto *SLiteral =
+      dyn_cast<StringLiteral>(Node.getBase()->IgnoreParenImpCasts());
   uint64_t size;
 
   if (!BaseDRE && !SLiteral)
     return false;
 
-  if(BaseDRE) {
+  if (BaseDRE) {
     if (!BaseDRE->getDecl())
       return false;
     const auto *CATy = Finder->getASTContext().getAsConstantArrayType(
@@ -451,7 +452,7 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
       return false;
     }
     size = CATy->getLimitedSize();
-  } else if(SLiteral) {
+  } else if (SLiteral) {
     size = SLiteral->getLength();
   }
 
@@ -459,8 +460,7 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
     const APInt ArrIdx = IdxLit->getValue();
     // FIXME: ArrIdx.isNegative() we could immediately emit an error as that's a
     // bug
-    if (ArrIdx.isNonNegative() &&
-        ArrIdx.getLimitedValue() < size)
+    if (ArrIdx.isNonNegative() && ArrIdx.getLimitedValue() < size)
       return true;
   }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/115552


More information about the cfe-commits mailing list