views:

211

answers:

4

The MSDN states in its description of ReadFile() function:

If hFile is opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must point to a valid and unique OVERLAPPED structure, otherwise the function can incorrectly report that the read operation is complete.

I have some applications that are violating the above recommendation and I would like to know the severity of the problem. I mean the program uses named pipe that has been created with FILE_FLAG_OVERLAPPED, but it reads from it using the following call:

ReadFile(handle, &buf, n, &n_read, NULL);

That means it passes NULL as the lpOverlapped parameter. That call should not work correctly in some circumstances according to documentation. I have spent a lot of time trying to reproduce the problem, but I was unable to! I always got all data in right place at right time. I was testing only Named Pipes though.

Would anybody know when can I expect that ReadFile() will incorrectly return and report successful completion even the data are not yet in the buffer? What would have to happen in order to reproduce the problem? Does it happen with files, pipes, sockets, consoles, or other devices? Do I have to use particular version of OS? Or particular version of reading (like register the handle to I/O completion port)? Or particular synchronization of reading and writing processes/threads?

Or when would that fail? It works for me :/

Please help!

With regards, Martin

A: 

“If hFile is opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must point to a valid and unique OVERLAPPED structure, otherwise the function can incorrectly report that the read operation is complete.”

William H. Gates
Let me remind you that, for example, any application that reads from stdin using C-runtime has this possible problem if somebody decides to redirect its stdio handles from pipes created with OVERLAPPED flag. How can I fix the application then if I don't have absolutely any control over starting of my programs in third party environment? Then at least a mechanism for detecting presence of OVERLAPPED flag in handle is needed. But it does not exist! Or does it? Please see my other questions on this forum (http://stackoverflow.com/users/296846/martin-dobsik).
Martin Dobšík
A: 

Seems like you are in a situation where you are deliberately calling an API in contravention of the documented best practices. In such situations all bets are off. It may work, it may not. If may work on this OS, but not on the next iteration of the OS, or the next service pack of the same OS. What happens when you port to Win64? Will it still work then?

Does calling GetLastError() (or looking at @ERR,hr in the debugger) give any value that is useful in addition to the error code?

I recommend that you call it with a valid OVERLAPPED structure, get it working and remove all doubt (and possibility of random failure). Why have possibly buggy code (and very hard to reproduce bugs) in your software when you can fix the problem easily by using a valid OVERLAPPED structure?

Stephen Kellett
That is precisely what I am thinking. The problem is that it works even it deliberately breaks the best practices. Therefore, nobody noticed it until now. We have already fixed that, but old version of the software may be in use by some of our customers. That is why I am looking for the severity of the issue. Have you ever seen this reproduced?
Martin Dobšík
A: 

Why ask the question rather than fix the code to call the API as it was intended?

I suspect it always appears to work because, even though this is an asynchronous I/O, it completes very quickly. Depending on how you're testing for success, it's possible the function is incorrectly reporting that the operation completed, but it actually completes before you test the results.

The real test would be to do a read on the pipe before there's data to be read.

But really, you should just fix the code. If your architecture cannot handle asynchronous I/O, then remove the FILE_FLAG_OVERLAPPED from the creation of the named pipe.

Adrian McCarthy
Let me remind you that, for example, any application that reads from stdin using C-runtime has this possible problem if somebody decides to redirect its stdio handles from pipes created with OVERLAPPED flag. How can I fix the application then if I don't have absolutely any control over starting of my programs in third party environment? Then at least a mechanism for detecting presence of OVERLAPPED flag in handle is needed. But it does not exist! Or does it? Please see my other questions on this forum (http://stackoverflow.com/users/296846/martin-dobsik).
Martin Dobšík
If you cannot know whether the `HANDLE` was created with `FILE_FLAG_OVERLAPPED`, then perhaps you should _always_ provide an `OVERLAPPED` structure. Also note that `ReadFileEx` doesn't list quite the same limitation as `ReadFile` with respect to the `lpOverlapped` parameter. Perhaps you can use that and `GetLastError` to be sure your I/O has completed.
Adrian McCarthy