Skip to content

Commit eda7284

Browse files
committed
gui: Fix various off-by-one errors in memory viewer
1 parent 040ce28 commit eda7284

3 files changed

Lines changed: 11 additions & 15 deletions

File tree

gui/qt/debugger/hexwidget.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void HexWidget::scroll(int value) {
7070
}
7171
}
7272
if (value >= verticalScrollBar()->maximum()) {
73-
int addr = m_maxOffset + m_base + 1;
73+
int addr = m_base + m_size;
7474
QByteArray data;
7575
for (int i = 0; i < m_bytesPerLine && (addr + i) < 0x1000000; i++) {
7676
data.append(mem_peek_byte(addr + i));
@@ -118,10 +118,10 @@ int HexWidget::indexOf(const QByteArray &ba) {
118118

119119
int HexWidget::indexNotOf(const QByteArray &ba) {
120120
int res = -1;
121-
QByteArray buffer{m_data.mid(m_cursorOffset, m_maxOffset)};
121+
QByteArray buffer{m_data.mid(m_cursorOffset / 2)};
122122
std::size_t found = buffer.toStdString().find_first_not_of(ba.toStdString());
123123
if (found != std::string::npos) {
124-
setOffset(res = static_cast<int>(found));
124+
setOffset(res = static_cast<int>(m_cursorOffset / 2 + found));
125125
}
126126
return res;
127127
}
@@ -144,8 +144,8 @@ void HexWidget::setHighlight(const int address) {
144144
}
145145

146146
void HexWidget::setCursorOffset(int offset, bool selection, bool clearHighlight) {
147-
if (offset > m_size * 2) {
148-
offset = m_size * 2;
147+
if (offset >= m_size * 2) {
148+
offset = m_size * 2 - 1;
149149
}
150150
if (offset < 0) {
151151
offset = 0;
@@ -213,7 +213,6 @@ void HexWidget::showCursor() {
213213

214214
void HexWidget::adjust() {
215215
m_size = m_data.size();
216-
m_maxOffset = m_size - 1;
217216

218217
m_charWidth = fontMetrics().horizontalAdvance(QLatin1Char('D'));
219218
m_charHeight = fontMetrics().height();
@@ -234,20 +233,20 @@ void HexWidget::adjust() {
234233
horizontalScrollBar()->setRange(0, xWidth - viewport()->width());
235234
horizontalScrollBar()->setPageStep(viewport()->width());
236235

237-
int rows = m_size / m_bytesPerLine;
236+
int rows = (m_size + m_bytesPerLine - 1) / m_bytesPerLine;
238237
int visibleHeight = viewport()->height() - m_gap;
239238
if (horizontalScrollBar()->isVisible()) {
240239
visibleHeight -= horizontalScrollBar()->height();
241240
}
242241
m_visibleRows = visibleHeight / m_charHeight;
243-
verticalScrollBar()->setRange(0, rows - m_visibleRows);
242+
verticalScrollBar()->setRange(0, rows - m_visibleRows - 1);
244243
verticalScrollBar()->setPageStep(m_visibleRows);
245244

246245
int lines = verticalScrollBar()->value();
247246
m_lineStart = lines * m_bytesPerLine;
248247
m_lineEnd = m_lineStart + m_visibleRows * m_bytesPerLine - 1;
249248
if (m_lineEnd >= m_size) {
250-
m_lineEnd = m_maxOffset;
249+
m_lineEnd = m_size - 1;
251250
}
252251

253252
int x, y = (((m_cursorOffset / 2) - m_lineStart) / m_bytesPerLine + 1) * m_charHeight;
@@ -363,9 +362,7 @@ void HexWidget::paintEvent(QPaintEvent *event) {
363362
if (addr + m_base > 0xffffff) { break; }
364363
painter.setPen(cText);
365364
painter.drawText(xAddr, y, int2hex(m_base + lineAddr, 6));
366-
for (int col = 0; col < m_bytesPerLine && addr < m_maxOffset; col++) {
367-
addr = lineAddr + col;
368-
365+
for (int col = 0; col < m_bytesPerLine && addr < m_size; col++, addr++) {
369366
painter.setPen(cText);
370367
uint8_t data = static_cast<uint8_t>(m_data[addr]);
371368
uint8_t flags = debug.addr[addr + m_base];

gui/qt/debugger/hexwidget.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ private slots:
9191
QByteArray m_data;
9292
QByteArray m_modified;
9393
int m_size;
94-
int m_maxOffset;
9594

9695
QRect m_cursor;
9796
int m_cursorOffset = 0;

gui/qt/memorywidget.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void MainWindow::memUpdateEdit(HexWidget *edit, bool force) {
7171
int base = edit->getBase();
7272
int addr = off + base;
7373
int start = addr - 0x1000;
74-
int end = addr + 0x1000;
74+
int end = addr + 0x1000 - 1;
7575
off = 0x1000;
7676

7777
if (start < 0) {
@@ -83,7 +83,7 @@ void MainWindow::memUpdateEdit(HexWidget *edit, bool force) {
8383
}
8484
data.resize(end - start + 1);
8585

86-
for (int j = 0, i = start; i < end; j++, i++) {
86+
for (int j = 0, i = start; i <= end; j++, i++) {
8787
data[j] = static_cast<char>(mem_peek_byte(static_cast<uint32_t>(i)));
8888
}
8989

0 commit comments

Comments
 (0)