[clang] [clang][analyzer] Model more wide-character versions of string functions (PR #113908)

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 30 00:59:11 PDT 2024


================
@@ -2524,8 +2625,33 @@ void CStringChecker::evalStdCopyCommon(CheckerContext &C,
   C.addTransition(State);
 }
 
-void CStringChecker::evalMemset(CheckerContext &C,
-                                const CallEvent &Call) const {
+namespace {
+CharUnits getSizeOfUnit(CharKind CK, CheckerContext &C) {
+  assert(CK == CK_Regular || CK == CK_Wide);
+  auto UnitType =
+      CK == CK_Regular ? C.getASTContext().CharTy : C.getASTContext().WCharTy;
+
+  return C.getASTContext().getTypeSizeInChars(UnitType);
+}
+
+SVal getCharValCast(CharKind CK, CheckerContext &C, ProgramStateRef State,
+                    const Expr *CharE) {
+  const LocationContext *LCtx = C.getLocationContext();
+  auto CharVal = State->getSVal(CharE, LCtx);
+  if (CK == CK_Regular) {
+    auto &svalBuilder = C.getSValBuilder();
+    const auto &Ctx = C.getASTContext();
+    // With the semantic of 'memset()', we should convert the CharVal to
+    // unsigned char.
+    return svalBuilder.evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy);
----------------
steakhal wrote:

```suggestion
    const auto &Ctx = C.getASTContext();
    // With the semantic of 'memset()', we should convert the CharVal to
    // unsigned char.
    return C.getSValBuilder().evalCast(CharVal, Ctx.UnsignedCharTy, Ctx.IntTy);
```

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


More information about the cfe-commits mailing list