This is what a Win32 app's source code looks like:
https://www.paulgriffiths.net/program/c/srcs/winhellosrc.htm...
https://www.paulgriffiths.net/program/c/srcs/winhellosrc.htm...
wndclass.cbSize = sizeof(wndclass);
why does a struct need to be told its own size?Inside the implementation of the API they might say:
if (wndClass->cbSize == SIZE_IN_PREVIOUS_SDK_VERSION)
{
// Code to handle legacy structure goes here
}
If they add to the struct and don't make accesses to the new members conditional on cbSize's value, then it's a bad pointer dereference. The caller allocated it with the "old" size, likely on their stack, and some unrelated value might be located where the new members would be, or it could be a bad pointer.I do believe there's a good reason; I just can't figure out what it is.
edit for any curious readers: I found an answer here https://stackoverflow.com/questions/30416191/why-do-some-win...