Address possible overflow, adhere to style

The multiplication by 99 can overflow an int8_t depending on whether the
expression is coerced to a regular-sized int (based on the 99 literal).
Just to be safe, perform a cast to int16_t in case the coercion does not
happen.
pull/365/head
Jack Zhou 7 years ago
parent 04112de297
commit 77acd347e8
No known key found for this signature in database
GPG Key ID: C2E29FDA9C9261E1

@ -119,12 +119,13 @@ void MouseWrapper_::move(int8_t x, int8_t y) {
static int8_t remainderY = 0; static int8_t remainderY = 0;
if (x != 0 && y != 0) { if (x != 0 && y != 0) {
// 99 / 140 closely approximates sqrt(2) / 2. // 99 / 140 closely approximates sqrt(2) / 2. Since integer division
int8_t adjusted_x = x * 99 / 140; // truncates towards zero we do not need to worry about truncation errors.
int8_t adjusted_y = y * 99 / 140; int8_t adjustedX = (int16_t)x * 99 / 140;
int8_t adjustedY = (int16_t)y * 99 / 140;
x = (adjusted_x == 0 ? x : adjusted_x); if (adjustedX != 0) x = adjustedX;
y = (adjusted_y == 0 ? y : adjusted_y); if (adjustedY != 0) y = adjustedY;
} }
if (x != 0) { if (x != 0) {

Loading…
Cancel
Save