- Fix potential overflow issue in binangle::tosigned() and getincanglebam().

This commit is contained in:
Mitchell Richters 2021-04-15 08:25:08 +10:00
parent 9a58299bee
commit 92d1d7fbde
2 changed files with 8 additions and 20 deletions

View file

@ -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(); }

View file

@ -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);