Skip to content

Commit 85fca26

Browse files
authored
Merge pull request #124 from OpenFodder/codex/propose-fix-for-sprite-vector-oob-write
Clamp sprite limit inputs to prevent OOB and exhaustion
1 parent 480f754 commit 85fca26

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

Source/Parameters.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using Json = nlohmann::json;
3131
cxxopts::Options* sFodderParameters::mCliOptions = 0;
3232

3333
/* These values override the original engine values, when in custom mode */
34-
const size_t CUSTOM_DEFAULT_MAX_SPRITES = 100000;
34+
const size_t CUSTOM_DEFAULT_MAX_SPRITES = sFodderParameters::MAX_SPRITES_MAX;
3535
const size_t CUSTOM_DEFAULT_MAX_SPAWN = 25;
3636

3737
std::string sFodderParameters::ToJson() {
@@ -90,8 +90,10 @@ bool sFodderParameters::FromJson(const std::string& pJson) {
9090
else
9191
mSpritesMax = 45; // The original engine limit
9292

93-
if (mSpritesMax < 16)
94-
mSpritesMax = 16;
93+
if (mSpritesMax < MIN_SPRITES_MAX)
94+
mSpritesMax = MIN_SPRITES_MAX;
95+
if (mSpritesMax > MAX_SPRITES_MAX)
96+
mSpritesMax = MAX_SPRITES_MAX;
9597
}
9698

9799
// Max Spawned
@@ -309,8 +311,10 @@ bool sFodderParameters::ProcessCLI(int argc, char *argv[]) {
309311
if (result.count("max-spawn"))
310312
mSpawnEnemyMax = result["max-spawn"].as<uint32_t>();
311313

312-
if (mSpritesMax < 16)
313-
mSpritesMax = 16;
314+
if (mSpritesMax < MIN_SPRITES_MAX)
315+
mSpritesMax = MIN_SPRITES_MAX;
316+
if (mSpritesMax > MAX_SPRITES_MAX)
317+
mSpritesMax = MAX_SPRITES_MAX;
314318

315319
// Cheats perm enabled in debug build
316320
#ifdef _DEBUG
@@ -507,6 +511,11 @@ bool sFodderParameters::ProcessINI() {
507511
auto maxspawn = ini.get("maxspawn", 0);
508512
if (maxspawn)
509513
mSpawnEnemyMax = maxspawn;
514+
515+
if (mSpritesMax < MIN_SPRITES_MAX)
516+
mSpritesMax = MIN_SPRITES_MAX;
517+
if (mSpritesMax > MAX_SPRITES_MAX)
518+
mSpritesMax = MAX_SPRITES_MAX;
510519
}
511520
}
512521

Source/Parameters.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class sFodderParameters {
8080
size_t mSpritesMax;
8181
size_t mSpawnEnemyMax;
8282

83+
static constexpr size_t MIN_SPRITES_MAX = 45;
84+
static constexpr size_t MAX_SPRITES_MAX = 100000;
85+
8386
bool mShowHelp;
8487
bool mCopyProtection;
8588

0 commit comments

Comments
 (0)