【问题标题】:ZStd. Heap corruption after decompressionZ标准。解压后堆损坏
【发布时间】:2021-04-07 18:14:03
【问题描述】:

在大多数情况下,它工作得很好。例如,我压缩 3D 网格。几乎所有型号的压缩\解压缩都很好。但是当程序尝试解压缩时,2或3个模型可能会出错。解压很好,但是释放内存时出错

    // how I read my files
    u8* compressed_v = nullptr;
    u8* compressed_i = nullptr;
    if(!meshhead.m_dataComressSize_i)
    {
        YY_PRINT_FAILED;
        return nullptr;
    }
    if(!meshhead.m_dataComressSize_v)
    {
        YY_PRINT_FAILED;
        return nullptr;
    }
    
    compressed_v = (u8*)yyMemAlloc(meshhead.m_dataComressSize_v);
    compressed_i = (u8*)yyMemAlloc(meshhead.m_dataComressSize_i);
                            
    in.read((char*)compressed_v, meshhead.m_dataComressSize_v);
    in.read((char*)compressed_i, meshhead.m_dataComressSize_i);
    u32 outsize = 0;
    new_mesh.m_data->m_vertices = yyDecompressData(compressed_v, meshhead.m_dataComressSize_v, outsize, yyCompressType::ZStd);
    new_mesh.m_data->m_indices = (u8*)yyDecompressData(compressed_i, meshhead.m_dataComressSize_i, outsize, yyCompressType::ZStd);
    
    ...
    yyMemFree(compressed_v);
    yyMemFree(compressed_i); // ERROR HERE

yyMemAllocyyMemFree 只是来自 mylib.dllmallocfree

现在是 ZStd 代码。请检查。

    u8* Engine::compressData_zstd( u8* in_data, u32 in_data_size, u32& out_data_size)
    {
        u8* out_data = (u8*)yyMemAlloc(in_data_size); // malloc
        if( !out_data )
        {
            YY_PRINT_FAILED;
            return nullptr;
        }
    
        auto compressBound = ZSTD_compressBound(in_data_size);
    
        size_t const cSize = ZSTD_compressCCtx( m_cctx, out_data, compressBound, in_data, in_data_size, 1);
        if( ZSTD_isError(cSize) )
        {
            yyMemFree(out_data); // free
            return nullptr;
        }
    
        //yyMemRealloc(out_data,(u32)cSize); // this give errors so I removed it
        out_data_size = (u32)cSize;
        return out_data;
    }
    
    u8* Engine::decompressData_zstd( u8* in_data, u32 in_data_size, u32& out_data_size)
    {
        unsigned long long const rSize = ZSTD_getFrameContentSize(in_data, in_data_size);
        u8* out_data = (u8*)yyMemAlloc((u32)rSize);
        if( !out_data )
        {
            YY_PRINT_FAILED;
            return nullptr;
        }
    
        size_t const dSize = ZSTD_decompress(out_data, (size_t)rSize, in_data, in_data_size);
        out_data_size = (u32)dSize;
        return out_data;
    }

或者也许 ZStd 代码很好,但在其他地方有问题?

yyMemAllocyyMemFree 中看起来像问题,但它只是mallocfree,我在不同的模块(.exe 和许多.dll)中到处使用它,一切正常。

【问题讨论】:

  • 你确定rSize返回一个有效值吗?

标签: compression zstd zstandard


【解决方案1】:

如果in_data 不可压缩,则表示它需要超过in_data_size

实际最坏的情况是顺便计算的,值compressBound

所以为out_data分配compressBound字节,而不是in_data_size

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-08
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多