29 Finder->addMatcher(stringLiteral(containsNul()).bind(
"strlit"),
this);
32 if (!getLangOpts().CPlusPlus)
35 const auto StrLitWithNul =
36 ignoringParenImpCasts(stringLiteral(containsNul()).bind(
"truncated"));
39 const auto StringConstructorExpr = expr(anyOf(
40 cxxConstructExpr(argumentCountIs(1),
41 hasDeclaration(cxxMethodDecl(hasName(
"basic_string")))),
44 cxxConstructExpr(argumentCountIs(2),
45 hasDeclaration(cxxMethodDecl(hasName(
"basic_string"))),
46 hasArgument(1, cxxDefaultArgExpr()))));
51 traverse(TK_AsIs, cxxConstructExpr(StringConstructorExpr,
52 hasArgument(0, StrLitWithNul))),
56 Finder->addMatcher(cxxOperatorCallExpr(hasAnyArgument(StrLitWithNul)),
this);
60 const MatchFinder::MatchResult &Result) {
61 if (
const auto *SL = Result.Nodes.getNodeAs<StringLiteral>(
"strlit")) {
62 for (
size_t Offset = 0, Length = SL->getLength(); Offset < Length;
65 if (Offset + 3 < Length && SL->getCodeUnit(Offset) ==
'\0' &&
66 SL->getCodeUnit(Offset + 1) ==
'x' &&
67 isDigit(SL->getCodeUnit(Offset + 2)) &&
68 isDigit(SL->getCodeUnit(Offset + 3))) {
69 diag(SL->getBeginLoc(),
"suspicious embedded NUL character");
75 if (
const auto *SL = Result.Nodes.getNodeAs<StringLiteral>(
"truncated")) {
76 diag(SL->getBeginLoc(),
77 "truncated string literal with embedded NUL character");