News:

The Forum Rules and Guidelines
Our forum has Rules and Guidelines. Please, be kind and read them ;).

I believe I found a fencepost error

Started by neroden, May 27, 2024, 06:13:16 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

neroden

Ran into this while doing other work.  Looks like a fencepost error (off by one).  Indexes can range from 0 to get_count() - 1 last I checked.

diff --git a/src/simutrans/simconvoi.cc b/src/simutrans/simconvoi.cc
index 4e6189ef0..a7656bde8 100644
--- a/src/simutrans/simconvoi.cc
+++ b/src/simutrans/simconvoi.cc
@@ -476,8 +476,8 @@ DBG_MESSAGE("convoi_t::finish_rd()","next_stop_index=%d", next_stop_index );
else {
// since start may have been changed
route_t::index_t start_index = max(1,fahr[vehicle_count-1]->get_route_index())-1;
- if (start_index > route.get_count()) {
- dbg->error( "convoi_t::finish_rd()", "Routeindex of last vehicle of (%s) too large (%hu > %hu)!", get_name(), start_index, route.get_count() );
+ if (start_index >= route.get_count()) {
+ dbg->error( "convoi_t::finish_rd()", "Routeindex of last vehicle of (%s) too large (%hu >= %hu)!", get_name(), start_index, route.get_count() );
start_index = 0;
}

prissi

Indeed, although that should have never been triggered unless very very old games. Incorporated in r11308