From 8d6e57799c9b6a92dce53241bad3cb8da51549d9 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Wed, 15 Feb 2023 23:05:03 +0000 Subject: [PATCH] Fix various possible integer type conversion issues --- src/blitter/40bpp_anim.cpp | 2 +- src/linkgraph/linkgraph.h | 2 +- src/saveload/map_sl.cpp | 2 +- src/saveload/town_sl.cpp | 2 +- src/tbtr_template_vehicle_func.cpp | 2 +- src/train_cmd.cpp | 2 +- src/vehicle_cmd.cpp | 2 +- src/viewport.cpp | 8 ++++---- src/viewport_type.h | 5 +++++ 9 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/blitter/40bpp_anim.cpp b/src/blitter/40bpp_anim.cpp index 1f6ad2bb5c..97d532107b 100644 --- a/src/blitter/40bpp_anim.cpp +++ b/src/blitter/40bpp_anim.cpp @@ -45,7 +45,7 @@ void Blitter_40bppAnim::SetPixel32(void *video, int x, int y, uint8 colour, uint } else { *((Colour *)video + x + y * _screen.pitch) = colour32; - VideoDriver::GetInstance()->GetAnimBuffer()[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + y * _screen.pitch] = 0; + VideoDriver::GetInstance()->GetAnimBuffer()[((uint32 *)video - (uint32 *)_screen.dst_ptr) + x + (ssize_t)y * _screen.pitch] = 0; } } diff --git a/src/linkgraph/linkgraph.h b/src/linkgraph/linkgraph.h index 2dee34dc16..23bf6fb4e6 100644 --- a/src/linkgraph/linkgraph.h +++ b/src/linkgraph/linkgraph.h @@ -375,7 +375,7 @@ public: void RemoveEdge(NodeID from, NodeID to); inline uint64 CalculateCostEstimate() const { - uint64 size_squared = this->Size() * this->Size(); + uint64 size_squared = (uint32)this->Size() * (uint32)this->Size(); return size_squared * FindLastBit(size_squared * size_squared); // N^2 * 4log_2(N) } diff --git a/src/saveload/map_sl.cpp b/src/saveload/map_sl.cpp index bf6d33ad99..12cafaa4cd 100644 --- a/src/saveload/map_sl.cpp +++ b/src/saveload/map_sl.cpp @@ -68,7 +68,7 @@ static void Load_MAPT() static void Check_MAPH_common() { - if (_sl_maybe_chillpp && (SlGetFieldLength() == 0 || SlGetFieldLength() == _map_dim_x * _map_dim_y * 2)) { + if (_sl_maybe_chillpp && (SlGetFieldLength() == 0 || SlGetFieldLength() == (size_t)_map_dim_x * (size_t)_map_dim_y * 2)) { _sl_maybe_chillpp = false; extern void SlXvChillPPSpecialSavegameVersions(); SlXvChillPPSpecialSavegameVersions(); diff --git a/src/saveload/town_sl.cpp b/src/saveload/town_sl.cpp index aa0f25906a..66a72cf2ab 100644 --- a/src/saveload/town_sl.cpp +++ b/src/saveload/town_sl.cpp @@ -345,7 +345,7 @@ static void Load_TOWN() uint16 w = SlReadUint16(); uint16 h = SlReadUint16(); if (w != 0) { - SlSkipBytes((SlXvIsFeaturePresent(XSLFI_TOWN_CARGO_MATRIX) ? 8 : 4) * (w / 4 * h / 4)); + SlSkipBytes((SlXvIsFeaturePresent(XSLFI_TOWN_CARGO_MATRIX) ? 8 : 4) * ((uint)(w / 4) * (uint)(h / 4))); } } } diff --git a/src/tbtr_template_vehicle_func.cpp b/src/tbtr_template_vehicle_func.cpp index 3472743df8..be0a820266 100644 --- a/src/tbtr_template_vehicle_func.cpp +++ b/src/tbtr_template_vehicle_func.cpp @@ -191,7 +191,7 @@ TemplateVehicle* TemplateVehicleFromVirtualTrain(Train *virt) Train *init_virt = virt; - TemplateVehicle *tmp; + TemplateVehicle *tmp = nullptr; TemplateVehicle *prev = nullptr; for (; virt; virt = virt->Next()) { tmp = new TemplateVehicle(virt->engine_type); diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index ca97a321b4..e24c0acb65 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -7319,7 +7319,7 @@ int GetTrainRealisticAccelerationAtSpeed(const int speed, const int mass, const if (!maglev) { /* Static resistance plus rolling friction. */ resistance = 10 * mass; - resistance += mass * (15 * (512 + speed) / 512); + resistance += (int64)mass * (int64)(15 * (512 + speed) / 512); } const int area = 14; diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index 921ff22d24..2d67f84dc4 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -1348,7 +1348,7 @@ CommandCost CmdTemplateVehicleFromTrain(TileIndex tile, DoCommandFlag flags, uin bool should_execute = (flags & DC_EXEC) != 0; if (should_execute) { - TemplateVehicle *tmp; + TemplateVehicle *tmp = nullptr; TemplateVehicle *prev = nullptr; for (; clicked != nullptr; clicked = clicked->Next()) { tmp = new TemplateVehicle(clicked->engine_type); diff --git a/src/viewport.cpp b/src/viewport.cpp index a0d32b937f..28f9c1d9ea 100644 --- a/src/viewport.cpp +++ b/src/viewport.cpp @@ -3993,12 +3993,12 @@ void UpdateViewportSizeZoom(Viewport *vp) UpdateViewportDirtyBlockLeftMargin(vp); if (vp->zoom >= ZOOM_LVL_DRAW_MAP) { memset(vp->map_draw_vehicles_cache.done_hash_bits, 0, sizeof(vp->map_draw_vehicles_cache.done_hash_bits)); - vp->map_draw_vehicles_cache.vehicle_pixels.assign(vp->width * vp->height, false); + vp->map_draw_vehicles_cache.vehicle_pixels.assign(vp->ScreenArea(), false); if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 32) { - vp->land_pixel_cache.assign(vp->height * vp->width * 4, 0xD7); + vp->land_pixel_cache.assign(vp->ScreenArea() * 4, 0xD7); } else { - vp->land_pixel_cache.assign(vp->height * vp->width, 0xD7); + vp->land_pixel_cache.assign(vp->ScreenArea(), 0xD7); } } else { vp->map_draw_vehicles_cache.vehicle_pixels.clear(); @@ -4108,7 +4108,7 @@ void MarkViewportDirty(Viewport * const vp, int left, int top, int right, int bo uint bitdepth = BlitterFactory::GetCurrentBlitter()->GetScreenDepth() / 8; uint8 *land_cache = vp->land_pixel_cache.data() + ((l + (t * vp->width)) * bitdepth); while (--h) { - memset(land_cache, 0xD7, w * bitdepth); + memset(land_cache, 0xD7, (size_t)w * bitdepth); land_cache += vp->width * bitdepth; } } diff --git a/src/viewport_type.h b/src/viewport_type.h index 074e21155d..da6cd0ec35 100644 --- a/src/viewport_type.h +++ b/src/viewport_type.h @@ -79,6 +79,11 @@ struct Viewport { this->update_vehicles = false; } + size_t ScreenArea() const + { + return ((size_t)this->width) * ((size_t)this->height); + } + private: uint GetDirtyBlockShift() const {