diff --git a/src/core/math_func.cpp b/src/core/math_func.cpp index d927702083..749e018d80 100644 --- a/src/core/math_func.cpp +++ b/src/core/math_func.cpp @@ -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; diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 20c88e4a76..43ae02378f 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -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. */ diff --git a/src/track_func.h b/src/track_func.h index 6896792374..448665b7c6 100644 --- a/src/track_func.h +++ b/src/track_func.h @@ -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); } /**