c++ - boost::spirit::hold_any 内存损坏

标签 c++ boost boost-spirit memory-corruption boost-any

我有一个很大的代码库可以使用 boost::anyboost::spirit::hold_any (取决于宏定义)。

hold_any 似乎与 boost::any 兼容(例如 How to print boost::any to a stream?Type erasure - Part IV )并且速度更快( Why you shouldn’t use boost::any ),但我遇到了一些问题使用 hold_any 出现段错误(Boost v1.55/1.54/1.53)。

这是一个最小的工作示例,它表现出与原始代码相同的问题:

#include <iostream>
#include <string>
#include <vector>

#include <boost/spirit/home/support/detail/hold_any.hpp>

typedef boost::spirit::hold_any any;
typedef std::vector<any> vany;

int main()
{
  vany data0, data1;

  for (unsigned i(0); i < 1000; ++i)
  {
    std::string s("test_test_test");
    data0.push_back(any(s));
  }

  const unsigned n(data0.size());
  vany::iterator iter(data0.begin());

  for (unsigned i(0); i < n; ++i)
  {
    std::cout << "Moving " << i << std::endl;

    data1.push_back(*iter);
    iter = data0.erase(iter);
  }

  return 0;
}

该程序似乎运行正常:

  • boost::spirit::hold_any 更改为 boost::any
  • hold_any 的内容更改为足够小的数据类型,以执行小型缓冲区优化(例如从 std::string 更改为 int )。

像 Boost Spirit 这样广泛使用的库中可能存在一些重大错误,这似乎很奇怪,但是

  • 我很难在示例中找到错误;
  • 我尝试过 g++/clang++ 但没有成功。

这个例子有什么问题吗?

最佳答案

出于某种原因,您不应该使用 hold_any,因为它在 detail/hold_any.hpp 中。

也就是说,hold_any 的复制分配似乎已被破坏。我创建了 a pull request on github 并提出修复建议。

如果没有修复,以下程序将演示 UB(因为编译器会生成首选的浅赋值运算符):

#include <iostream>
#include <string>

#include <boost/spirit/home/support/detail/hold_any.hpp>

typedef boost::spirit::hold_any any;

int main()
{
    any b;
    {
        any a;
        a = std::string("test_test_test");
        b = a;
    }

    std::cout << "b: " << b << '\n';
}

在 valgrind 下运行时:

==11827== Invalid read of size 8
==11827==    at 0x5E9D793: std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std
==11827==    by 0x4012FC: boost::spirit::detail::fxns<mpl_::bool_<true> >::type<std::string, char>::stream_out(std::ostream&, void* const*) (hold_any.hpp:113)
==11827==    by 0x4010F5: std::basic_ostream<char, std::char_traits<char> >& boost::spirit::operator<< <char>(std::basic_ostream<char, std::char_traits<char> >&, boost::spirit::basic_hold_any<char> const&) (hold_any.hpp:368)
==11827==    by 0x400FC9: main (test.cpp:17)
==11827==  Address 0x8ac1650 is 0 bytes inside a block of size 39 free'd
==11827==    at 0x4C2BADC: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==11827==    by 0x5EC405E: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.18)
==11827==    by 0x401204: boost::spirit::detail::fxns<mpl_::bool_<true> >::type<std::string, char>::static_delete(void**) (hold_any.hpp:89)
==11827==    by 0x401328: boost::spirit::basic_hold_any<char>::~basic_hold_any() (hold_any.hpp:246)
==11827==    by 0x4010B4: boost::spirit::basic_hold_any<char>::~basic_hold_any() (hold_any.hpp:245)
==11827==    by 0x400FA0: main (test.cpp:15)

关于c++ - boost::spirit::hold_any 内存损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24065769/

相关文章:

c++ - 灵气 : Inserting pre-defined output in a rule

c++ - boost spirit 解析器前瞻解析

c++ - boost::spirit::karma 语义 Action 分配错误

c++ - kdevelop 没有指定有效的可执行文件

c++ - 可变参数模板参数转发

c++ - Boost Graph Library 无向图 无平行边实现

c++ - Boost 和 std 的 result_of 问题,有些在 gcc 中编译,而在 msvc 中失败,反之亦然

c++ - QTableWidget信号(cellChanged)上的无限循环

c++ - 管理共享指针 C++

c++ - 如何升级 c++ builder 2010 boost 库