fix double-free error (take 2)

This commit is contained in:
bunnie 2023-03-23 21:34:29 +08:00
parent cb5bd20739
commit 80bbdd10a5

View file

@ -550,6 +550,7 @@ fn main() -> ! {
// Note that we start the RTC at somewhere between 0-10 years, so in practice, a user can expect between 90-100 years // Note that we start the RTC at somewhere between 0-10 years, so in practice, a user can expect between 90-100 years
// of continuous uptime service out of the RTC. // of continuous uptime service out of the RTC.
let mut settings = [0u8; 8]; let mut settings = [0u8; 8];
let mut aborted = false;
loop { loop {
// retry loop is necessary because this function can get called during "congested" periods // retry loop is necessary because this function can get called during "congested" periods
i2c.i2c_mutex_acquire(); i2c.i2c_mutex_acquire();
@ -564,7 +565,8 @@ fn main() -> ! {
tt.sleep_ms(38).unwrap(); tt.sleep_ms(38).unwrap();
xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller"); xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller");
continue; aborted = true;
break;
}, },
_ => { _ => {
log::error!("Couldn't read seconds from RTC!"); log::error!("Couldn't read seconds from RTC!");
@ -574,13 +576,19 @@ fn main() -> ! {
tt.sleep_ms(37).unwrap(); // short pause in case the upset was caused by too much activity tt.sleep_ms(37).unwrap(); // short pause in case the upset was caused by too much activity
xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller"); xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller");
continue; aborted = true;
break;
}, },
}; };
} }
// this continue has to be outside the above loop to avoid a double-free error!
if aborted {
continue;
}
log::debug!("GetRtcValue regs: {:?}", settings); log::debug!("GetRtcValue regs: {:?}", settings);
let total_secs: u64; let mut total_secs: u64 = 0;
let mut retries = 0; let mut retries = 0;
aborted = false;
loop { loop {
match rtc_to_seconds(&settings) { match rtc_to_seconds(&settings) {
Some(s) => { Some(s) => {
@ -618,15 +626,21 @@ fn main() -> ! {
// without a valid resolution to the RTC setting! // without a valid resolution to the RTC setting!
log::error!("rtc_to_seconds() never returned a valid value. Returning an error, that may result in a panic..."); log::error!("rtc_to_seconds() never returned a valid value. Returning an error, that may result in a panic...");
xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller"); xous::return_scalar2(msg.sender, 0x8000_0000, 0).expect("couldn't return to caller");
continue; aborted = true;
break;
} else { } else {
log::warn!("rtc_to_seconds() returned an invalid value. Retry #{}", retries); log::warn!("rtc_to_seconds() returned an invalid value. Retry #{}", retries);
} }
} }
xous::return_scalar2(msg.sender, // this continue has to be outside the above loop to avoid the double-free error!
((total_secs >> 32) & 0xFFFF_FFFF) as usize, if aborted || total_secs == 0 {
(total_secs & 0xFFFF_FFFF) as usize, continue;
).expect("couldn't return to caller"); } else {
xous::return_scalar2(msg.sender,
((total_secs >> 32) & 0xFFFF_FFFF) as usize,
(total_secs & 0xFFFF_FFFF) as usize,
).expect("couldn't return to caller");
}
}), }),
#[cfg(not(target_os = "xous"))] #[cfg(not(target_os = "xous"))]
Some(Opcode::GetRtcValue) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { Some(Opcode::GetRtcValue) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {