From f4a289091ee226cb9a323e2b2796ba706c2fc149 Mon Sep 17 00:00:00 2001 From: Frej Drejhammar Date: Sun, 17 May 2015 16:34:49 +0200 Subject: [PATCH] Avoid buffer overrun on smaller AVRs On smaller AVRs, such as the attiny13, MAX_IOs will be larger than the size of SRAM which will lead to out of range writes to a malloc allocated buffer. Therefore limit the number of bytes cleared to the size of SRAM. --- simavr/sim/sim_avr.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/simavr/sim/sim_avr.c b/simavr/sim/sim_avr.c index 6c985cc..3e12c74 100644 --- a/simavr/sim/sim_avr.c +++ b/simavr/sim/sim_avr.c @@ -118,10 +118,11 @@ void avr_terminate(avr_t * avr) void avr_reset(avr_t * avr) { + int noof_ios = MAX_IOs > avr->ramend ? avr->ramend : avr->ramend; AVR_LOG(avr, LOG_TRACE, "%s reset\n", avr->mmcu); avr->state = cpu_Running; - for(int i = 0x20; i <= MAX_IOs; i++) + for(int i = 0x20; i < noof_ios; i++) avr->data[i] = 0; _avr_sp_set(avr, avr->ramend); avr->pc = 0; -- 2.39.5