无效的读取大小为1,新的操作符

Invalid read of size 1, new operator

本文关键字:操作符 小为 读取 无效      更新时间:2023-10-16

读取特殊格式的二进制文件。我必须使用动态数组读取字符串,其中有一个未知的大小。所有工作正常,但有错误的valgrind。的赋值没有问题,我试过没有它。我不知道还有什么地方出错了。

int ReadInt(ifstream& i)
{
    int x=0;
    i.read((char*)&x,4);
    return x;
}
bool BINtoCSV ( const char * inFileName, const char * outFileName )
{
    ifstream i(inFileName,ios::binary|ios::in);
    if(i.fail()) return false;
    ofstream o(outFileName,ios::binary|ios::out);
    if(o.fail()) return false;
    char eater[4];
    for(unsigned f=0;f<4;f++)eater[f]='';
    int rows=0,inLine=0;
    char c='k';
    i.read(eater,1);//H
    i.read(eater,4);//num
    i.read((char*)&rows,4);//rows
    i.read((char*)&inLine,4);//inlines

    for(int a=0;a<rows;a++){
        i.read((char*)&c,1);
        if(c!='R') {if(a==0){i.close(); o.close(); return true;}i.close(); o.close();
            return false;}
        i.read(eater,4);
        for(int b=0;b<inLine;b++)
        {
            for(unsigned f=0;f<4;f++)eater[f]='';
            i.read((char*)&c,1);
            if(c=='I') { o<<ReadInt(i)<<(!((b+1)%inLine)?'n':';');}
            else if(c=='S')
            {
                int l=0; i.read((char*)&l,4);
                char* block=new char[l];
                for(int a=0;a<l;a++) block[a]='';
                i.read(block,l);
                o<<block<<(!((b+1)%inLine)?'n':';'); 
                delete [] block;
            }
            else 
            {
                i.close(); 
                o.close(); 
                return false;
            }
        }
    }
    i.close();
    o.close();
    return true;
}

有一个来自valgrind的日志示例

Invalid read of size 1
at 0x4C2BFB4: strlen (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x4EC62E0: 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/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
by 0x401841: BINtoCSV(char const*, char const*) (in /home/ondrnovy/Plocha/a.out)
by 0x401EA7: main (in /home/ondrnovy/Plocha/a.out)
Address 0x5a07683 is 0 bytes after a block of size 3 alloc'd
at 0x4C2AC27: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-
amd64-linux.so)
by 0x40179F: BINtoCSV(char const*, char const*) (in /home/ondrnovy/Plocha/a.out) 
by 0x401EA7: main (in /home/ondrnovy/Plocha/a.out)

在这部分

char* block=new char[l];
for(int a=0;a<l;a++) block[a]='';
i.read(block,l);
o<<block<<(!((b+1)%inLine)?'n':';'); 

您尝试使用期望C风格字符串的操作符<<block,但block不是正确的零终止。

操作符将使用strlen来查找字符串的结尾,但是没有结尾,并且它读取buffer之外的内容。

我不熟悉amd64的GNU标准库实现,但我在基于arm的平台上从Valgrind看到过类似的警告。他们在那里使用的strlen实现被优化为每次迭代处理一个字(4字节)(这是有问题的代码)。

在C语言中与之等价的可能是这样的:

uint32_t dat, *p;
uint32_t temp;
int len = 0;
p = (uint32_t*)inputstring;
dat = *p++;
len -= (int)p;
// The only case where ((x-1) & 0x80) & ~x will be non-zero is
// if x == 0. So loop as long as the result is zero, i.e. no
// word has been loaded that contains a NUL-byte.
do {
    temp = dat - 0x01010101;
    temp &= 0x80808080;
    temp &= ~dat;
    if (!temp) dat = *p++;
} while (!temp);
len += (int)p;
if (dat & 0xFF) {
    len++;
    if (dat & 0xFF00) {
        len++;
        if (dat & 0xFF0000) {
            len++;
        }
    }
}
return len;

我只能猜测,在这些平台上使用的new操作符实现将char数组填充为4字节的倍数,以使此优化安全。但是当Valgrind检查越界问题时,它可能不会关心这个。