diff --git a/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp index 673356319833..3f6549de56b0 100644 --- a/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/UndefResultChecker.cpp @@ -40,6 +40,15 @@ void UndefResultChecker::checkPostStmt(const BinaryOperator *B, ProgramStateRef state = C.getState(); const LocationContext *LCtx = C.getLocationContext(); if (state->getSVal(B, LCtx).isUndef()) { + + // Do not report assignments of uninitialized values inside swap functions. + // This should allow to swap partially uninitialized structs + // (radar://14129997) + if (const FunctionDecl *EnclosingFunctionDecl = + dyn_cast(C.getStackFrame()->getDecl())) + if (C.getCalleeName(EnclosingFunctionDecl) == "swap") + return; + // Generate an error node. ExplodedNode *N = C.generateSink(); if (!N) diff --git a/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp index e04f49c3746d..016e3c804592 100644 --- a/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/UndefinedAssignmentChecker.cpp @@ -38,6 +38,14 @@ void UndefinedAssignmentChecker::checkBind(SVal location, SVal val, if (!val.isUndef()) return; + // Do not report assignments of uninitialized values inside swap functions. + // This should allow to swap partially uninitialized structs + // (radar://14129997) + if (const FunctionDecl *EnclosingFunctionDecl = + dyn_cast(C.getStackFrame()->getDecl())) + if (C.getCalleeName(EnclosingFunctionDecl) == "swap") + return; + ExplodedNode *N = C.generateSink(); if (!N) diff --git a/clang/test/Analysis/uninit-vals-ps-region.m b/clang/test/Analysis/uninit-vals-ps-region.m index 614ce2fc3354..a4aa5a114aeb 100644 --- a/clang/test/Analysis/uninit-vals-ps-region.m +++ b/clang/test/Analysis/uninit-vals-ps-region.m @@ -76,3 +76,18 @@ void PR10163 (void) { test_PR10163(x[1]); // expected-warning{{uninitialized value}} } +struct MyStr { + int x; + int y; +}; +void swap(struct MyStr *To, struct MyStr *From) { + // This is not really a swap but close enough for our test. + To->x = From->x; + To->y = From->y; // no warning +} +int test_undefined_member_assignment_in_swap(struct MyStr *s2) { + struct MyStr s1; + s1.x = 5; + swap(s2, &s1); + return s2->y; // expected-warning{{Undefined or garbage value returned to caller}} +}