Fix undefined behaviour when left-shifting negative values by casting to unsigned
This silences warning spam from UndefinedBehaviorSanitizer.
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
#define OVERFLOWSAFE_TYPE_HPP
|
#define OVERFLOWSAFE_TYPE_HPP
|
||||||
|
|
||||||
#include "math_func.hpp"
|
#include "math_func.hpp"
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overflow safe template for integers, i.e. integers that will never overflow
|
* Overflow safe template for integers, i.e. integers that will never overflow
|
||||||
@@ -28,6 +29,7 @@ class OverflowSafeInt
|
|||||||
private:
|
private:
|
||||||
/** The non-overflow safe backend to store the value in. */
|
/** The non-overflow safe backend to store the value in. */
|
||||||
T m_value;
|
T m_value;
|
||||||
|
typedef typename std::make_unsigned<T>::type T_unsigned;
|
||||||
public:
|
public:
|
||||||
OverflowSafeInt() : m_value(0) { }
|
OverflowSafeInt() : m_value(0) { }
|
||||||
|
|
||||||
@@ -103,7 +105,7 @@ public:
|
|||||||
inline OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
|
inline OverflowSafeInt operator % (const int divisor) const { OverflowSafeInt result = *this; result %= divisor; return result; }
|
||||||
|
|
||||||
/* Operators for shifting */
|
/* Operators for shifting */
|
||||||
inline OverflowSafeInt& operator <<= (const int shift) { this->m_value <<= shift; return *this; }
|
inline OverflowSafeInt& operator <<= (const int shift) { this->m_value = ((T_unsigned) this->m_value) << shift; return *this; }
|
||||||
inline OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
|
inline OverflowSafeInt operator << (const int shift) const { OverflowSafeInt result = *this; result <<= shift; return result; }
|
||||||
inline OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; }
|
inline OverflowSafeInt& operator >>= (const int shift) { this->m_value >>= shift; return *this; }
|
||||||
inline OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
|
inline OverflowSafeInt operator >> (const int shift) const { OverflowSafeInt result = *this; result >>= shift; return result; }
|
||||||
|
@@ -232,7 +232,7 @@ static inline uint TileY(TileIndex tile)
|
|||||||
*/
|
*/
|
||||||
static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
|
static inline TileIndexDiff ToTileIndexDiff(TileIndexDiffC tidc)
|
||||||
{
|
{
|
||||||
return (tidc.y << MapLogX()) + tidc.x;
|
return (((uint) tidc.y) << MapLogX()) + tidc.x;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -166,7 +166,7 @@ static inline uint GetTreeCount(TileIndex t)
|
|||||||
static inline void AddTreeCount(TileIndex t, int c)
|
static inline void AddTreeCount(TileIndex t, int c)
|
||||||
{
|
{
|
||||||
assert(IsTileType(t, MP_TREES)); // XXX incomplete
|
assert(IsTileType(t, MP_TREES)); // XXX incomplete
|
||||||
_m[t].m5 += c << 6;
|
_m[t].m5 += ((uint) c) << 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -24,7 +24,7 @@
|
|||||||
static inline int ScaleByZoom(int value, ZoomLevel zoom)
|
static inline int ScaleByZoom(int value, ZoomLevel zoom)
|
||||||
{
|
{
|
||||||
assert(zoom >= 0);
|
assert(zoom >= 0);
|
||||||
return value << zoom;
|
return ((uint) value) << zoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,7 +49,7 @@ static inline int UnScaleByZoom(int value, ZoomLevel zoom)
|
|||||||
static inline int ScaleByZoomLower(int value, ZoomLevel zoom)
|
static inline int ScaleByZoomLower(int value, ZoomLevel zoom)
|
||||||
{
|
{
|
||||||
assert(zoom >= 0);
|
assert(zoom >= 0);
|
||||||
return value << zoom;
|
return ((uint) value) << zoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user