msvcr90.dll!_invalid_parameter_noinfo() Line 125 + 0xc bytes 的排错

// compile with: /EHsc
#define _SECURE_SCL 1
#define _SECURE_SCL_THROWS 1
#include <vector>
#include <iostream>
using namespace std;
int main() {
   vector v;
   v.push_back(67);
   int i = v[0];
   cout << i << endl;

   try {
      i = v[1];
   }
   catch (std::out_of_range) {
      cout << "invalid container access" << endl;
   }
};


这里vector访问越界,catch会抓到异常。但是如果把vector换成string:
#include <string> 
int main() { 
    string str = ""; 
    char s; 
    try { 
        s = str[1]; 
    } 
    catch (std::out_of_range) { 
        cout << "invalid container access" << endl; 
    } 
};

却什么也catch不到,Release版直接crash。通过dump定位到如题的错误。为什么呢?

看一下string::operator[]()的实现。它在Microsoft Visual Studio 9.0/VC/crt/src/xstring中:

    reference __CLR_OR_THIS_CALL operator[](size_type _Off) 
        {    // subscript mutable sequence
#if _HAS_ITERATOR_DEBUGGING 
        // skip debug checks if the container is initizialed with _IGNORE_MYITERLIST 
        if (this->_Myfirstiter != _IGNORE_MYITERLIST) 
            { 
            if (_Mysize < _Off) 
                { 
                _DEBUG_ERROR("string subscript out of range"); 
                _SCL_SECURE_OUT_OF_RANGE; 
                } 
            } 
#else 
        _SCL_SECURE_VALIDATE_RANGE(_Off <= _Mysize); 
#endif /* _HAS_ITERATOR_DEBUGGING */
        return (_Myptr()[_Off]); 
        }

_SCL_SECURE_VALIDATE_RANGE 是在/VC/include/yvals.h中定义的:


#define _SCL_SECURE_VALIDATE_RANGE(cond)                / 
    {                                                    / 
        if (!(cond))                                    / 
        {                                                / 
            _ASSERTE((#cond, 0));                        / 
            _SCL_SECURE_OUT_OF_RANGE_NO_ASSERT;            / 
        }                                                / 
        __analysis_assume(cond);                        / 
    }

只要能走到红色位置,就可以和vector一样抛出out_of_range异常,可惜却没有。经跟踪,发现是_SECURE_SCL_THROWS宏在搞鬼。

#if _SECURE_SCL_THROWS
#define _SCL_SECURE_INVALID_ARGUMENT_NO_ASSERT        _Xinvarg() 
#define _SCL_SECURE_OUT_OF_RANGE_NO_ASSERT            _Xran()
#else /* _SECURE_SCL_THROWS */
#define _SCL_SECURE_INVALID_ARGUMENT_NO_ASSERT        _SCL_SECURE_INVALID_PARAMETER("invalid argument") 
#define _SCL_SECURE_OUT_OF_RANGE_NO_ASSERT            _SCL_SECURE_INVALID_PARAMETER("out of range") 

可是我明明 #define _SECURE_SCL_THROWS 1 了啊。过一段时间有机会再查吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值