From 92d1d7fbde9d2ad74ca3014c7e894060fbffbb34 Mon Sep 17 00:00:00 2001 From: Mitchell Richters Date: Thu, 15 Apr 2021 08:25:08 +1000 Subject: [PATCH] - Fix potential overflow issue in `binangle::tosigned()` and `getincanglebam()`. --- source/core/binaryangle.h | 18 +++--------------- source/core/gameinput.cpp | 10 +++++----- 2 files changed, 8 insertions(+), 20 deletions(-) diff --git a/source/core/binaryangle.h b/source/core/binaryangle.h index dbb0fce8f..b9d75593e 100644 --- a/source/core/binaryangle.h +++ b/source/core/binaryangle.h @@ -95,18 +95,6 @@ inline double bcosf(const double ang, const int8_t shift = 0) } -//--------------------------------------------------------------------------- -// -// Shift a Build angle left by 21 bits. -// -//--------------------------------------------------------------------------- - -inline constexpr int64_t BAngToBAM(int ang) -{ - return ang << BAMBITS; -} - - //--------------------------------------------------------------------------- // // @@ -132,14 +120,14 @@ public: binangle() = default; binangle(const binangle &other) = default; // This class intentionally makes no allowances for implicit type conversions because those would render it ineffective. - constexpr int32_t tosigned() const { return value > BAngToBAM(1024) ? int64_t(value) - BAngToBAM(2048) : value; } - constexpr short asbuild() const { return value >> 21; } + constexpr int32_t tosigned() const { return value > INT32_MAX ? int64_t(value) - UINT32_MAX : value; } + constexpr short asbuild() const { return value >> BAMBITS; } constexpr double asbuildf() const { return value * (1. / BAMUNIT); } constexpr fixed_t asq16() const { return value >> 5; } constexpr uint32_t asbam() const { return value; } constexpr double asrad() const { return value * (pi::pi() / 0x80000000u); } constexpr double asdeg() const { return AngleToFloat(value); } - constexpr short signedbuild() const { return tosigned() >> 21; } + constexpr short signedbuild() const { return tosigned() >> BAMBITS; } constexpr double signedbuildf() const { return tosigned() * (1. / BAMUNIT); } constexpr fixed_t signedq16() const { return tosigned() >> 5; } constexpr int32_t signedbam() const { return tosigned(); } diff --git a/source/core/gameinput.cpp b/source/core/gameinput.cpp index 0c31a9c23..fcab71144 100644 --- a/source/core/gameinput.cpp +++ b/source/core/gameinput.cpp @@ -80,13 +80,13 @@ fixed_t getincangleq16(fixed_t a, fixed_t na) binangle getincanglebam(binangle a, binangle na) { - int64_t cura = a.asbam() & 0xFFFFFFFF; - int64_t newa = na.asbam() & 0xFFFFFFFF; + int64_t cura = a.asbam(); + int64_t newa = na.asbam(); - if(abs(cura-newa) >= BAngToBAM(1024)) + if(abs(cura-newa) > INT32_MAX) { - if(newa > BAngToBAM(1024)) newa -= BAngToBAM(2048); - if(cura > BAngToBAM(1024)) cura -= BAngToBAM(2048); + if(newa > INT32_MAX) newa -= UINT32_MAX; + if(cura > INT32_MAX) cura -= UINT32_MAX; } return bamang(newa-cura);