Skip to content

clang-tidy reports uninitialized fields in format_string_checker #3541

Description

@kieranclancy

clang-tidy reports uninitialized fields in format_string_checker when building with -std=c++20.

Minimal example on https://godbolt.org/z/o79qcj5oo :

#include <fmt/core.h>

std::string f()
{
    return fmt::format("hello world");
}
$ clang-tidy --extra-arg -Ifmt/include --extra-arg -std=c++20 test.cpp
...
fmt/include/fmt/core.h:2610:15: warning: 5 uninitialized fields at the end of the constructor call [clang-analyzer-optin.cplusplus.UninitializedObject]
        types_{mapped_type_constant<Args, buffer_context<Char>>::value...} {}
              ^
/tmp/example/fmt/include/fmt/core.h:725:7: note: uninitialized field 'this->context_.num_args_'
  int num_args_;
      ^~~~~~~~~
/tmp/example/fmt/include/fmt/core.h:726:15: note: uninitialized pointer 'this->context_.types_'
  const type* types_;
              ^~~~~~
/tmp/example/fmt/include/fmt/core.h:399:15: note: uninitialized pointer 'this->context_.basic_format_parse_context::format_str_.data_'
  const Char* data_;
              ^~~~~
/tmp/example/fmt/include/fmt/core.h:400:10: note: uninitialized field 'this->context_.basic_format_parse_context::format_str_.size_'
  size_t size_;
         ^~~~~
/tmp/example/fmt/include/fmt/core.h:655:7: note: uninitialized field 'this->context_.basic_format_parse_context::next_arg_id_'
  int next_arg_id_;
      ^~~~~~~~~~~~
/tmp/example/test.cpp:5:24: note: Calling constructor for 'basic_format_string<char, >'
    return fmt::format("hello world");
                       ^~~~~~~~~~~~~
/tmp/example/fmt/include/fmt/core.h:2733:5: note: Taking true branch
    if constexpr (detail::count_named_args<Args...>() ==
    ^
/tmp/example/fmt/include/fmt/core.h:2737:47: note: Calling constructor for 'format_string_checker<char, >'
      detail::parse_format_string<true>(str_, checker(s));
                                              ^~~~~~~~~~
/tmp/example/fmt/include/fmt/core.h:2610:15: note: 5 uninitialized fields at the end of the constructor call
        types_{mapped_type_constant<Args, buffer_context<Char>>::value...} {}
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

fmtlib version: latest master at time of writing (661b23e), but as far as I can see this affects all versions after June 2022 (b135f1c).

I am getting this error with clang-tidy 15.0.7 on Linux but the same error also occurs on the clang-tidy in compiler explorer (linked above).

The fix seems to be relatively straightforward, I will submit a pull request.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions