Skip to content

Commit 91ba436

Browse files
committed
fix heap corruption on LP64 platforms
Mixing unsigned long and int on LP64 platforms caused the chunksize adjustment to be wrong for flash memory reads from "negative" addresses. This caused runaway reads and heap corruption, because chunksize was being adjusted to be greater than numBytes. Simplify the computation by computing the offset within the page using a mask, and use that to check the length against pageSize. This is less necessary after the qXfer:memory-map:read support was added, but it's definitely needed in 2.13, and maybe some older GDB versions don't support qXfer:memory-map:read.
1 parent 3c39ae8 commit 91ba436

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/jtag2rw.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,12 @@ uchar *jtag2::jtagRead(unsigned long addr, unsigned int numBytes)
151151
unsigned int chunksize = numBytes;
152152
unsigned int targetOffset = 0;
153153

154-
if (addr + chunksize >= pageAddr + pageSize)
154+
offset = addr & mask;
155+
if (offset + chunksize > pageSize) {
155156
// Chunk would cross a page boundary, reduce it
156157
// appropriately.
157-
chunksize -= (addr + numBytes - (pageAddr + pageSize));
158-
offset = addr - pageAddr;
158+
chunksize = pageSize - offset;
159+
}
159160

160161
while (numBytes > 0)
161162
{

0 commit comments

Comments
 (0)