Fix crash upon an out of bounds Simulation::Load

Also fix out of bounds signs being allowed to load and a potential out of bounds operator[] on Sign::text.
This commit is contained in:
Tamás Bálint Misius 2023-06-02 07:06:40 +02:00
parent 1a69bbfb51
commit a205612802
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
2 changed files with 23 additions and 18 deletions

View File

@ -294,36 +294,43 @@ void Simulation::Load(const GameSave *originalSave, bool includePressure, Vec2<i
for (size_t i = 0; i < save->signs.size() && signs.size() < MAXSIGNS; i++) for (size_t i = 0; i < save->signs.size() && signs.size() < MAXSIGNS; i++)
{ {
if (save->signs[i].text[0]) if (save->signs[i].text.length())
{ {
sign tempSign = save->signs[i]; sign tempSign = save->signs[i];
tempSign.x += partP.X; tempSign.x += partP.X;
tempSign.y += partP.Y; tempSign.y += partP.Y;
if (!InBounds(tempSign.x, tempSign.y))
{
continue;
}
signs.push_back(tempSign); signs.push_back(tempSign);
} }
} }
for (auto bpos : save->blockSize.OriginRect()) for (auto bpos : RectSized(blockP, save->blockSize) & CELLS.OriginRect())
{ {
if(save->blockMap[bpos]) auto spos = bpos - blockP;
if (save->blockMap[spos])
{ {
bmap[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->blockMap[bpos]; bmap[bpos.Y][bpos.X] = save->blockMap[spos];
fvx[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->fanVelX[bpos]; fvx[bpos.Y][bpos.X] = save->fanVelX[spos];
fvy[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->fanVelY[bpos]; fvy[bpos.Y][bpos.X] = save->fanVelY[spos];
} }
if (includePressure) if (includePressure)
{ {
if (save->hasPressure) if (save->hasPressure)
{ {
pv[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->pressure[bpos]; pv[bpos.Y][bpos.X] = save->pressure[spos];
vx[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->velocityX[bpos]; vx[bpos.Y][bpos.X] = save->velocityX[spos];
vy[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->velocityY[bpos]; vy[bpos.Y][bpos.X] = save->velocityY[spos];
} }
if (save->hasAmbientHeat) if (save->hasAmbientHeat)
hv[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->ambientHeat[bpos]; {
hv[bpos.Y][bpos.X] = save->ambientHeat[spos];
}
if (save->hasBlockAirMaps) if (save->hasBlockAirMaps)
{ {
air->bmap_blockair[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->blockAir[bpos]; air->bmap_blockair[bpos.Y][bpos.X] = save->blockAir[spos];
air->bmap_blockairh[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->blockAirh[bpos]; air->bmap_blockairh[bpos.Y][bpos.X] = save->blockAirh[spos];
} }
} }
} }
@ -4085,11 +4092,6 @@ String Simulation::BasicParticleInfo(Particle const &sample_part) const
return sampleInfo.Build(); return sampleInfo.Build();
} }
bool Simulation::InBounds(int x, int y)
{
return (x>=0 && y>=0 && x<XRES && y<YRES);
}
int Simulation::remainder_p(int x, int y) int Simulation::remainder_p(int x, int y)
{ {
return (x % y) + (x>=0 ? 0 : y); return (x % y) + (x>=0 ? 0 : y);

View File

@ -218,7 +218,10 @@ public:
Simulation(); Simulation();
~Simulation(); ~Simulation();
bool InBounds(int x, int y); static bool InBounds(int x, int y)
{
return RES.OriginRect().Contains({ x, y });
}
// These don't really belong anywhere at the moment, so go here for loop edge mode // These don't really belong anywhere at the moment, so go here for loop edge mode
static int remainder_p(int x, int y); static int remainder_p(int x, int y);