ExtruderFilaments: Check index of a selected filament to avoid an out of range.

This commit is contained in:
YuSanka 2024-02-16 12:40:05 +01:00 committed by Lukas Matena
parent 17f628fe52
commit 207d1ab026
2 changed files with 9 additions and 1 deletions

View File

@ -2235,6 +2235,14 @@ const std::string& ExtruderFilaments::get_preset_name_by_alias(const std::string
return alias; return alias;
} }
void ExtruderFilaments::select_filament(size_t idx)
{
assert(idx == size_t(-1) || idx < m_extr_filaments.size());
// Check idx befor saving it's value to m_idx_selected.
// Invalidate m_idx_selected, if idx is out of range m_extr_filaments
m_idx_selected = (idx == size_t(-1) || idx < m_extr_filaments.size()) ? idx : size_t(-1);
}
bool ExtruderFilaments::select_filament(const std::string &name_w_suffix, bool force/*= false*/) bool ExtruderFilaments::select_filament(const std::string &name_w_suffix, bool force/*= false*/)
{ {
std::string name = Preset::remove_suffix_modified(name_w_suffix); std::string name = Preset::remove_suffix_modified(name_w_suffix);

View File

@ -906,7 +906,7 @@ public:
// Select filament by the full filament name, which contains name of filament, separator and name of selected preset // Select filament by the full filament name, which contains name of filament, separator and name of selected preset
// If full_name doesn't contain name of selected preset, then select first preset in the list for this filament // If full_name doesn't contain name of selected preset, then select first preset in the list for this filament
bool select_filament(const std::string& name, bool force = false); bool select_filament(const std::string& name, bool force = false);
void select_filament(size_t idx) { m_idx_selected = idx; } void select_filament(size_t idx);
std::string get_selected_preset_name() const { return m_idx_selected == size_t(-1) ? std::string() : m_extr_filaments[m_idx_selected].preset->name; } std::string get_selected_preset_name() const { return m_idx_selected == size_t(-1) ? std::string() : m_extr_filaments[m_idx_selected].preset->name; }
const Preset* get_selected_preset() const { return m_idx_selected == size_t(-1) ? nullptr : m_extr_filaments[m_idx_selected].preset; } const Preset* get_selected_preset() const { return m_idx_selected == size_t(-1) ? nullptr : m_extr_filaments[m_idx_selected].preset; }