c++ - 尽管 try-catch 异常仍泄漏到系统

标签 c++ visual-c++

我在继承的应用程序中有以下代码,使用 VS2012 针对 boost 1.48.0 构建

bool ConvertToBoolean(const std::string& s)
{
  try
  {
    return boost::lexical_cast<bool>(s);
  }
  catch (...)
  {
    if (boost::iequals("true", s.c_str()))
    {
      return true;
    }
  }
  return false;
}

如果您将“True”或“False”传递给此方法,lexical_cast 将抛出一个 bad_lexical_cast 异常,因为它需要“0”或“1”并将评估字符串比较。

这似乎在我的机器上工作正常,无论是在调试器内还是在调试器外(不是总是这样吗?:)),但在我们的一台客户机器上,异常以某种方式“泄漏”并导致以下消息使用转储文件进行调试:

Unhandled exception at 0x000007FEFD08A06D in application.exe_161117_152748.dmp: Microsoft C++ exception: boost::exception_detail::clone_impl > at memory location 0x00000000002CD9B8.

堆栈跟踪:

    KERNELBASE.dll!RaiseException() Unknown
    snowagent.exe!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 154    C++
    application.exe!boost::throw_exception<boost::bad_lexical_cast>(const boost::bad_lexical_cast & e) Line 61  C++
    application.exe!boost::detail::lexical_cast_do_cast<bool,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >::lexical_cast_impl(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & arg) Line 1750  C++
    application.exe!ConvertToBoolean(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & s) Line 111   C++
    application.exe!CScanner::Exec() Line 326   C++

什么会导致这种泄漏?你很少会责怪编译器,但因为有一个 similar issuebeen fixed在 VS2015 中,我很想这样做,但为什么它不会在我的机器上发生?会不会是因为我同时安装了 VS2015 和 VS2012,因此有一个更新的运行时?

最后,下面反汇编的异常处理在哪里?我不是 ASM 方面的专家,但我希望它更适合此功能的 ASM。我什至看不到对 boost::iequals 的调用 更新:存在异常处理,只是不在同一程序集 block 中。所以链接编译器问题似乎与我的问题无关。正如@Hans Passant 在他的评论中指出的那样,这可能是另外一回事。

   107: bool ConvertToBoolean(const std::string& s)
   108: {
000000013FE654F0  mov         qword ptr [rsp+8],rcx  
000000013FE654F5  sub         rsp,38h  
000000013FE654F9  mov         qword ptr [rsp+20h],0FFFFFFFFFFFFFFFEh  
   109:   try
   110:   {
   111:     return boost::lexical_cast<bool>(s);
000000013FE65502  call        boost::detail::lexical_cast_do_cast<bool,std::basic_string<char,std::char_traits<char>,std::allocator<char> > >::lexical_cast_impl (013FD1A0D3h)  
000000013FE65507  jmp         ConvertToBoolean+1Fh (013FE6550Fh)  
   112:   }
   113:   catch (...)
   114:   {
   115:     if (boost::iequals("true", s.c_str()))
   116:     {
   117:       return true;
000000013FE65509  mov         al,1  
000000013FE6550B  jmp         ConvertToBoolean+1Fh (013FE6550Fh)  
   118:     }
   119:   }
   120:   return false;
000000013FE6550D  xor         al,al  
   121: }
000000013FE6550F  add         rsp,38h  
000000013FE65513  ret  

更新:为了完整性,这是异常 block

   114:   {
   115:     if (boost::iequals("true", s.c_str()))
00007FF744D9F19B  mov         rcx,qword ptr [s]  
00007FF744D9F19F  call        std::basic_string<char,std::char_traits<char>,std::allocator<char> >::c_str (07FF74425E8B3h)  
00007FF744D9F1A4  mov         qword ptr [rbp+30h],rax  
00007FF744D9F1A8  lea         rcx,[rbp+28h]  
00007FF744D9F1AC  call        std::locale::locale (07FF744252991h)  
00007FF744D9F1B1  mov         qword ptr [rbp+48h],rax  
00007FF744D9F1B5  mov         rax,qword ptr [rbp+48h]  
00007FF744D9F1B9  mov         qword ptr [rbp+50h],rax  
00007FF744D9F1BD  mov         r8,qword ptr [rbp+50h]  
00007FF744D9F1C1  lea         rdx,[rbp+30h]  
00007FF744D9F1C5  lea         rcx,[CNTServiceCommandLineInfo::`vftable'+11170h (07FF744FBF778h)]  
00007FF744D9F1CC  call        boost::algorithm::iequals<char const [5],char const * __ptr64> (07FF744251596h)  
00007FF744D9F1D1  mov         byte ptr [rbp+20h],al  
00007FF744D9F1D4  lea         rcx,[rbp+28h]  
00007FF744D9F1D8  call        std::locale::~locale (07FF74425D1C0h)  
00007FF744D9F1DD  movzx       eax,byte ptr [rbp+20h]  
00007FF744D9F1E1  test        eax,eax  
00007FF744D9F1E3  je          __catch$?ConvertToBoolean@@YA_NAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z$0+57h (07FF744D9F1F2h)  
   116:     {
   117:       return true;
00007FF744D9F1E5  mov         byte ptr [rbp+38h],1  
00007FF744D9F1E9  lea         rax,[ConvertToBoolean+37h (07FF7444C8FD7h)]  
00007FF744D9F1F0  jmp         __catch$?ConvertToBoolean@@YA_NAEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z$0+5Eh (07FF744D9F1F9h)  
   118:     }
   119:   }
00007FF744D9F1F2  lea         rax,[ConvertToBoolean+35h (07FF7444C8FD5h)]  
00007FF744D9F1F9  add         rsp,28h  
00007FF744D9F1FD  pop         rdi  
00007FF744D9F1FE  pop         rbp  
00007FF744D9F1FF  ret  

最佳答案

在代码中进行长时间搜索后,我发现缓冲区溢出不少于 80 字节。

STARTUPINFO startup_info;
PROCESS_INFORMATION process_information;
ZeroMemory(&startup_info, sizeof(startup_info));
ZeroMemory(&process_information, sizeof(startup_info)); <-- wrong size!

这段代码在测试期间从未在我的机器上执行过,因此为什么原始问题中的代码在这些测试中运行良好。覆盖的堆栈显然是灾难性的,可能会导致许多奇怪的行为。

关于c++ - 尽管 try-catch 异常仍泄漏到系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40667023/

相关文章:

c++ - 是否有一个单行程序可以将元组/对解压缩到引用中?

c++ - 正整数的组合 uniq(顺序不重要)

c++ - 如何卸载托管的c++ dll?

c++ - C3321 : an initializer list is unexpected in this context

c++ - 具有两个循环的OpenGL,一个循环不起作用

c++ - 为什么 sizeof... 不能使用这个别名模板?

c++ - C++中如何访问基类的成员变量?

c++ - 在 DLL 边界上使用 std::move 是一种好习惯吗?

java - Boost Asio io_service,从客户端读取消息

c++ - 当算术运算溢出时,它们会产生确定性数字吗?