Skip to content

Commit ee77432

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 the difference between pageSize and offset to limit chunksize. 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 724b492 commit ee77432

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
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 (chunksize > pageSize - offset) {
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
{

src/jtag3rw.cc

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

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

159160
while (numBytes > 0)
160161
{

0 commit comments

Comments
 (0)