From 1a2973a2de48ebe888054dc24f06959b0965f47b Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Thu, 6 Aug 2015 20:04:42 +0200 Subject: [PATCH] analog-avr.c: take the opportunity to rewrite analog_read() here. The former implementation easily led to memory corruption, because no range check happened. Costs 22 bytes binary size, unfortunately. --- analog-avr.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/analog-avr.c b/analog-avr.c index c5bdf5c..2c34a22 100644 --- a/analog-avr.c +++ b/analog-avr.c @@ -82,23 +82,28 @@ ISR(ADC_vect, ISR_NOBLOCK) { } } -/*! Read analog value from saved result array - \param channel Channel to be read - \return analog reading, 10-bit right aligned +/** Read analog value from saved result array. + + \param channel Channel to be read. Channel numbering starts at zero. + + \return Analog reading, 10-bit right aligned. */ -uint16_t analog_read(uint8_t index) { - if (analog_mask > 0) { - uint16_t r; +uint16_t analog_read(uint8_t index) { + uint16_t result = 0; + + #ifdef AIO8_PIN + if (index < sizeof(adc_channel) && adc_channel[index] < 16) { + #else + if (index < sizeof(adc_channel) && adc_channel[index] < 8) { + #endif ATOMIC_START - // atomic 16-bit copy - r = adc_result[index]; + // Atomic 16-bit copy. + result = adc_result[index]; ATOMIC_END + } - return r; - } else { - return 0; - } + return result; } #endif /* defined TEACUP_C_INCLUDE && defined __AVR__ */