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++)
{
if (save->signs[i].text[0])
if (save->signs[i].text.length())
{
sign tempSign = save->signs[i];
tempSign.x += partP.X;
tempSign.y += partP.Y;
if (!InBounds(tempSign.x, tempSign.y))
{
continue;
}
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];
fvx[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->fanVelX[bpos];
fvy[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->fanVelY[bpos];
bmap[bpos.Y][bpos.X] = save->blockMap[spos];
fvx[bpos.Y][bpos.X] = save->fanVelX[spos];
fvy[bpos.Y][bpos.X] = save->fanVelY[spos];
}
if (includePressure)
{
if (save->hasPressure)
{
pv[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->pressure[bpos];
vx[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->velocityX[bpos];
vy[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->velocityY[bpos];
pv[bpos.Y][bpos.X] = save->pressure[spos];
vx[bpos.Y][bpos.X] = save->velocityX[spos];
vy[bpos.Y][bpos.X] = save->velocityY[spos];
}
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)
{
air->bmap_blockair[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->blockAir[bpos];
air->bmap_blockairh[blockP.Y + bpos.Y][blockP.X + bpos.X] = save->blockAirh[bpos];
air->bmap_blockair[bpos.Y][bpos.X] = save->blockAir[spos];
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();
}
bool Simulation::InBounds(int x, int y)
{
return (x>=0 && y>=0 && x<XRES && y<YRES);
}
int Simulation::remainder_p(int x, int y)
{
return (x % y) + (x>=0 ? 0 : y);

View File

@ -218,7 +218,10 @@ public:
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
static int remainder_p(int x, int y);