[clang] [analyzer] Compute length of string literal initializers (#66990) (PR #68368)

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 5 17:58:36 PDT 2023


https://github.com/luamfb created https://github.com/llvm/llvm-project/pull/68368

Fix issue https://github.com/llvm/llvm-project/issues/66990

>From 1f0249fdb0cf7b1799eb9e532bfbb4a3ffe983b0 Mon Sep 17 00:00:00 2001
From: luamfb <mcmfbarbosa at gmail.com>
Date: Thu, 5 Oct 2023 21:50:10 -0300
Subject: [PATCH] [analyzer] Compute length of string literal initializers
 (#66990)

---
 .../StaticAnalyzer/Checkers/CStringChecker.cpp  | 17 ++++++++++++++++-
 clang/test/Analysis/string.c                    | 11 +++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
index f1539f2733298d9..b3b15c35450acdc 100644
--- a/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp
@@ -930,9 +930,24 @@ SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
     const StringLiteral *strLit = cast<StringRegion>(MR)->getStringLiteral();
     return svalBuilder.makeIntVal(strLit->getLength(), sizeTy);
   }
+  case MemRegion::NonParamVarRegionKind: {
+    // If we have a global constant with a string literal initializer,
+    // compute the initializer's length.
+    const VarDecl *decl = cast<NonParamVarRegion>(MR)->getDecl();
+    if (decl->hasGlobalStorage()) {
+      if (const Expr *init = decl->getInit()) {
+        if (auto *strLit = dyn_cast<StringLiteral>(init)) {
+          SValBuilder &svalBuilder = C.getSValBuilder();
+          QualType sizeTy = svalBuilder.getContext().getSizeType();
+          return svalBuilder.makeIntVal(strLit->getLength(), sizeTy);
+        }
+      }
+    }
+    // Otherwise, fallback to this.
+    return getCStringLengthForRegion(C, state, Ex, MR, hypothetical);
+  }
   case MemRegion::SymbolicRegionKind:
   case MemRegion::AllocaRegionKind:
-  case MemRegion::NonParamVarRegionKind:
   case MemRegion::ParamVarRegionKind:
   case MemRegion::FieldRegionKind:
   case MemRegion::ObjCIvarRegionKind:
diff --git a/clang/test/Analysis/string.c b/clang/test/Analysis/string.c
index d369ee9f7d854a1..8da905b33145562 100644
--- a/clang/test/Analysis/string.c
+++ b/clang/test/Analysis/string.c
@@ -97,6 +97,17 @@ void strlen_constant2(char x) {
   clang_analyzer_eval(strlen(a) == 3); // expected-warning{{UNKNOWN}}
 }
 
+const char *const global_str_ptr = "abcd";
+const char global_str_arr[] = "efgh";
+
+void strlen_global_constant_ptr(void) {
+  clang_analyzer_eval(strlen(global_str_ptr) == 4); // expected-warning{{TRUE}}
+}
+
+void strlen_global_constant_arr(void) {
+  clang_analyzer_eval(strlen(global_str_arr) == 4); // expected-warning{{TRUE}}
+}
+
 size_t strlen_null(void) {
   return strlen(0); // expected-warning{{Null pointer passed as 1st argument to string length function}}
 }



More information about the cfe-commits mailing list