From: Frej Drejhammar Date: Sun, 17 May 2015 14:34:49 +0000 (+0200) Subject: Avoid buffer overrun on smaller AVRs X-Git-Tag: v1.3~47^2 X-Git-Url: https://git.htl-mechatronik.at/public/?a=commitdiff_plain;h=f4a289091ee226cb9a323e2b2796ba706c2fc149;p=sx%2Fsimavr.git 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. --- 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;