Skip to content

Commit

Permalink
Merge pull request #611 from zzam/pathfinder
Browse files Browse the repository at this point in the history
Pathfinder: Fix out-of-bounds read/write at load/store of games
  • Loading branch information
Jarod42 authored Feb 26, 2024
2 parents 35df666 + a214447 commit 34ec4cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/include/pathfinder.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class PathFinderInput
void SetMinRange(int range);
void SetMaxRange(int range);

void PathRacalculated();
void PathRecalculated();

void Save(CFile &file) const;
void Load(lua_State *l);
Expand Down
17 changes: 11 additions & 6 deletions src/pathfinder/pathfinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ void PathFinderInput::SetMaxRange(int range)
}
}

void PathFinderInput::PathRacalculated()
void PathFinderInput::PathRecalculated()
{
unitSize.x = unit->Type->TileWidth;
unitSize.y = unit->Type->TileHeight;
Expand Down Expand Up @@ -405,18 +405,23 @@ static int NewPath(PathFinderInput &input, PathFinderOutput &output)
input.GetMinRange(), input.GetMaxRange(),
path, PathFinderOutput::MAX_PATH_LENGTH,
*input.GetUnit());
input.PathRacalculated();
input.PathRecalculated();
if (i == PF_FAILED) {
i = PF_UNREACHABLE;
}

// Update path if it was requested. Otherwise we may only want
// to know if there exists a path.
if (path != nullptr) {
output.Length = std::min<int>(i, PathFinderOutput::MAX_PATH_LENGTH);
output.OverflowLength = std::min<int>(i - output.Length, PathFinderOutput::MAX_OVERFLOW);
if (output.Length == 0) {
++output.Length;
if (i >= 0) {
output.Length = std::min<int>(i, PathFinderOutput::MAX_PATH_LENGTH);
output.OverflowLength = std::min<int>(i - output.Length, PathFinderOutput::MAX_OVERFLOW);
if (output.Length == 0) {
++output.Length;
}
} else {
output.Length = 0;
output.OverflowLength = 0;
}
}
return i;
Expand Down
9 changes: 6 additions & 3 deletions src/unit/script_unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,13 @@ void PathFinderOutput::Load(lua_State *l)
LuaError(l, "incorrect argument _");
}
const int subargs = lua_rawlen(l, -1);
for (int k = 0; k < subargs; ++k) {
this->Path[k] = LuaToNumber(l, -1, k + 1);
if (subargs <= PathFinderOutput::MAX_PATH_LENGTH)
{
for (int k = 0; k < subargs; ++k) {
this->Path[k] = LuaToNumber(l, -1, k + 1);
}
this->Length = subargs;
}
this->Length = subargs;
lua_pop(l, 1);
} else {
LuaError(l, "PathFinderOutput::Load: Unsupported tag: %s", tag.data());
Expand Down
2 changes: 1 addition & 1 deletion src/unit/unit_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void PathFinderOutput::Save(CFile &file) const
if (this->OverflowLength) {
file.printf("\"overflow-length\", %d, ", this->OverflowLength);
}
if (this->Length > 0) {
if (this->Length > 0 && this->Length <= PathFinderOutput::MAX_PATH_LENGTH) {
file.printf("\"path\", {");
for (int i = 0; i < this->Length; ++i) {
file.printf("%d, ", this->Path[i]);
Expand Down

0 comments on commit 34ec4cb

Please sign in to comment.