llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Teach CFG-based uninitialized variable warnings about C++ member initializers

Open llvmbot opened this issue 15 years ago • 6 comments

Bugzilla Link 10962
Version unspecified
OS All
Reporter LLVM Bugzilla Contributor
CC @chandlerc,@devincoughlin,@tkremenek

Extended Description

Reduced from a real-life bug:

struct Object { Object(int *ptr) { *ptr = 5; } }; struct UsesObjects { UsesObjects() : obj(ptr), ptr(new int) {} Object obj; int *ptr; };

runUninitializedVariablesAnalysis only operates on VarDecls, which keeps it from complaining about the CFG for the UsesObjects constructor:

[ B2 (ENTRY) ] Predecessors (0): Successors (1): B1

[ B1 ] 1: this->ptr 2: [B1.1] 3: obj([B1.2]) (Member initializer) 4: ptr(new int) (Member initializer) Predecessors (1): B2 Successors (1): B0

[ B0 (EXIT) ] Predecessors (1): B1 Successors (0):

Since member initialization order is trivially known, applying the full power of the general uninitialized checker might be overkill, but it would be nice to have a warning here.

llvmbot avatar Sep 20 '11 08:09 llvmbot

Richard Trieu added Sema-based checking for uninitialized member initializers in r193386.

It does catch the common case of referring to a field whose member initializer hasn’t run yet:

struct Object { Object(int *ptr) { *ptr = 5; } }; struct UsesObjects { UsesObjects() : obj(ptr), ptr(new int) {} // warning: field 'ptr' is uninitialized when used here [-Wuninitialized] Object obj; int *ptr; };

Since that checking is Sema-based, it has an ad hoc understanding of control flow. It knows about ordering of CXXCtorInitializer initializers but not branches and join points.

This means it has false negatives in cases like:

struct S { int x, y, z; S(int i) : x(i ? z = 2 : 4), y(z), // Missing warning that 'z' is uninitialized here z(y) {} };

where a field is conditionally initiatlized in another field’s initializer.

Teaching the CFG-based uninitialized variables analysis about this would help catch this (somewhat unusual) case — but the common case is already handled.

devincoughlin avatar Feb 19 '18 19:02 devincoughlin

Moving to the correct component.

tkremenek avatar Sep 20 '11 19:09 tkremenek

cloned to rdar://problem/10153935

tkremenek avatar Sep 20 '11 19:09 tkremenek

It would be trivial to extent -Wuninitialized to this case, assuming the CFG support for member initializers is where it needs to be.

tkremenek avatar Sep 20 '11 19:09 tkremenek

Reduced from a real-life bug:

struct Object { Object(int *ptr) { *ptr = 5; } }; struct UsesObjects { UsesObjects() : obj(ptr), ptr(new int) {} Object obj; int *ptr; };

runUninitializedVariablesAnalysis only operates on VarDecls, which keeps it from complaining about the CFG for the UsesObjects constructor:

[ B2 (ENTRY) ] Predecessors (0): Successors (1): B1

[ B1 ] 1: this->ptr 2: [B1.1] 3: obj([B1.2]) (Member initializer) 4: ptr(new int) (Member initializer) Predecessors (1): B2 Successors (1): B0

[ B0 (EXIT) ] Predecessors (1): B1 Successors (0):

Since member initialization order is trivially known, applying the full power of the general uninitialized checker might be overkill, but it would be nice to have a warning here.

I would prefer to teach the general uninit checker to reason about these constructs. Member initializers can be complex beasts, including control flow. I don't like it one bit, but the following is (I believe) valid:

% cat x5.cc #include <stdio.h>

struct S { int x, y, z; S(int i) : x(y = i), y((x&1)? ++y : y++), z(y) {} };

int main() { S s(1); printf("x: %d, y: %d, z: %d\n", s.x, s.y, s.z); }

chandlerc avatar Sep 20 '11 09:09 chandlerc

assigned to @devincoughlin

llvmbot avatar Sep 20 '11 08:09 llvmbot