valgrind检测到未初始化的值:为什么以及在哪里?

时间:2017-01-17 18:36:43

标签: c++ valgrind

我有这个简单的功能:

template <>
std::string get_yaml_attribute(const YAML::Node& node, const std::string& attribute, const std::string& section)
{
    std::string value;
    try
    {
        value = node[attribute].as<std::string>();
    }
    catch (YAML::BadConversion)
    {
        throw std::invalid_argument("Error while converting attribute '"+attribute+"' in section '"+section+"' to string");
    }
    return value;
} 

在这个更大的代码中:https://bitbucket.org/specmicp/specmicp。上周我在特定设置(https://github.com/jupyter/help/issues/122)中运行代码时发现了一个问题。我认为这是未初始化变量的问题。我使用valgrind运行我的代码,并查看一长串错误,这似乎与之前显示的函数有关。

==2561== Conditional jump or move depends on uninitialised value(s)
==2561==    at 0x4C2F128: strlen (vg_replace_strmem.c:454)
==2561==    by 0x40EB1B6: specmicp::io::save_yaml(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, YAML::Emitter&) (char_traits.h:267)
==2561==    by 0x403A26E: specmicp::AdimensionalSystemSolutionSaver::save_solution(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, specmicp::AdimensionalSystemSolution const&) (adimensional_system_solution_saver.cpp:102)
==2561==    by 0x404D4A: specmicp::run_formulation(specmicp::io::YAMLConfigHandle&&, std::shared_ptr<specmicp::database::DataContainer> const&, specmicp::AdimensionalSystemSolverOptions const&) (specmicp.cpp:260)
==2561==    by 0x40C2B6: specmicp::run_specmicp(specmicp::io::YAMLConfigHandle&) (specmicp.cpp:192)
==2561==    by 0x4041E1: main (specmicp.cpp:148)
==2561==  Uninitialised value was created by a stack allocation
==2561==    at 0x40E9809: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > specmicp::io::get_yaml_attribute<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(YAML::Node const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (yaml.cpp:132)
==2561==
==2561== Syscall param writev(vector[...]) points to uninitialised byte(s)
==2561==    at 0x679FDE0: __writev_nocancel (in /lib64/libc-2.23.so)
==2561==    by 0x5A488C4: std::__basic_file<char>::xsputn_2(char const*, long, char const*, long) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/5.4.0/libstdc++.so.6.0.21)
==2561==    by 0x5A8716F: std::basic_filebuf<char, std::char_traits<char> >::xsputn(char const*, long) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/5.4.0/libstdc++.so.6.0.21)
==2561==    by 0x5AAE0E5: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/5.4.0/libstdc++.so.6.0.21)
==2561==    by 0x40EB1C4: specmicp::io::save_yaml(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, YAML::Emitter&) (ostream:561)
==2561==    by 0x403A26E: specmicp::AdimensionalSystemSolutionSaver::save_solution(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, specmicp::AdimensionalSystemSolution const&) (adimensional_system_solution_saver.cpp:102)
==2561==    by 0x404D4A: specmicp::run_formulation(specmicp::io::YAMLConfigHandle&&, std::shared_ptr<specmicp::database::DataContainer> const&, specmicp::AdimensionalSystemSolverOptions const&) (specmicp.cpp:260)
==2561==    by 0x40C2B6: specmicp::run_specmicp(specmicp::io::YAMLConfigHandle&) (specmicp.cpp:192)
==2561==    by 0x4041E1: main (specmicp.cpp:148)
==2561==  Address 0x75cc2ef is 175 bytes inside a block of size 2,766 alloc'd
==2561==    at 0x4C2C56F: operator new(unsigned long) (vg_replace_malloc.c:334)
==2561==    by 0x4E86D39: std::vector<char, std::allocator<char> >::_M_fill_insert(__gnu_cxx::__normal_iterator<char*, std::vector<char, std::allocator<char> > >, unsigned long, char const&) (in /usr/lib64/libyaml-cpp.so.0.5.3)
==2561==    by 0x4E86B61: YAML::ostream_wrapper::write(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib64/libyaml-cpp.so.0.5.3)
==2561==    by 0x4EA00DB: YAML::Emitter::Write(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib64/libyaml-cpp.so.0.5.3)
==2561==    by 0x406E300: operator<< (emitter.h:175)
==2561==    by 0x406E300: format_aqueous_molalities (adimensional_system_solution_saver.cpp:335)
==2561==    by 0x406E300: specmicp::AdimensionalSystemSolutionSaver::format_solution(specmicp::AdimensionalSystemSolution const&, YAML::Emitter&) [clone .constprop.119] (adimensional_system_solution_saver.cpp:171)
==2561==    by 0x4063CB0: specmicp::AdimensionalSystemSolutionSaver::set_yaml_emitter(specmicp::AdimensionalSystemSolution const&, YAML::Emitter&) [clone .constprop.118] (adimensional_system_solution_saver.cpp:119)
==2561==    by 0x403A263: specmicp::AdimensionalSystemSolutionSaver::save_solution(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, specmicp::AdimensionalSystemSolution const&) (adimensional_system_solution_saver.cpp:101)
==2561==    by 0x404D4A: specmicp::run_formulation(specmicp::io::YAMLConfigHandle&&, std::shared_ptr<specmicp::database::DataContainer> const&, specmicp::AdimensionalSystemSolverOptions const&) (specmicp.cpp:260)
==2561==    by 0x40C2B6: specmicp::run_specmicp(specmicp::io::YAMLConfigHandle&) (specmicp.cpp:192)
==2561==    by 0x4041E1: main (specmicp.cpp:148)
==2561==  Uninitialised value was created by a stack allocation
==2561==    at 0x40E9809: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > specmicp::io::get_yaml_attribute<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(YAML::Node const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (yaml.cpp:132)
==2561==

编辑:第132行对应于“std :: string value”行

我不明白问题出在哪里。初始化'value'不会改变输出(我不指望它)。这是valgrind和异常的问题吗?或者我是盲目的,错过了明显的?

编辑:将函数修改为单行时(无错误验证)获得类似错误(如下所示)

YAML :: Node是由yaml-cpp库创建的对象(https://github.com/jbeder/yaml-cpp

template <>
std::string get_yaml_attribute(const YAML::Node& node, const std::string& attribute, const std::string& section)
{
    return node[attribute].as<std::string>();
}

错误:

==6522== Syscall param writev(vector[...]) points to uninitialised byte(s)
==6522==    at 0x6361DE0: __writev_nocancel (in /lib64/libc-2.23.so)
==6522==    by 0x5A488C4: std::__basic_file<char>::xsputn_2(char const*, long, char const*, long) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/5.4.0/libstdc++.so.6.0.21)
==6522==    by 0x5A8716F: std::basic_filebuf<char, std::char_traits<char> >::xsputn(char const*, long) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/5.4.0/libstdc++.so.6.0.21)
==6522==    by 0x5AAE0E5: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/5.4.0/libstdc++.so.6.0.21)
==6522==    by 0x5AAE4D6: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) (in /usr/lib64/gcc/x86_64-pc-linux-gnu/5.4.0/libstdc++.so.6.0.21)
==6522==    by 0x5614FD: specmicp::io::save_yaml(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, YAML::Emitter&) (yaml.cpp:82)
==6522==    by 0x48E319: specmicp::AdimensionalSystemSolutionSaver::save_solution(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, specmicp::AdimensionalSystemSolution const&) (adimensional_system_solution_saver.cpp:102)
==6522==    by 0x486AA6: specmicp::run_formulation(specmicp::io::YAMLConfigHandle&&, std::shared_ptr<specmicp::database::DataContainer> const&, specmicp::AdimensionalSystemSolverOptions const&) (specmicp.cpp:260)
==6522==    by 0x486256: specmicp::run_specmicp(specmicp::io::YAMLConfigHandle&) (specmicp.cpp:192)
==6522==    by 0x485B7E: main (specmicp.cpp:148)
==6522==  Address 0x71c92af is 175 bytes inside a block of size 2,766 alloc'd
==6522==    at 0x4C2C56F: operator new(unsigned long) (vg_replace_malloc.c:334)
==6522==    by 0x4E86D39: std::vector<char, std::allocator<char> >::_M_fill_insert(__gnu_cxx::__normal_iterator<char*, std::vector<char, std::allocator<char> > >, unsigned long, char const&) (in /usr/lib64/libyaml-cpp.so.0.5.3)
==6522==    by 0x4E86B61: YAML::ostream_wrapper::write(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib64/libyaml-cpp.so.0.5.3)
==6522==    by 0x4EA00DB: YAML::Emitter::Write(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in /usr/lib64/libyaml-cpp.so.0.5.3)
==6522==    by 0x49422D: YAML::operator<<(YAML::Emitter&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (emitter.h:175)
==6522==    by 0x490965: specmicp::AdimensionalSystemSolutionSaver::format_aqueous_molalities(specmicp::AdimensionalSystemSolution const&, YAML::Emitter&) (adimensional_system_solution_saver.cpp:335)
==6522==    by 0x48EAF5: specmicp::AdimensionalSystemSolutionSaver::format_solution(specmicp::AdimensionalSystemSolution const&, YAML::Emitter&) (adimensional_system_solution_saver.cpp:171)
==6522==    by 0x48E559: specmicp::AdimensionalSystemSolutionSaver::set_yaml_emitter(specmicp::AdimensionalSystemSolution const&, YAML::Emitter&) (adimensional_system_solution_saver.cpp:119)
==6522==    by 0x48E306: specmicp::AdimensionalSystemSolutionSaver::save_solution(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, specmicp::AdimensionalSystemSolution const&) (adimensional_system_solution_saver.cpp:101)
==6522==    by 0x486AA6: specmicp::run_formulation(specmicp::io::YAMLConfigHandle&&, std::shared_ptr<specmicp::database::DataContainer> const&, specmicp::AdimensionalSystemSolverOptions const&) (specmicp.cpp:260)
==6522==    by 0x486256: specmicp::run_specmicp(specmicp::io::YAMLConfigHandle&) (specmicp.cpp:192)
==6522==    by 0x485B7E: main (specmicp.cpp:148)

编辑:

静态链接到yaml-cpp之后,错误现在是

...
==5783==  Uninitialised value was created by a stack allocation
==5783==    at 0x5AAC2D: YAML::Scanner::ScanQuotedScalar() (in /home/manawy/princeton/progra/specmicp-build/src/bin/specmicp/specmicp)

我猜这是图书馆里的一个错误。我不知道它会如何传播到我的代码。我必须分析它。

0 个答案:

没有答案