I believe this bit of code is the culprit
offset += MTU;
data = &data[offset];
offset starts at 0, data starts at x.
- First time round the loop offset is now 1460, data = data + offset = x + 1460
- Next time round the loop offset is now 2920, data = data + offset = (x + 1460) + 2920 = x + 4380
- Next time round the loop offset is now 4380, data = data + offset = (x + 4380) + 4380 = x + 8790
So data
is increasing faster than it should. This means that eventually you will be accessing outside of the bounds of data
.
I recommend removing the data = &data[offset];
part and just using data + offset
in the memcpy.