Fix of a crash due to the way how the presets are sorted and searched

for in the PresetCollection: The 1st preset is always the "-- default --"
even if there are some presets starting with an ASCII character lower than '-'.
https://github.com/prusa3d/Slic3r/issues/603
This commit is contained in:
bubnikv 2017-12-06 16:47:53 +01:00
parent 2eeca93a97
commit 75dcdb84b1
2 changed files with 21 additions and 10 deletions

View file

@ -283,10 +283,11 @@ Preset& PresetCollection::load_preset(const std::string &path, const std::string
Preset& PresetCollection::load_preset(const std::string &path, const std::string &name, DynamicPrintConfig &&config, bool select)
{
Preset key(m_type, name);
auto it = std::lower_bound(m_presets.begin(), m_presets.end(), key);
if (it == m_presets.end() || it->name != name)
auto it = this->find_preset_internal(name);
if (it == m_presets.end() || it->name != name) {
// The preset was not found. Create a new preset.
it = m_presets.emplace(it, Preset(m_type, name, false));
}
Preset &preset = *it;
preset.file = path;
preset.config = std::move(config);
@ -301,9 +302,8 @@ void PresetCollection::save_current_preset(const std::string &new_name)
{
// 1) Find the preset with a new_name or create a new one,
// initialize it with the edited config.
Preset key(m_type, new_name, false);
auto it = std::lower_bound(m_presets.begin(), m_presets.end(), key);
if (it != m_presets.end() && it->name == key.name) {
auto it = this->find_preset_internal(new_name);
if (it != m_presets.end() && it->name == new_name) {
// Preset with the same name found.
Preset &preset = *it;
if (preset.is_default)
@ -356,7 +356,7 @@ bool PresetCollection::load_bitmap_default(const std::string &file_name)
Preset* PresetCollection::find_preset(const std::string &name, bool first_visible_if_not_found)
{
Preset key(m_type, name, false);
auto it = std::lower_bound(m_presets.begin(), m_presets.end(), key);
auto it = this->find_preset_internal(name);
// Ensure that a temporary copy is returned if the preset found is currently selected.
return (it != m_presets.end() && it->name == key.name) ? &this->preset(it - m_presets.begin()) :
first_visible_if_not_found ? &this->first_visible() : nullptr;
@ -509,10 +509,9 @@ bool PresetCollection::select_preset_by_name(const std::string &name_w_suffix, b
{
std::string name = Preset::remove_suffix_modified(name_w_suffix);
// 1) Try to find the preset by its name.
Preset key(m_type, name, false);
auto it = std::lower_bound(m_presets.begin(), m_presets.end(), key);
auto it = this->find_preset_internal(name);
size_t idx = 0;
if (it != m_presets.end() && it->name == key.name)
if (it != m_presets.end() && it->name == name)
// Preset found by its name.
idx = it - m_presets.begin();
else {

View file

@ -212,6 +212,18 @@ private:
PresetCollection(const PresetCollection &other);
PresetCollection& operator=(const PresetCollection &other);
// Find a preset in the sorted list of presets.
// The "-- default -- " preset is always the first, so it needs
// to be handled differently.
std::deque<Preset>::iterator find_preset_internal(const std::string &name)
{
Preset key(m_type, name);
auto it = std::lower_bound(m_presets.begin() + 1, m_presets.end(), key);
return (it == m_presets.end() && m_presets.front().name == name) ? m_presets.begin() : it;
}
std::deque<Preset>::const_iterator find_preset_internal(const std::string &name) const
{ return const_cast<PresetCollection*>(this)->find_preset_internal(name); }
// Type of this PresetCollection: TYPE_PRINT, TYPE_FILAMENT or TYPE_PRINTER.
Preset::Type m_type;
// List of presets, starting with the "- default -" preset.