-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
possible infinite loop in sys::windows::fill_utf16_buf #73212
Comments
Here's one of the docs on how filling a buffer usually works in Win32:
Note the difference in lengths between success and failure. So there's two potential cases where
So Windows does indeed guarantee this can't happen. |
OTOH GetFinalPathNameByHandleA on some versions returns the size written including the NUL terminator, so for that function the infinite loop could happen, right? The doubling logic that checks for the insufficient buffer error code must be there for some other function that I have not yet found, it would be great to have a comment as to where this behavior is needed. |
As far as I know Rust only ever uses the Unicode version of functions (they end in I'm not at all against adding a comment (and returning an error?) in this case but it's not something that should ever happen with the kinds of APIs Rust uses. |
Come to think of it |
Still, it would be more robust to change the |
My issue with assuming success is that we can't know what a hypothetical Win32 function is doing in this case. For example, it may have truncated the output or it may even be returning an error code, not a size. We can only guess. Best case is we guess right and it works. Worse case is we guess wrong and it causes a bug that only becomes apparent later on, making the source of the bug hard to track down. Having given more thought to this, I'm leaning towards using It might also be good to clearly document the expected return for the
|
Yes, that sounds like a good improvement! Since this Win32 behavior is not generically documented, it might also be good to note the exact syscalls that this function has been tested with. I should probably mention that I don’t have a Windows system to develop or test this on, I was merely perusing the code out of curiosity — otherwise I’d volunteer a PR. |
Use `unreachable!` for an unreachable code path Closes rust-lang#73212
Looking at the code without prior knowledge of Windows system call semantics, I see an infinite loop if the syscall were to fill up the buffer exactly: when k == n on line 195, the loop will be rerun with no changes, which should result in the exact same outcome if the syscall is idempotent.
Even if Windows guarantees in some way that this cannot happen, would it not be clearer to handle this case in an obvious fashion?
The text was updated successfully, but these errors were encountered: