September 19th, 2016

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

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.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.