Skip to content

Commit 96963d8

Browse files
committed
analogReference: fix heap corruption
on startup, analogReference() is being called to read the real value of 5V voltage so there's a way to retrieve the real voltage via analogRead(). However, calling R_ADC_Close when the ADC is not yet open triggers a chain of events that ends up at __STATIC_INLINE void R_FSP_IsrContextSet (IRQn_Type const irq, void * p_context) { gp_renesas_isr_context[irq] = p_context; } If the irq is not initialized, setting gp_renesas_isr_context[somethig] will overwrite random memory. Fixes #76
1 parent 44e51bd commit 96963d8

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Diff for: cores/arduino/analog.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,15 @@ void analogReference(uint8_t mode) {
545545
ctrl = ADC_VREF_CONTROL_AVCC0_AVSS0; break;
546546
}
547547

548-
R_ADC_Close(&adc.ctrl);
548+
if(adc.ctrl.opened) {
549+
R_ADC_Close(&adc.ctrl);
550+
}
549551
adc.cfg_extend.adc_vref_control = ctrl;
550552
R_ADC_Open(&adc.ctrl, &adc.cfg);
551553

552-
R_ADC_Close(&adc1.ctrl);
554+
if(adc1.ctrl.opened) {
555+
R_ADC_Close(&adc1.ctrl);
556+
}
553557
adc1.cfg_extend.adc_vref_control = ctrl;
554558
R_ADC_Open(&adc1.ctrl, &adc1.cfg);
555559
}
@@ -638,13 +642,17 @@ void analogReadResolution(int bits) {
638642
break;
639643
}
640644

641-
R_ADC_Close(&adc.ctrl);
645+
if(adc.ctrl.opened) {
646+
R_ADC_Close(&adc.ctrl);
647+
}
642648
auto res = R_ADC_Open(&adc.ctrl, &adc.cfg);
643649
if (res != FSP_SUCCESS) {
644650
adc.cfg.resolution = old_read_resolution;
645651
}
646652

647-
R_ADC_Close(&adc1.ctrl);
653+
if(adc1.ctrl.opened) {
654+
R_ADC_Close(&adc1.ctrl);
655+
}
648656
res = R_ADC_Open(&adc1.ctrl, &adc1.cfg);
649657
if (res != FSP_SUCCESS) {
650658
adc1.cfg.resolution = old1_read_resolution;

0 commit comments

Comments
 (0)