[clang] [analyzer] Check the correct first and last elements in cstring.UninitializedRead (PR #95408)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 24 05:29:45 PDT 2024


================
@@ -393,6 +401,162 @@ ProgramStateRef CStringChecker::checkNonNull(CheckerContext &C,
   return stateNonNull;
 }
 
+static std::optional<NonLoc> getIndex(ProgramStateRef State,
+                                      const ElementRegion *ER, CharKind CK) {
+  SValBuilder &SVB = State->getStateManager().getSValBuilder();
+  ASTContext &Ctx = SVB.getContext();
+
+  if (CK == CharKind::Regular) {
+    if (ER->getValueType() != Ctx.CharTy)
+      return {};
+    return ER->getIndex();
+  }
+
+  if (ER->getValueType() != Ctx.WideCharTy)
+    return {};
+
+  QualType SizeTy = Ctx.getSizeType();
+  NonLoc WideSize =
+      SVB.makeIntVal(Ctx.getTypeSizeInChars(Ctx.WideCharTy).getQuantity(),
+                     SizeTy)
+          .castAs<NonLoc>();
+  SVal Offset =
+      SVB.evalBinOpNN(State, BO_Mul, ER->getIndex(), WideSize, SizeTy);
+  if (Offset.isUnknown())
+    return {};
+  return Offset.castAs<NonLoc>();
+}
+
+// Try to get hold of the origin region (e.g. the actual array region from an
+// element region).
+static const TypedValueRegion *getOriginRegion(const ElementRegion *ER) {
+  const MemRegion *MR = ER->getSuperRegion();
+  const MemRegion *Ret = MR;
+  assert(MR);
+  if (const auto *sym = MR->getAs<SymbolicRegion>()) {
+    SymbolRef sym2 = sym->getSymbol();
+    if (!sym2)
+      return nullptr;
+    Ret = sym2->getOriginRegion();
+  }
+  return dyn_cast_or_null<TypedValueRegion>(Ret);
+}
+
+// Basically 1 -> 1st, 12 -> 12th, etc.
+static void printIdxWithOrdinalSuffix(llvm::raw_ostream &Os, unsigned Idx) {
+  Os << Idx << llvm::getOrdinalSuffix(Idx);
----------------
NagyDonat wrote:

Hmm, I agree that "(second)" doesn't look good when it represents an element index _(as highlighted)_:
![image](https://github.com/llvm/llvm-project/assets/43410265/bb01a19c-9d32-4a1d-a356-8cdf742d311d)
but that's already changed to "(at index 1)".

On the other hand, I still think "second argument" is significantly better than "2nd argument":
![image](https://github.com/llvm/llvm-project/assets/43410265/1e053a0d-7a38-47d6-8ebe-d66ebf46bba1)

I can accept "2nd argument" if you think that it's significantly better, but if you're indifferent, then please change it.


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


More information about the cfe-commits mailing list