From 1e4c82c781ff8a634cecc9e419377c36f0775492 Mon Sep 17 00:00:00 2001 From: "lane.wei" Date: Wed, 12 Mar 2025 09:05:11 +0800 Subject: [PATCH] FIX: config: fix some potential crash when switch configs switch configs between single extruder and multiple extruders jira: no-jira Change-Id: I3a7ebd590b061f7dec4d8d12d5508e869a941beb --- src/libslic3r/PrintConfig.cpp | 130 ++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 53 deletions(-) diff --git a/src/libslic3r/PrintConfig.cpp b/src/libslic3r/PrintConfig.cpp index 879683540..bd9bd1b58 100644 --- a/src/libslic3r/PrintConfig.cpp +++ b/src/libslic3r/PrintConfig.cpp @@ -6181,57 +6181,68 @@ int DynamicPrintConfig::update_values_from_single_to_multi(DynamicPrintConfig& m switch (optdef->type) { case coStrings: { - ConfigOptionStrings * opt = this->option(key); - ConfigOptionStrings * src_opt = multi_config.option(key); + ConfigOptionStrings* src_opt = multi_config.option(key); + if (src_opt) { + ConfigOptionStrings* opt = this->option(key, true); - opt->values = src_opt->values; + opt->values = src_opt->values; + } break; } case coInts: { - ConfigOptionInts * opt = this->option(key); - ConfigOptionInts * src_opt = multi_config.option(key); + ConfigOptionInts* src_opt = multi_config.option(key); + if (src_opt) { + ConfigOptionInts* opt = this->option(key, true); - opt->values = src_opt->values; + opt->values = src_opt->values; + } break; } case coFloats: { - ConfigOptionFloats * opt = this->option(key); ConfigOptionFloats * src_opt = multi_config.option(key); + if (src_opt) { + ConfigOptionFloats * opt = this->option(key, true); - assert(variant_count == src_opt->size()); - opt->resize(variant_count, opt); + assert(variant_count == src_opt->size()); + opt->resize(variant_count, opt); - for (int index = 0; index < variant_count; index++) - { - if (opt->values[index] > src_opt->values[index]) - opt->values[index] = src_opt->values[index]; + for (int index = 0; index < variant_count; index++) + { + if (opt->values[index] > src_opt->values[index]) + opt->values[index] = src_opt->values[index]; + } } break; } case coFloatsOrPercents: { - ConfigOptionFloatsOrPercents * opt = this->option(key); ConfigOptionFloatsOrPercents * src_opt = multi_config.option(key); + if (src_opt) { + ConfigOptionFloatsOrPercents * opt = this->option(key, true); - assert(variant_count == src_opt->size()); - opt->resize(variant_count, opt); + assert(variant_count == src_opt->size()); + opt->resize(variant_count, opt); - for (int index = 0; index < variant_count; index++) - { - if (opt->values[index].value > src_opt->values[index].value) - opt->values[index] = src_opt->values[index]; + for (int index = 0; index < variant_count; index++) + { + if (opt->values[index].value > src_opt->values[index].value) + opt->values[index] = src_opt->values[index]; + } } break; } case coBools: { - ConfigOptionBools * opt = this->option(key); ConfigOptionBools * src_opt = multi_config.option(key); + if (src_opt) + { + ConfigOptionBools * opt = this->option(key, true); - assert(variant_count == src_opt->size()); - opt->resize(variant_count, opt); + assert(variant_count == src_opt->size()); + opt->resize(variant_count, opt); + } break; } @@ -6271,7 +6282,7 @@ int DynamicPrintConfig::update_values_from_single_to_multi_2(DynamicPrintConfig& ConfigOptionFloatsNullable * opt = this->option(key); ConfigOptionFloatsNullable* src_opt = multi_config.option(key); - if (!opt->is_nil(0)) + if (src_opt && !opt->is_nil(0)) opt->values.resize(src_opt->size(), opt->values[0]); break; } @@ -6280,7 +6291,7 @@ int DynamicPrintConfig::update_values_from_single_to_multi_2(DynamicPrintConfig& ConfigOptionFloatsOrPercentsNullable* opt = this->option(key); ConfigOptionFloatsOrPercentsNullable* src_opt = multi_config.option(key); - if (!opt->is_nil(0)) + if (src_opt &&!opt->is_nil(0)) opt->values.resize(src_opt->size(), opt->values[0]); break; } @@ -6289,7 +6300,7 @@ int DynamicPrintConfig::update_values_from_single_to_multi_2(DynamicPrintConfig& ConfigOptionBoolsNullable* opt = this->option(key); ConfigOptionBoolsNullable* src_opt = multi_config.option(key); - if (!opt->is_nil(0)) + if (src_opt &&!opt->is_nil(0)) opt->values.resize(src_opt->size(), opt->values[0]); break; @@ -6347,64 +6358,77 @@ int DynamicPrintConfig::update_values_from_multi_to_single(DynamicPrintConfig& s switch (optdef->type) { case coStrings: { - ConfigOptionStrings* opt = this->option(key); ConfigOptionStrings* src_opt = single_config.option(key); + if (src_opt) { + ConfigOptionStrings* opt = this->option(key, true); - assert(variant_count == opt->size()); - opt->values = src_opt->values; + assert(variant_count == opt->size()); + opt->values = src_opt->values; + } break; } case coInts: { - ConfigOptionInts* opt = this->option(key); ConfigOptionInts* src_opt = single_config.option(key); + if (src_opt) { + ConfigOptionInts* opt = this->option(key, true); - assert(variant_count == opt->size()); - opt->values = src_opt->values; + assert(variant_count == opt->size()); + opt->values = src_opt->values; + } break; } case coFloats: { - ConfigOptionFloats* opt = this->option(key); ConfigOptionFloats* src_opt = single_config.option(key); - std::vector old_values = opt->values; + if (src_opt) { + ConfigOptionFloats* opt = this->option(key, true); - assert(variant_count == opt->size()); - opt->values = src_opt->values; + std::vector old_values = opt->values; + int old_count = old_values.size(); - for (int i = 0; i < extruder_count; i++) - { - assert(extruder_index[i] != -1); - if (old_values[extruder_index[i]] < opt->values[0]) - opt->values[0] = old_values[extruder_index[i]]; + assert(variant_count == opt->size()); + opt->values = src_opt->values; + + for (int i = 0; i < extruder_count; i++) + { + assert(extruder_index[i] != -1); + if ((old_count > extruder_index[i]) && (old_values[extruder_index[i]] < opt->values[0])) + opt->values[0] = old_values[extruder_index[i]]; + } } break; } case coFloatsOrPercents: { - ConfigOptionFloatsOrPercents* opt = this->option(key); ConfigOptionFloatsOrPercents* src_opt = single_config.option(key); + if (src_opt) { + ConfigOptionFloatsOrPercents* opt = this->option(key, true); - std::vector old_values = opt->values; + std::vector old_values = opt->values; + int old_count = old_values.size(); - assert(variant_count == opt->size()); - opt->values = src_opt->values; + assert(variant_count == opt->size()); + opt->values = src_opt->values; - for (int i = 0; i < extruder_count; i++) - { - assert(extruder_index[i] != -1); - if (old_values[extruder_index[i]].value < opt->values[0].value) - opt->values[0] = old_values[extruder_index[i]]; + for (int i = 0; i < extruder_count; i++) + { + assert(extruder_index[i] != -1); + if ((old_count > extruder_index[i]) && (old_values[extruder_index[i]] < opt->values[0])) + opt->values[0] = old_values[extruder_index[i]]; + } } break; } case coBools: { - ConfigOptionBools* opt = this->option(key); ConfigOptionBools* src_opt = single_config.option(key); + if (src_opt) { + ConfigOptionBools* opt = this->option(key, true); - assert(variant_count == opt->size()); - opt->values = src_opt->values; + assert(variant_count == opt->size()); + opt->values = src_opt->values; + } break; }