SHGetFileInfo causes Heap Corruption when using SHGFI_ICON

168 views Asked by At

When I enable Page Heap for my process under test, it triggers an Access Violation which indicates to me some type of heap corruption has occurred when SHGetFileInfo is called.

The top of the call stack shows msvcr90!wcspbrk and walking down it shows COM-related items in ole32 until shell32 is reached where we call SHGetFileInfo.

From what I've found online, a common problem for weirdness using shell32 is not calling CoInitialize/CoInitializeEx first, but at this point CoInitializeEx() has already been called, and calling it immediately prior to the below code simply returns S_FALSE.

The below code is in our DLL which is PInvoked from .NET (the code is used to retrieve the icon used for a particular file):

SHFILEINFOW shfi;

memset(&shfi,0,sizeof(shfi));

SHGetFileInfoW(A2W("C:\\logfile.txt"),
        FILE_ATTRIBUTE_NORMAL,
        &shfi,
        sizeof(shfi),
        SHGFI_USEFILEATTRIBUTES 
        | SHGFI_ICON
        );

(where logfile.txt is random text file on my root drive)

I've hard-coded the first parameter to a file on my machine for simplicity.

I'm using a 64-bit Windows OS, but the code is run in a 32-bit context. I get the same result if I use the narrow version of SHGetFileInfo.

If I disable Page Heap for my process, there is no problem.

When I don't use the flag SHGFI_ICON, the issue doesn't occur.

EDIT: @HansPassant requested I add a reproduceable sample, here's a link to a Visual Studio 2010 Win32 Console Application demonstrating the issue: sample

0

There are 0 answers