Skip to content

Commit

Permalink
ExtruderFilaments: Check index of a selected filament to avoid an out…
Browse files Browse the repository at this point in the history
… of range.
  • Loading branch information
YuSanka authored and lukasmatena committed Mar 22, 2024
1 parent 17f628f commit 207d1ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/libslic3r/Preset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,14 @@ const std::string& ExtruderFilaments::get_preset_name_by_alias(const std::string
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*/)
{
std::string name = Preset::remove_suffix_modified(name_w_suffix);
Expand Down
2 changes: 1 addition & 1 deletion src/libslic3r/Preset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ class ExtruderFilaments
// 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
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; }
const Preset* get_selected_preset() const { return m_idx_selected == size_t(-1) ? nullptr : m_extr_filaments[m_idx_selected].preset; }
Expand Down

0 comments on commit 207d1ab

Please sign in to comment.