-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
Fix out-of-bounds exceptions in Set...{Buffer,Screen}Size #8309
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great to me, but I'll only be comfortable with @miniksa's signoff 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that's what I was expecting. I briefly thought about "deduplicate those fixes into a helper" but that would be ugly and confusing and meh.
Good catch on the inclusive/exclusive thing. It looks like it was just someone (probably me) getting confused on that point again.
When resizing the buffer in the `SetConsoleScreenBufferSize` and `SetConsoleScreenBufferInfoEx` APIs, we have tests in place to make sure that the resize doesn't result in the viewport extending past the bottom or right of the buffer (since that can eventually result in exceptions being thrown). Unfortunately these tests were using the wrong X coordinate, so they failed to detect an overflow along the horizontal axis. This PR corrects that mistake. PR #8309 was where the overflow detection was initially added. The original code was using the `Viewport::EndExclusive` method to determine the extent of the viewport, mistakenly thinking that it returned the bottom right coordinates (it actually returns the left coordinate). So I've now added a `BottomRightExclusive` method to the `Viewport` class, that actually does return the coordinates we need, and have updated the overflow tests to use that method instead. ## Validation Steps Performed I've manually confirmed that the test case is issue #8453 is no longer throwing an exception. Closes #8453
This fixes a number of exceptions that can cause conhost to crash when the buffer is resized in such a way that the viewport or cursor position end up out of bounds. Technically this is a fix for issue #256, although that has been closed as "needs-repro". The main fix was to add checks in the `SetConsoleScreenBufferSizeImpl` and `SetConsoleScreenBufferInfoExImpl` methods, to make sure the viewport doesn't extend past the bottom or right of the buffer after a resize. If it has overflowed, we move the viewport back up or left until it's back within the buffer boundaries. We also check if the cursor position has ended up out of bounds, and if so, clamp it back inside the buffer. The `SCREEN_INFORMATION::SetViewport` was also a source of viewport overflow problems, because it was mistakenly using inclusive coordinates in its range checks, which resulted in them being off by one. That has now been corrected to use exclusive coordinates. Finally, the `IsCursorDoubleWidth` method was incorrectly marked as `noexcept`, which was preventing its exceptions from being caught. Ideally it shouldn't be throwing exceptions at all any more, but I've removed the `noexcept` specifier, so if it does throw an exception, it'll at least have more chance of recovering without a crash. ## Validation Steps Performed I put together a few test cases (based on the reports in issues #276 and #1976) which consistently caused conhost to crash, or to generate an exception visible in the debug output. With this PR applied, those test cases are no longer crashing or triggering exceptions. Closes #1976 (cherry picked from commit 9a07049)
When resizing the buffer in the `SetConsoleScreenBufferSize` and `SetConsoleScreenBufferInfoEx` APIs, we have tests in place to make sure that the resize doesn't result in the viewport extending past the bottom or right of the buffer (since that can eventually result in exceptions being thrown). Unfortunately these tests were using the wrong X coordinate, so they failed to detect an overflow along the horizontal axis. This PR corrects that mistake. PR #8309 was where the overflow detection was initially added. The original code was using the `Viewport::EndExclusive` method to determine the extent of the viewport, mistakenly thinking that it returned the bottom right coordinates (it actually returns the left coordinate). So I've now added a `BottomRightExclusive` method to the `Viewport` class, that actually does return the coordinates we need, and have updated the overflow tests to use that method instead. ## Validation Steps Performed I've manually confirmed that the test case is issue #8453 is no longer throwing an exception. Closes #8453 (cherry picked from commit 2a2f6b3)
🎉 Handy links: |
🎉 Handy links: |
This fixes a number of exceptions that can cause conhost to crash when
the buffer is resized in such a way that the viewport or cursor position
end up out of bounds.
References
Technically this is a fix for issue #256, although that has been closed
as "needs-repro".
PR Checklist
Detailed Description of the Pull Request / Additional comments
The main fix was to add checks in the
SetConsoleScreenBufferSizeImpl
and
SetConsoleScreenBufferInfoExImpl
methods, to make sure theviewport doesn't extend past the bottom or right of the buffer after a
resize. If it has overflowed, we move the viewport back up or left until
it's back within the buffer boundaries. We also check if the cursor
position has ended up out of bounds, and if so, clamp it back inside the
buffer.
The
SCREEN_INFORMATION::SetViewport
was also a source of viewportoverflow problems, because it was mistakenly using inclusive coordinates
in its range checks, which resulted in them being off by one. That has
now been corrected to use exclusive coordinates.
Finally, the
IsCursorDoubleWidth
method was incorrectly marked asnoexcept
, which was preventing its exceptions from being caught.Ideally it shouldn't be throwing exceptions at all any more, but I've
removed the
noexcept
specifier, so if it does throw an exception,it'll at least have more chance of recovering without a crash.
Validation Steps Performed
I put together a few test cases (based on the reports in issues #276 and
#1976) which consistently caused conhost to crash, or to generate an
exception visible in the debug output. With this PR applied, those test
cases are no longer crashing or triggering exceptions.
Closes #1976