Skip to content

Vector subscript out of range in Nicowar::update_farming on Island of the Renfur #122

@Quipyowert2

Description

@Quipyowert2

Globulation 2 crashed in NewNicowar::update_farming (AINicowar.cpp:2647) on the Island of the Renfur level with one of each AI in the game. At Gradient::get_height(), x is 21, y is -1, and width=128, which results in an index of -107 into the gradients array. Visual Studio 2022 then halts the program because the index is invalid.

Should be easily fixable by checking y>0 before passing y-1 to water_gradient.get_height() or adding a modulo operator somewhere.

I noticed this crash when profiling my PR 120 with Intel vTune plugin for Visual Studio and then checked out master branch, and it crashes the same way.

Backtrace below:

glob2.exe!std::vector<short,std::allocator<short>>::operator[](const unsigned __int64 _Pos) Line 1899
	at C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\vector(1899)
glob2.exe!AIEcho::Gradients::Gradient::get_height(int posx, int posy) Line 929
	at D:\Linux_home\nathan\src\glob2\src\AIEcho.cpp(929)
glob2.exe!NewNicowar::update_farming(AIEcho::Echo & echo) Line 2647
	at D:\Linux_home\nathan\src\glob2\src\AINicowar.cpp(2647)
glob2.exe!NewNicowar::tick(AIEcho::Echo & echo) Line 412
	at D:\Linux_home\nathan\src\glob2\src\AINicowar.cpp(412)
glob2.exe!AIEcho::Echo::getOrder() Line 5054
	at D:\Linux_home\nathan\src\glob2\src\AIEcho.cpp(5054)
glob2.exe!AI::getOrder(bool paused) Line 105
	at D:\Linux_home\nathan\src\glob2\src\AI.cpp(105)
glob2.exe!Engine::run() Line 399
	at D:\Linux_home\nathan\src\glob2\src\Engine.cpp(399)
glob2.exe!Glob2::run(int argc, char * * argv) Line 347
	at D:\Linux_home\nathan\src\glob2\src\Glob2.cpp(347)
glob2.exe!SDL_main(int argc, char * * argv) Line 442
	at D:\Linux_home\nathan\src\glob2\src\Glob2.cpp(442)
glob2.exe!main_getcmdline() Line 80
	at D:\Linux_home\nathan\src\glob2\vcpkg_installed\x64-windows\vcpkg\blds\sdl2\src\ase-2.30.3-180151d870.clean\src\main\windows\SDL_windows_main.c(80)
[External Code]

To reproduce:

  1. Custom Game
  2. Choose Island of Renfur map
  3. Add one each of the various AIs
  4. Start game
  5. Wait 30 seconds
  6. Globulation crashes

Actual results:
Visual Studio 2022 pops up a "Vector subscript out of range" error.

Expected results:
Didn't expect it to crash.

Level: Island of the Renfur
AIs: One each of Numbi Castor WarRush ReachToInfinity Nicowar
Commit: sprite-sheet (PR #120) 4cd6bded (Jun 8, 2024) Also crashes with master bf277676 (Jun 6, 2024)
At Gradient::get_height: x=21 y=-1 width=128
Visual Studio settings that may be relevant:

  • Optimization: MaxSpeed
  • Basic Runtime Checks: Default
  • DebugInformationFormat: EditAndContinue
    Windows version: 10 Home 22H2 (OS Build 19045.4412)
    Compiler:
MSBuild version 17.10.4+10fbfbf2e for .NET Framework
17.10.4.21802

Full error:

Debug Assertion Failed!
Program: D:\Linux_home\nathan\src\glob2\x64\Debug SDL\glob2.exe
File: C\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.40.33807\include\vector
Line: 1900

Expression: vector subscript out of range

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions