Fix some undefined behaviour: signed overflow and over shift left.

Caught by UndefinedBehaviourSanitizer.
pull/6/merge
Jonathan G Rennison 9 years ago
parent 8e3d9a520c
commit ecf5943954

@ -58,7 +58,7 @@ int GreatestCommonDivisor(int a, int b)
*/
int DivideApprox(int a, int b)
{
int random_like = ((a + b) * (a - b)) % b;
int random_like = (((int64) (a + b)) * ((int64) (a - b))) % b;
int remainder = a % b;

@ -8322,7 +8322,7 @@ static void CalculateRefitMasks()
}
/* Clear invalid cargoslots (from default vehicles or pre-NewCargo GRFs) */
if (!HasBit(_cargo_mask, ei->cargo_type)) ei->cargo_type = CT_INVALID;
if (ei->cargo_type != CT_INVALID && !HasBit(_cargo_mask, ei->cargo_type)) ei->cargo_type = CT_INVALID;
/* Ensure that the vehicle is either not refittable, or that the default cargo is one of the refittable cargoes.
* Note: Vehicles refittable to no cargo are handle differently to vehicle refittable to a single cargo. The latter might have subtypes. */

@ -61,7 +61,7 @@ static inline bool IsValidTrackdirForRoadVehicle(Trackdir trackdir)
*/
static inline bool IsValidTrackdir(Trackdir trackdir)
{
return (1 << trackdir & TRACKDIR_BIT_MASK) != 0;
return trackdir < TRACKDIR_END && ((1 << trackdir & TRACKDIR_BIT_MASK) != 0);
}
/**

Loading…
Cancel
Save