From 6d511f01a475954d0155f39b39f992273ac5f8cb Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Wed, 11 Nov 2020 17:54:21 -0500 Subject: [PATCH] Ensures intended no-drive behaviour; no more risks with dangling pointers or nullptr. --- Components/DiskII/IWM.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/Components/DiskII/IWM.cpp b/Components/DiskII/IWM.cpp index 17438d2db..b8256001e 100644 --- a/Components/DiskII/IWM.cpp +++ b/Components/DiskII/IWM.cpp @@ -263,7 +263,7 @@ void IWM::run_for(const Cycles cycles) { } break; case ShiftMode::Writing: - if(drives_[active_drive_]->is_writing()) { + if(drives_[active_drive_] && drives_[active_drive_]->is_writing()) { while(cycles_since_shift_ + integer_cycles >= bit_length_) { const auto cycles_until_write = bit_length_ - cycles_since_shift_; drives_[active_drive_]->run_for(cycles_until_write); @@ -293,11 +293,11 @@ void IWM::run_for(const Cycles cycles) { } cycles_since_shift_ = integer_cycles; - if(integer_cycles) { + if(drives_[active_drive_] && integer_cycles) { drives_[active_drive_]->run_for(cycles_since_shift_); } } else { - drives_[active_drive_]->run_for(cycles); + if(drives_[active_drive_]) drives_[active_drive_]->run_for(cycles); } break; @@ -386,16 +386,20 @@ void IWM::propose_shift(uint8_t bit) { void IWM::set_drive(int slot, IWMDrive *drive) { drives_[slot] = drive; - drive->set_event_delegate(this); - drive->set_clocking_hint_observer(this); + if(drive) { + drive->set_event_delegate(this); + drive->set_clocking_hint_observer(this); + } else { + drive_is_rotating_[slot] = false; + } } void IWM::set_component_prefers_clocking(ClockingHint::Source *component, ClockingHint::Preference clocking) { const bool is_rotating = clocking != ClockingHint::Preference::None; - if(component == static_cast(drives_[0])) { + if(drives_[0] && component == static_cast(drives_[0])) { drive_is_rotating_[0] = is_rotating; - } else { + } else if(drives_[1] && component == static_cast(drives_[1])) { drive_is_rotating_[1] = is_rotating; } }