Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading invalid data from X: The readable size is Y bytes, but Z bytes may be read

I updated my Visual Studio 2019 to version 16.1.3 and I'm now getting a warning inside a GDI+ library based function to get the encoder:

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 yields the following warning:

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

The code works but how can this warning be fixed? I debugged the code step-by-step but I do not see an issue with the indexing or the allocated size of p_image_codec_info. The documentation for the warning is here.

like image 270
BullyWiiPlaza Avatar asked Oct 27 '25 09:10

BullyWiiPlaza


1 Answers

As commented by Evg, this seems to be a Visual Studio/Resharper C++ static analyzer bug since the code is correct.

like image 105
BullyWiiPlaza Avatar answered Oct 29 '25 23:10

BullyWiiPlaza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!