运行时检查失败#2 - 变量'length'周围的堆栈已损坏 [英] Run-Time Check Failure #2 - Stack around the variable 'length' was corrupted

查看:108
本文介绍了运行时检查失败#2 - 变量'length'周围的堆栈已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力改变我们的应用程序以支持unicode特性。因此,在进行更改时,我必须将所有char转换为wchar_t,并且我们有一些包装器流类,我转换为支持wchar_t。现在我的问题是在对以下代码进行更改时遇到运行时检查失败#2 - 变量'length'周围的堆栈已损坏。虽然如果我继续我得到正确的价值观。请让我知道如何摆脱这个错误。



I am working on changing our application to support unicode characteristics . Hence while doing the changes i had to convert all char to wchar_t and we had some wrapper stream classes which i converted to support wchar_t . Now my issue is while doing the change to the below code i encounter Run-Time Check Failure #2 - Stack around the variable 'length' was corrupted. though if i continue i get the proper values . Please let me know how to get rid of this error.

FDIStream&  
FDIStream::
operator>>(std::wstring& data)
{
    if (CanReadData())
    {
        int length = -1;
        *this >> length;

        if (length >= 0)
        {
            // See if length is a valid value (not pass eof)
            if (length > GetLength()) {
                throw FDException("Corrupted file");
            }

            wchar_t* buffer = new wchar_t[length];

            try
            {
                ReadBytes(buffer, length);

                data = std::wstring(buffer,length); 
            } catch (...)
            {
                delete[] buffer;
                throw;
            }

            delete[] buffer;
        }
    }

    return *this;
}

推荐答案

Unicode字符宽2个字节。

如果长度保持计数然后你可以读取一个较短的字符串并错过结尾的null。因此,字符串上的其他操作可以突破分配的空间来破坏堆栈。在这种情况下更改:

Unicode chars are 2 bytes wide.
if length holds the count in bytes then you may read a shorter string and miss the ending null. Because of this other operations on string can runover the allocated space corrupting the stack. In this case change:
ReadBytes(buffer, length);



to


to

ReadBytes(buffer, length*sizeof(wchar_t));



如果长度保持字符数,可能会发生同样的问题,因为字符串长度和字节数不再重合。

In这种情况可能你想要改变:


If length hold the count of chars the same problem could happen because the string length and the number of bytes are no more coincident.
In this case maybe you want change:

data = std::wstring(buffer,length);



使用:


With:

data = std::wstring(buffer,length/sizeof(wchar_t));


我只传递字节值...这是4 ....而我正在将数据复制到缓冲区...缓冲区有没有嵌入代码它应该是它中的字符串和长度13 ....并且当我检查数据的值时复制...我看到它有15个大小并且它写入了没有嵌入核心....请让我知道什么是真的这个代码错了...



我怀疑
I am passing byte value only ... which is 4 .... and while i am copying data to buffer ... buffer has "NO Embed Code" which should be the string in it and length 13 .... and after copying when i check the value of data ... i see it has 15 as size and it has "NO Embed Core" written into it .... Please let me know what actually is wrong with that code ...

I have doubt about that
data = FDCoreUtils::ConvertToFromBigEndianFormat(static_cast(data));



函数因为它有一些字节操作它...



内联字符

FDLoByte(短值)

{

返回static_cast< char>(价值& 0x000000FF);

}



inline int

FDHiWord(int value)

{

return(value>> 16)& 0x0000FFFF;

}



inline int

FDLoWord(int value)

{

返回值& 0x0000FFFF;

}



我是unicode和字符串转换的新手,请让我知道我应该寻找什么以及我应该在哪里看解决此问题。


function as that has some byte manipluations it ...

inline char
FDLoByte(short value)
{
return static_cast<char>(value & 0x000000FF);
}

inline int
FDHiWord(int value)
{
return (value >> 16) & 0x0000FFFF;
}

inline int
FDLoWord(int value)
{
return value & 0x0000FFFF;
}

I am very new to unicode and string conversions please let me know what i should look for and where i should look for to resolve this issue .


这篇关于运行时检查失败#2 - 变量'length'周围的堆栈已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆