[clang] [clang][analyzer] Support strlen with offset to string literal in CStringChecker (PR #159795)
Balazs Benics via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 2 04:29:29 PDT 2025
=?utf-8?q?Balázs_Kéri?= <balazs.keri at ericsson.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/159795 at github.com>
================
@@ -1042,10 +1049,29 @@ SVal CStringChecker::getCStringLength(CheckerContext &C, ProgramStateRef &state,
case MemRegion::CompoundLiteralRegionKind:
// FIXME: Can we track this? Is it necessary?
return UnknownVal();
- case MemRegion::ElementRegionKind:
- // FIXME: How can we handle this? It's not good enough to subtract the
- // offset from the base string length; consider "123\x00567" and &a[5].
+ case MemRegion::ElementRegionKind: {
+ // If an offset into the string literal is used, use the original length
+ // minus the offset.
+ // FIXME: Embedded null characters are not handled.
+ const ElementRegion *ER = cast<ElementRegion>(MR);
+ const SubRegion *SuperReg =
+ cast<SubRegion>(ER->getSuperRegion()->StripCasts());
+ const StringLiteral *StrLit = getStringLiteralFromRegion(SuperReg);
+ if (!StrLit)
+ return UnknownVal();
+ SValBuilder &SVB = C.getSValBuilder();
+ NonLoc Idx = ER->getIndex();
+ NonLoc LengthVal =
+ SVB.makeIntVal(StrLit->getLength(), SVB.getContext().getSizeType())
----------------
steakhal wrote:
`SVB.getContext().getSizeType()` is a common subexpression in this hunk. Hoist it?
https://github.com/llvm/llvm-project/pull/159795
More information about the cfe-commits
mailing list