diff --git a/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp --- a/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/CStringChecker.cpp @@ -848,7 +848,15 @@ SValBuilder &svalBuilder = C.getSValBuilder(); QualType sizeTy = svalBuilder.getContext().getSizeType(); const StringLiteral *strLit = cast(MR)->getStringLiteral(); - return svalBuilder.makeIntVal(strLit->getByteLength(), sizeTy); + unsigned countx = 0; + // get the number of string literal characters by the target's "code unit" + // size, checking for an embedded literal of 0 up to the string literal's + // length. + for (countx = 0; + countx < strLit->getLength() && (strLit->getCodeUnit(countx) != 0); + countx++) + ; + return svalBuilder.makeIntVal(countx, sizeTy); } case MemRegion::SymbolicRegionKind: case MemRegion::AllocaRegionKind: diff --git a/clang/test/Analysis/string.c b/clang/test/Analysis/string.c --- a/clang/test/Analysis/string.c +++ b/clang/test/Analysis/string.c @@ -1652,3 +1652,8 @@ __builtin___memset_chk(&x, 0, sizeof(x), __builtin_object_size(&x, 0)); clang_analyzer_eval(x == 0); // expected-warning{{TRUE}} } + +void strcpy_no_overflow_2(char *y) { + char x[3]; + strcpy(x, "12\0"); +}