It’s an unfortunate choice of data type for the file system redirection cookie

Raymond Chen

Raymond

If you want to disable 64-bit file system redirection, you call the Wow64­Disable­Wow64­Fs­Redirection function. This function gives you a cookie. When you are finished, you call Wow64­Revert­Wow64­Fs­Redirection, passing the cookie you received from the previous call. Like this:

void* cookie;
if (Wow64DisableWow64FsRedirection(&cookie)) {
   ... do stuff ...
   Wow64RevertWow64FsRedirection(cookie);
}

The unfortunate thing is that the data type for the cookie is an untyped pointer: void*. This means that the following mistake goes undetected:

// Remember: Code in italics is wrong.
void* cookie;
if (Wow64DisableWow64FsRedirection(&cookie)) {
   ... do stuff ...
   Wow64RevertWow64FsRedirection(&cookie);
}

The erroneous parameter to Wow64­Revert­Wow64­Fs­Redirection goes undetected because void** is implicitly convertible to void*. Because any pointer is implicitly convertible to void*, because void* is a generic pointer.

In retrospect, the type of the cookie used by the file system redirection functions should have been something other than void*. It could have used DECLARE_HANDLE, which declares a pointer to a dummy structure with a unique name. Or it could have been a pointer to a uniquely-named incomplete type.

Raymond Chen
Raymond Chen

Follow Raymond