Skip to content

Heap corruption opening some ktx2 files. #643

@bluescan

Description

@bluescan

The ktx2 files causing the issue were generated from the latest NVidia Texture Tools Generator and can be found here:
https://github.com/bluescan/tacent/tree/master/UnitTests/TestData/Images/KTX2

Specifically R8_A.ktx2 and B8G8R8_RGB.ktx2 (they are both 2D, with mipmaps, and supercompression). I'm not sure how 'well-formed' the files are as NVTT is closed source, but I figure LibKTX shouldn't crash in any case. The other test files in that folder seem to work fine. They were all generated using NV texture-tools exporter (standalone), just with different pixel formats.

As of LibKTX v4.1.0-rc3-8-g691e9ca3-dirty (quite recent), using the KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT flag causes heap corruption (noticed by VS 2022) when calling ktxTexture_Destroy.

HEAP CORRUPTION DETECTED: after Normal block (#179) at 0x0000019C24BCA070.
CRT detected that the application wrote to memory after end of heap buffer.

Some minimal code to repo:

    ktxTexture* tex = nullptr;
    ktx_error_code_e result = ktxTexture_CreateFromNamedFile("R8_A.ktx2", KTX_TEXTURE_CREATE_LOAD_IMAGE_DATA_BIT, &tex);
    if ((result == KTX_SUCCESS) && tex)
        ktxTexture_Destroy(tex);   //<< Detected here

I just put this that code at the top of toktxApp::main

The corruption seems to be with the pixel data:

    if (This->kvData != NULL)
        free(This->kvData);
    if (This->pData != NULL)
        free(This->pData);      ///<<<HERE
    free(This->_protected);
}

Thx

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions