From bb9121dbd4690405b54e7e6ed6e711ead16435ac Mon Sep 17 00:00:00 2001 From: Rubidium Date: Mon, 10 May 2021 23:43:52 +0200 Subject: [PATCH] Fix: comparison of narrow type to wide type in loop (potential for infinite loops) --- src/depot_gui.cpp | 4 ++-- src/industry_gui.cpp | 8 ++++---- src/linkgraph/linkgraph.h | 2 +- src/linkgraph/linkgraphjob.cpp | 2 +- src/linkgraph/mcf.cpp | 8 ++++---- src/network/core/packet.cpp | 7 ++++--- src/rail_gui.cpp | 2 +- src/saveload/linkgraph_sl.cpp | 2 +- src/story_gui.cpp | 4 ++-- src/texteff.hpp | 2 +- 10 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp index 8c018b73d8..0756eae2ca 100644 --- a/src/depot_gui.cpp +++ b/src/depot_gui.cpp @@ -395,11 +395,11 @@ struct DepotWindow : Window { uint16 rows_in_display = wid->current_y / wid->resize_y; - uint16 num = this->vscroll->GetPosition() * this->num_columns; + uint num = this->vscroll->GetPosition() * this->num_columns; uint maxval = static_cast(std::min(this->vehicle_list.size(), num + (rows_in_display * this->num_columns))); int y; for (y = r.top + 1; num < maxval; y += this->resize.step_height) { // Draw the rows - for (byte i = 0; i < this->num_columns && num < maxval; i++, num++) { + for (uint i = 0; i < this->num_columns && num < maxval; i++, num++) { /* Draw all vehicles in the current row */ const Vehicle *v = this->vehicle_list[num]; if (this->num_columns == 1) { diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 4e6c61ee21..b1768af1f0 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -357,7 +357,7 @@ class BuildIndustryWindow : public Window { int numcargo = 0; int firstcargo = -1; - for (byte j = 0; j < cargolistlen; j++) { + for (int j = 0; j < cargolistlen; j++) { if (cargolist[j] == CT_INVALID) continue; numcargo++; if (firstcargo < 0) { @@ -419,7 +419,7 @@ public: switch (widget) { case WID_DPI_MATRIX_WIDGET: { Dimension d = GetStringBoundingBox(STR_FUND_INDUSTRY_MANY_RANDOM_INDUSTRIES); - for (byte i = 0; i < this->count; i++) { + for (uint16 i = 0; i < this->count; i++) { if (this->index[i] == INVALID_INDUSTRYTYPE) continue; d = maxdim(d, GetStringBoundingBox(GetIndustrySpec(this->index[i])->name)); } @@ -438,7 +438,7 @@ public: uint extra_lines_newgrf = 0; uint max_minwidth = FONT_HEIGHT_NORMAL * MAX_MINWIDTH_LINEHEIGHTS; Dimension d = {0, 0}; - for (byte i = 0; i < this->count; i++) { + for (uint16 i = 0; i < this->count; i++) { if (this->index[i] == INVALID_INDUSTRYTYPE) continue; const IndustrySpec *indsp = GetIndustrySpec(this->index[i]); @@ -528,7 +528,7 @@ public: int icon_bottom = icon_top + this->legend.height; int y = r.top; - for (byte i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->count; i++) { + for (uint16 i = 0; i < this->vscroll->GetCapacity() && i + this->vscroll->GetPosition() < this->count; i++) { bool selected = this->selected_index == i + this->vscroll->GetPosition(); if (this->index[i + this->vscroll->GetPosition()] == INVALID_INDUSTRYTYPE) { diff --git a/src/linkgraph/linkgraph.h b/src/linkgraph/linkgraph.h index 99348daa1c..4f44db8276 100644 --- a/src/linkgraph/linkgraph.h +++ b/src/linkgraph/linkgraph.h @@ -495,7 +495,7 @@ public: * Get the current size of the component. * @return Size. */ - inline uint Size() const { return (uint)this->nodes.size(); } + inline uint16 Size() const { return (uint16)this->nodes.size(); } /** * Get date of last compression. diff --git a/src/linkgraph/linkgraphjob.cpp b/src/linkgraph/linkgraphjob.cpp index 5e69dbeba0..6a70e9fb69 100644 --- a/src/linkgraph/linkgraphjob.cpp +++ b/src/linkgraph/linkgraphjob.cpp @@ -101,7 +101,7 @@ LinkGraphJob::~LinkGraphJob() /* Link graph has been merged into another one. */ if (!LinkGraph::IsValidID(this->link_graph.index)) return; - uint size = this->Size(); + uint16 size = this->Size(); for (NodeID node_id = 0; node_id < size; ++node_id) { Node from = (*this)[node_id]; diff --git a/src/linkgraph/mcf.cpp b/src/linkgraph/mcf.cpp index 003412850c..f24a8e0282 100644 --- a/src/linkgraph/mcf.cpp +++ b/src/linkgraph/mcf.cpp @@ -260,7 +260,7 @@ void MultiCommodityFlow::Dijkstra(NodeID source_node, PathVector &paths) { typedef std::set AnnoSet; Tedge_iterator iter(this->job); - uint size = this->job.Size(); + uint16 size = this->job.Size(); AnnoSet annos; paths.resize(size, nullptr); for (NodeID node = 0; node < size; ++node) { @@ -473,7 +473,7 @@ bool MCF1stPass::EliminateCycles(PathVector &path, NodeID origin_id, NodeID next bool MCF1stPass::EliminateCycles() { bool cycles_found = false; - uint size = this->job.Size(); + uint16 size = this->job.Size(); PathVector path(size, nullptr); for (NodeID node = 0; node < size; ++node) { /* Starting at each node in the graph find all cycles involving this @@ -491,7 +491,7 @@ bool MCF1stPass::EliminateCycles() MCF1stPass::MCF1stPass(LinkGraphJob &job) : MultiCommodityFlow(job) { PathVector paths; - uint size = job.Size(); + uint16 size = job.Size(); uint accuracy = job.Settings().accuracy; bool more_loops; std::vector finished_sources(size); @@ -540,7 +540,7 @@ MCF2ndPass::MCF2ndPass(LinkGraphJob &job) : MultiCommodityFlow(job) { this->max_saturation = UINT_MAX; // disable artificial cap on saturation PathVector paths; - uint size = job.Size(); + uint16 size = job.Size(); uint accuracy = job.Settings().accuracy; bool demand_left = true; std::vector finished_sources(size); diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp index 883097dea1..738afec4e6 100644 --- a/src/network/core/packet.cpp +++ b/src/network/core/packet.cpp @@ -374,14 +374,13 @@ uint64 Packet::Recv_uint64() */ void Packet::Recv_string(char *buffer, size_t size, StringValidationSettings settings) { - PacketSize pos; char *bufp = buffer; const char *last = buffer + size - 1; /* Don't allow reading from a closed socket */ if (cs->HasClientQuit()) return; - pos = this->pos; + size_t pos = this->pos; while (--size > 0 && pos < this->Size() && (*buffer++ = this->buffer[pos++]) != '\0') {} if (size == 0 || pos == this->Size()) { @@ -391,7 +390,9 @@ void Packet::Recv_string(char *buffer, size_t size, StringValidationSettings set while (pos < this->Size() && this->buffer[pos] != '\0') pos++; pos++; } - this->pos = pos; + + assert(pos <= std::numeric_limits::max()); + this->pos = static_cast(pos); str_validate(bufp, last, settings); } diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index 49c7d736af..3cb99d785d 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -1219,7 +1219,7 @@ public: StringID str = this->GetWidget(widget)->widget_data; for (auto station_class : this->station_classes) { StationClass *stclass = StationClass::Get(station_class); - for (uint16 j = 0; j < stclass->GetSpecCount(); j++) { + for (uint j = 0; j < stclass->GetSpecCount(); j++) { const StationSpec *statspec = stclass->GetSpec(j); SetDParam(0, (statspec != nullptr && statspec->name != 0) ? statspec->name : STR_STATION_CLASS_DFLT); d = maxdim(d, GetStringBoundingBox(str)); diff --git a/src/saveload/linkgraph_sl.cpp b/src/saveload/linkgraph_sl.cpp index f571e331a5..e46054cf0f 100644 --- a/src/saveload/linkgraph_sl.cpp +++ b/src/saveload/linkgraph_sl.cpp @@ -139,7 +139,7 @@ static const SaveLoad _edge_desc[] = { */ void SaveLoad_LinkGraph(LinkGraph &lg) { - uint size = lg.Size(); + uint16 size = lg.Size(); for (NodeID from = 0; from < size; ++from) { Node *node = &lg.nodes[from]; SlObject(node, _node_desc); diff --git a/src/story_gui.cpp b/src/story_gui.cpp index 0ed39ba045..ccb9ccf7b8 100644 --- a/src/story_gui.cpp +++ b/src/story_gui.cpp @@ -776,7 +776,7 @@ public: case WID_SB_SEL_PAGE: { /* Get max title width. */ - for (uint16 i = 0; i < this->story_pages.size(); i++) { + for (size_t i = 0; i < this->story_pages.size(); i++) { const StoryPage *s = this->story_pages[i]; if (s->title != nullptr) { @@ -822,7 +822,7 @@ public: if (!list.empty()) { /* Get the index of selected page. */ int selected = 0; - for (uint16 i = 0; i < this->story_pages.size(); i++) { + for (size_t i = 0; i < this->story_pages.size(); i++) { const StoryPage *p = this->story_pages[i]; if (p->index == this->selected_page_id) break; selected++; diff --git a/src/texteff.hpp b/src/texteff.hpp index d122b17e9b..56b5933926 100644 --- a/src/texteff.hpp +++ b/src/texteff.hpp @@ -24,7 +24,7 @@ enum TextEffectMode { INVALID_TE_ID = 0xFFFF, }; -typedef uint16 TextEffectID; +typedef size_t TextEffectID; void MoveAllTextEffects(uint delta_ms); TextEffectID AddTextEffect(StringID msg, int x, int y, uint8 duration, TextEffectMode mode);