From 3a57378f15d8bbea48b9be13c09bacb6180843e7 Mon Sep 17 00:00:00 2001 From: N <71219152+PokeCodec@users.noreply.github.com> Date: Tue, 10 Nov 2020 21:20:58 -0500 Subject: [PATCH] Fix potential over/underflow as noted by "TODO:" comment (#8081) Fixed potential errors caused by overflow or underfow in SectionInput.cpp ## PR Checklist * [x] CLA signed * [x] Tests added/passed ## Detailed Description of the Pull Request / Additional comments In selectionInput.cpp, there is both a potential overflow and potential underflow. To address this issue, I casted the calculation up to int, which is then checked because of integer promotion. Underflow and underflow is therefore impossible because now if the calculation exceeds SHORT_MAX, it will have exceeded bufferSize.BottomInclusive() or bufferSize.Top() anyway, and be set to them. ## Validation Steps Performed Passed Unit Testing Manual Validation --- src/host/selectionInput.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/host/selectionInput.cpp b/src/host/selectionInput.cpp index 4ec415dd516..f551b346c10 100644 --- a/src/host/selectionInput.cpp +++ b/src/host/selectionInput.cpp @@ -390,7 +390,7 @@ bool Selection::HandleKeyboardLineSelectionEvent(const INPUT_KEY_INFO* const pIn // shift + pgup/pgdn extends selection up or down one full screen case VK_NEXT: { - coordSelPoint.Y += sWindowHeight; // TODO: potential overflow + coordSelPoint.Y = base::CheckAdd(coordSelPoint.Y, sWindowHeight).ValueOrDefault(bufferSize.BottomInclusive()); if (coordSelPoint.Y > bufferSize.BottomInclusive()) { coordSelPoint.Y = bufferSize.BottomInclusive(); @@ -399,7 +399,7 @@ bool Selection::HandleKeyboardLineSelectionEvent(const INPUT_KEY_INFO* const pIn } case VK_PRIOR: { - coordSelPoint.Y -= sWindowHeight; // TODO: potential underflow + coordSelPoint.Y = base::CheckSub(coordSelPoint.Y, sWindowHeight).ValueOrDefault(bufferSize.Top()); if (coordSelPoint.Y < bufferSize.Top()) { coordSelPoint.Y = bufferSize.Top();