从X读取无效数据:可读大小为Y字节,但可以读取Z字节

时间:2019-06-14 11:04:18

标签: c++ windows visual-studio gdi+

我将Visual Studio 2019更新为16.1.3版本,现在在基于GDI+库的函数中获取编码器警告:

inline int get_encoder(const WCHAR* format, CLSID* p_clsid)
{
    UINT image_encoders_count = 0;
    UINT image_encoder_array_size = 0;

    GetImageEncodersSize(&image_encoders_count, &image_encoder_array_size);
    if (image_encoder_array_size == 0)
    {
        return -1; // Failure
    }

    const auto p_image_codec_info = static_cast<ImageCodecInfo*>(malloc(image_encoder_array_size));
    if (p_image_codec_info == nullptr)
    {
        return -1; // Failure
    }

    GetImageEncoders(image_encoders_count, image_encoder_array_size, p_image_codec_info);

    for (UINT image_encoder_index = 0; image_encoder_index < image_encoders_count; image_encoder_index++)
    {
        // TODO: Fix the warning "Reading invalid data from 'p_image_codec_info':  the readable size is 'image_encoder_array_size' bytes, but '208' bytes may be read."
        const auto image_codec_info = p_image_codec_info[image_encoder_index];
        const auto mime_type = image_codec_info.MimeType;
        const auto comparison_result = wcscmp(mime_type, format);
        if (comparison_result == 0)
        {
            *p_clsid = image_codec_info.Clsid;
            free(p_image_codec_info);
            return image_encoder_index; // Success
        }
    }

    free(p_image_codec_info);
    return -1; // Failure
}

Visual Studio产生以下警告:

Reading invalid data from 'p_image_codec_info':  the readable size is 'image_encoder_array_size' bytes, but '208' bytes may be read.

该代码有效,但是如何解决此警告?我已逐步调试了代​​码,但没有看到索引或p_image_codec_info的分配大小问题。警告的文档为here

1 个答案:

答案 0 :(得分:0)

Evg评论说,这似乎是Visual Studio / Resharper C++静态分析器错误,因为代码正确。