winapi-rs icon indicating copy to clipboard operation
winapi-rs copied to clipboard

c_void has incorrect layout in no_std

Open calebzulawski opened this issue 5 years ago • 4 comments

This will be fixed with #771, but still reporting this just in case. In no_std, c_void has a size of 0 instead of 1.

calebzulawski avatar Oct 30 '20 16:10 calebzulawski

Does the layout matter?

retep998 avatar Nov 01 '20 15:11 retep998

If you use add() or similar on a pointer to c_void it does nothing, since the size is zero, rather than adding that many bytes.

calebzulawski avatar Nov 01 '20 15:11 calebzulawski

That makes winapi's own c_void different from C's void as in C adding to the void* also adds that number of bytes to the pointer.

_Bool test(void* p) {
    return p + 5 == (char*)p + 5;
}
test:                                   # @test
        mov     al, 1
        ret

MSxDOS avatar Nov 01 '20 15:11 MSxDOS

Just ran into a bug in my code where I wanted to offset some allocated memory (VirtualAllocEx returns LPVOID which is *mut winapi::ctypes::c_void), where add(offset) was a no-op because winapi's c_void is a ZST.

Here is how std (correctly) handles c_void.

LunarLambda avatar Jan 09 '21 14:01 LunarLambda