diff --git a/src/slic3r/GUI/ConfigValueFormatter.cpp b/src/slic3r/GUI/ConfigValueFormatter.cpp index 2ae515b0e2..8536e6731d 100644 --- a/src/slic3r/GUI/ConfigValueFormatter.cpp +++ b/src/slic3r/GUI/ConfigValueFormatter.cpp @@ -53,6 +53,8 @@ wxString get_string_from_enum(const std::string& opt_key, const DynamicPrintConf } return _L("Undefined"); } + if (val < 0 || val >= int(names.size())) + return _L("Undefined"); return from_u8(_utf8(names[val])); } @@ -215,6 +217,8 @@ wxString get_string_value(const std::string& opt_key, const DynamicPrintConfig& ; } case coEnums: { + if (!opt_vector || opt_idx >= opt_vector->size()) + return _L("Undefined"); return get_string_from_enum(pure_key, config, pure_key == "top_surface_pattern" || pure_key == "bottom_surface_pattern" || diff --git a/tests/slic3rutils/CMakeLists.txt b/tests/slic3rutils/CMakeLists.txt index 566835f495..b495f35526 100644 --- a/tests/slic3rutils/CMakeLists.txt +++ b/tests/slic3rutils/CMakeLists.txt @@ -2,6 +2,7 @@ get_filename_component(_TEST_NAME ${CMAKE_CURRENT_LIST_DIR} NAME) add_executable(${_TEST_NAME}_tests ${_TEST_NAME}_tests_main.cpp test_bambu_filament_ids.cpp + test_config_value_formatter.cpp test_creality_cfs_match.cpp test_dev_mapping.cpp test_filament_bitmap_utils.cpp diff --git a/tests/slic3rutils/test_config_value_formatter.cpp b/tests/slic3rutils/test_config_value_formatter.cpp new file mode 100644 index 0000000000..671e898ff5 --- /dev/null +++ b/tests/slic3rutils/test_config_value_formatter.cpp @@ -0,0 +1,40 @@ +#include + +#include + +#include "libslic3r/Config.hpp" +#include "libslic3r/PrintConfig.hpp" +#include "slic3r/GUI/ConfigValueFormatter.hpp" + +using namespace Slic3r; +using namespace Slic3r::GUI; + +TEST_CASE("An enum vector index past the end of the vector reads as undefined", "[ConfigValueFormatter][Regression]") +{ + const std::string key = "filament_retract_lift_enforce"; + const ConfigOptionDef* def = print_config_def.get(key); + DynamicPrintConfig config; + config.set_key_value(key, new ConfigOptionEnumsGenericNullable(def->enum_keys_map, 1, ConfigOptionEnumsGenericNullable::nil_value())); + + CHECK(get_string_value(key + "#1", config) == "Undefined"); +} + +TEST_CASE("An enum vector index inside the vector reads as its label", "[ConfigValueFormatter]") +{ + const std::string key = "filament_retract_lift_enforce"; + const ConfigOptionDef* def = print_config_def.get(key); + DynamicPrintConfig config; + config.set_key_value(key, new ConfigOptionEnumsGenericNullable(def->enum_keys_map, 1, def->enum_keys_map->at("Top Only"))); + + CHECK(get_string_value(key + "#0", config).utf8_string() == "Top Only"); +} + +TEST_CASE("An enum value outside the label list reads as undefined", "[ConfigValueFormatter]") +{ + const std::string key = "filament_retract_lift_enforce"; + const ConfigOptionDef* def = print_config_def.get(key); + DynamicPrintConfig config; + config.set_key_value(key, new ConfigOptionEnumsGenericNullable(def->enum_keys_map, 1, int(def->enum_labels.size()))); + + CHECK(get_string_value(key + "#0", config) == "Undefined"); +}