Skip to content

[analyzer] Fix uninitialized base class with initializer list when ctor is not declared in the base class (#70464) #70792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,14 @@ void ExprEngine::ProcessInitializer(const CFGInitializer CFGInit,
PostInitializer PP(BMI, FieldLoc.getAsRegion(), stackFrame);
evalBind(Tmp, Init, Pred, FieldLoc, InitVal, /*isInit=*/true, &PP);
}
} else if (BMI->isBaseInitializer() && isa<InitListExpr>(Init)) {
// When the base class is initialized with an initialization list and the
// base class does not have a ctor, there will not be a CXXConstructExpr to
// initialize the base region. Hence, we need to make the bind for it.
SVal BaseLoc = getStoreManager().evalDerivedToBase(
thisVal, QualType(BMI->getBaseClass(), 0), BMI->isBaseVirtual());
SVal InitVal = State->getSVal(Init, stackFrame);
evalBind(Tmp, Init, Pred, BaseLoc, InitVal, /*isInit=*/true);
} else {
assert(BMI->isBaseInitializer() || BMI->isDelegatingInitializer());
Tmp.insert(Pred);
Expand Down
68 changes: 68 additions & 0 deletions clang/test/Analysis/issue-70464.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// RUN: %clang_analyze_cc1 %s -verify -analyzer-checker=core,debug.ExprInspection

// Refer to https://github.com/llvm/llvm-project/issues/70464 for more details.
//
// When the base class does not have a declared constructor, the base
// initializer in the constructor of the derived class should use the given
// initializer list to finish the initialization of the base class.
//
// Also testing base constructor and delegate constructor to make sure this
// change will not break these two cases when a CXXConstructExpr is available.

void clang_analyzer_dump(int);

namespace init_list {

struct Base {
int foox;
};

class Derived : public Base {
public:
Derived() : Base{42} {
// The dereference to this->foox below should be initialized properly.
clang_analyzer_dump(this->foox); // expected-warning{{42 S32b}}
clang_analyzer_dump(foox); // expected-warning{{42 S32b}}
}
};

void entry() { Derived test; }

} // namespace init_list

namespace base_ctor_call {

struct Base {
int foox;
Base(int x) : foox(x) {}
};

class Derived : public Base {
public:
Derived() : Base{42} {
clang_analyzer_dump(this->foox); // expected-warning{{42 S32b}}
clang_analyzer_dump(foox); // expected-warning{{42 S32b}}
}
};

void entry() { Derived test; }

} // namespace base_ctor_call

namespace delegate_ctor_call {

struct Base {
int foox;
};

struct Derived : Base {
Derived(int parx) { this->foox = parx; }
Derived() : Derived{42} {
clang_analyzer_dump(this->foox); // expected-warning{{42 S32b}}
clang_analyzer_dump(foox); // expected-warning{{42 S32b}}
}
};

void entry() { Derived test; }

} // namespace delegate_ctor_call