Skip to content

clang-static-analyzer: Aggregate-initialized struct field incorrectly marked undefined #116444

@rojer

Description

@rojer

This code:

#include <iostream>
#include <string>

struct Structy {
  std::string foo;
  int bar;
};

int main() {
  auto *s = new Structy{
      .foo = "foo",
      .bar = 1,
  };
  std::cout << s->foo << s->bar;
  delete s;
}

produces the following static analyzer warning:

$ clang-tidy -p . qq.cpp 
1 warning generated.
/home/rojer/allterco/shelly-ng3/qq.cpp:14:3: error: 1st function call argument is an uninitialized value [clang-analyzer-core.CallAndMessage,-warnings-as-errors]
   14 |   std::cout << s->foo << s->bar;
      |   ^                      ~~~~~~
/home/rojer/allterco/shelly-ng3/qq.cpp:10:13: note: Uninitialized value stored to field 'bar'
   10 |   auto *s = new Structy{
      |             ^~~~~~~~~~~~
   11 |       .foo = "foo",
      |       ~~~~~~~~~~~~~
   12 |       .bar = 1,
      |       ~~~~~~~~~
   13 |   };
      |   ~
/home/rojer/allterco/shelly-ng3/qq.cpp:14:3: note: 1st function call argument is an uninitialized value
   14 |   std::cout << s->foo << s->bar;
      |   ^                      ~~~~~~
1 warning treated as error

s->bar is of course explicitly initialized to 1, so the generated warning is a false-positive.

swapping field order to bar, foo makes it go away.

tested with latest main (20.0.0git ec353b7).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions