Skip to content

Commit ea8de4d

Browse files
Fix potential out of bounds map layout read
1 parent 97bf444 commit ea8de4d

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project somewhat adheres to [Semantic Versioning](https://semver.org/sp
1616
- Previously, the middle mouse button could be used as a shortcut with the pencil tool to switch to bucket-fill mode. This is now achieved using the `Alt` key.
1717

1818
### Fixed
19+
- Fix potential crash when painting and the cursor leaves the map area.
1920
- Fix rare crash while quitting Porymap.
2021
- Fix `Edit > Clear Map Entries` in the Region Map Editor not saving the applied changes.
2122
- Fix `Edit > Undo/Redo` appearing enabled even when they don't do anything.

include/core/maplayout.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ class Layout : public QObject {
125125
bool isWithinBorderBounds(int x, int y) const;
126126

127127
bool getBlock(int x, int y, Block *out) const;
128+
bool getBlock(const QPoint& pos, Block *out) const { return getBlock(pos.x(), pos.y(), out); }
128129
void setBlock(int x, int y, Block block, bool enableScriptCallback = false);
130+
void setBlock(const QPoint& pos, Block block, bool enableScriptCallback = false) { setBlock(pos.x(), pos.y(), block, enableScriptCallback); }
129131
void setBlockdata(Blockdata blockdata, bool enableScriptCallback = false);
130132

131133
uint16_t getMetatileId(int x, int y) const;

src/editor.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,28 +1244,27 @@ void Editor::onMapHoverCleared() {
12441244
}
12451245

12461246
void Editor::setStatusFromMapPos(const QPoint &pos) {
1247-
int x = pos.x();
1248-
int y = pos.y();
1247+
Block block;
1248+
if (!this->layout || !this->layout->getBlock(pos, &block)) {
1249+
ui->statusBar->clearMessage();
1250+
return;
1251+
}
1252+
12491253
if (this->editMode == EditMode::Metatiles) {
1250-
int blockIndex = y * layout->getWidth() + x;
1251-
int metatileId = layout->blockdata.at(blockIndex).metatileId();
12521254
this->ui->statusBar->showMessage(QString("X: %1, Y: %2, %3, Scale = %4x")
1253-
.arg(x)
1254-
.arg(y)
1255-
.arg(getMetatileDisplayMessage(metatileId))
1255+
.arg(pos.x())
1256+
.arg(pos.y())
1257+
.arg(getMetatileDisplayMessage(block.metatileId()))
12561258
.arg(QString::number(zoomLevels[this->scaleIndex], 'g', 2)));
12571259
} else if (this->editMode == EditMode::Collision) {
1258-
int blockIndex = y * layout->getWidth() + x;
1259-
uint16_t collision = layout->blockdata.at(blockIndex).collision();
1260-
uint16_t elevation = layout->blockdata.at(blockIndex).elevation();
12611260
this->ui->statusBar->showMessage(QString("X: %1, Y: %2, %3")
1262-
.arg(x)
1263-
.arg(y)
1264-
.arg(this->getMovementPermissionText(collision, elevation)));
1261+
.arg(pos.x())
1262+
.arg(pos.y())
1263+
.arg(this->getMovementPermissionText(block.collision(), block.elevation())));
12651264
} else if (this->editMode == EditMode::Events) {
12661265
this->ui->statusBar->showMessage(QString("X: %1, Y: %2, Scale = %3x")
1267-
.arg(x)
1268-
.arg(y)
1266+
.arg(pos.x())
1267+
.arg(pos.y())
12691268
.arg(QString::number(zoomLevels[this->scaleIndex], 'g', 2)));
12701269
}
12711270
}

0 commit comments

Comments
 (0)