From d3992c228879826a9cb435e745fe2b8931cd95be Mon Sep 17 00:00:00 2001 From: HanifKoh <76276251+HanifKoh@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:04:32 +0800 Subject: [PATCH] Bound Out-of-Range filament_map and physical_extruder_map Values (#17476) * Treat a filament_map Nozzle Outside the Printer as Nozzle 1 A filament_map entry of 0, a negative number or a number above the nozzle count made Extruder::extruder_id() wrap or run past the end, and print_machine_envelope then took the std::max over the per-extruder machine limits out of bounds on a flavour that emits them. Print::apply now treats an entry outside [1, nozzle count] as nozzle 1, before the variant expansion and the config snapshot, and logs it once. After the clamp the slicing engine only sees in-range values. It works on this print's copy of the config; readers of the plate or project config are not changed, and in-range maps are left alone. * Skip a physical_extruder_map Entry Outside the Per-Nozzle Table on Export The first-filament table built for the start G-code is sized by the nozzle count, and the export wrote it at the index named by physical_extruder_map. An entry outside that range corrupted the heap, and a negative one aborted or hung the export. The value is a firmware tool number that is also emitted elsewhere, so it is left as it is; only the write into the table is bounded. * Treat a filament_map Nozzle Outside the Printer as Nozzle 1 in the CLI With --filament-map-mode Manual on a multi-nozzle profile, the CLI indexed the per-nozzle unprintable-filament sets, and shifted the per-filament printable status, with the raw filament_map value. A value of 0, a negative number or one above the nozzle count read out of bounds and crashed before the print was configured. The checks now use a local copy of the map in which an entry outside [1, nozzle count] is nozzle 1, the same policy Print::apply applies. In-range maps are unchanged. --- src/OrcaSlicer.cpp | 6 ++ src/libslic3r/GCode.cpp | 5 +- src/libslic3r/PrintApply.cpp | 18 ++++++ tests/fff_print/test_multifilament.cpp | 88 ++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 1 deletion(-) diff --git a/src/OrcaSlicer.cpp b/src/OrcaSlicer.cpp index dbbf83b007..8491d67883 100644 --- a/src/OrcaSlicer.cpp +++ b/src/OrcaSlicer.cpp @@ -6854,6 +6854,12 @@ int CLI::run(int argc, char **argv) else filament_maps = part_plate->get_real_filament_maps(m_print_config); + // The checks below index per-nozzle tables with these values; as in Print::apply, + // a nozzle outside [1, nozzle count] means nozzle 1. + for (int &nozzle : filament_maps) + if (nozzle < 1 || nozzle > new_extruder_count) + nozzle = 1; + // Multi-nozzle printers need the per-filament volume assignment as a grouping // input in the manual modes: synthesize it from the per-extruder flow types when // the caller did not provide one (an extruder whose nozzle stats span several diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index de9057ee18..4b2d385e94 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -3731,7 +3731,10 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato std::vector physicial_first_filaments; physicial_first_filaments.resize(filaments.size()); for (int extruder_id = 0; extruder_id < filaments.size(); extruder_id++) { - physicial_first_filaments[config.physical_extruder_map.get_at(extruder_id)] = filaments[extruder_id]; + // The map value is a firmware tool number, so it may legitimately fall outside this table. + const int physical_id = config.physical_extruder_map.get_at(extruder_id); + if (physical_id >= 0 && physical_id < static_cast(physicial_first_filaments.size())) + physicial_first_filaments[physical_id] = filaments[extruder_id]; } filaments = physicial_first_filaments; }; diff --git a/src/libslic3r/PrintApply.cpp b/src/libslic3r/PrintApply.cpp index 233eeb580a..f4d4dedca3 100644 --- a/src/libslic3r/PrintApply.cpp +++ b/src/libslic3r/PrintApply.cpp @@ -1428,6 +1428,24 @@ Print::ApplyStatus Print::apply(const Model &model, DynamicPrintConfig new_full_ BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << ", has_scarf_joint_seam:" << has_scarf_joint_seam; } + // A value outside [1, nozzle count] means nozzle 1. Normalize it here so the slicing engine + // (Extruder::extruder_id(), the machine envelope, the variant expansion and the snapshot below) + // only sees in-range values. This works on this print's copy of the config; readers of the + // plate/project config are not changed. + if (auto* filament_map_opt = new_full_config.option("filament_map")) { + const auto* nozzle_diameter_opt = new_full_config.option("nozzle_diameter"); + const int nozzle_count = nozzle_diameter_opt ? std::max(1, static_cast(nozzle_diameter_opt->values.size())) : 1; + bool out_of_range = false; + for (int& nozzle : filament_map_opt->values) + if (nozzle < 1 || nozzle > nozzle_count) { + nozzle = 1; + out_of_range = true; + } + if (out_of_range) + BOOST_LOG_TRIVIAL(error) << __FUNCTION__ << ": filament_map names a nozzle outside 1.." << nozzle_count + << ", using nozzle 1 for those filaments"; + } + //apply extruder related values std::vector print_variant_index; std::vector> nozzle_volume_types; diff --git a/tests/fff_print/test_multifilament.cpp b/tests/fff_print/test_multifilament.cpp index 7fe71a6348..2b8527c9bc 100644 --- a/tests/fff_print/test_multifilament.cpp +++ b/tests/fff_print/test_multifilament.cpp @@ -2462,3 +2462,91 @@ TEST_CASE("Custom M104 in filament start G-code wins on a T0-only print", "[Mult CHECK(temps_after_t0.front() == slicer_temp); CHECK(temps_after_t0.back() == custom_temp); } + +// Two filaments on a two-nozzle Marlin printer that emits its machine limits, with the second +// filament printing the infill so that its nozzle is used. +static DynamicPrintConfig two_nozzle_map_config(const std::string &filament_map, const std::string &physical_extruder_map = "0") +{ + DynamicPrintConfig config = multifilament_config(2, { + { "gcode_flavor", "marlin2" }, + { "emit_machine_limits_to_gcode", 1 }, + { "single_extruder_multi_material", 0 }, + { "nozzle_diameter", "0.4,0.4" }, + { "extruder_printable_height", "0,0" }, + { "printer_extruder_id", "1,2" }, + { "printer_extruder_variant", "Direct Drive Standard,Direct Drive Standard" }, + { "filament_map_mode", "Manual" }, + { "filament_map", filament_map }, + { "physical_extruder_map", physical_extruder_map }, + { "sparse_infill_filament_id", 2 }, + { "enable_prime_tower", 0 }, + { "skirt_loops", 0 }, + { "brim_type", "no_brim" }, + // the nozzles sit at different offsets, so the body shows which nozzle printed + { "extruder_offset", "0x0,10x5" }, + }); + size_flush_to_nozzles(config); + // one value per nozzle and, for the paired keys, per silent/normal column + for (const char *key : { "machine_max_acceleration_x", "machine_max_acceleration_y", "machine_max_acceleration_z", + "machine_max_acceleration_e", "machine_max_acceleration_extruding", "machine_max_acceleration_retracting", + "machine_max_acceleration_travel", "machine_max_speed_x", "machine_max_speed_y", "machine_max_speed_z", + "machine_max_speed_e", "machine_max_jerk_x", "machine_max_jerk_y", "machine_max_jerk_z", + "machine_max_jerk_e", "machine_max_junction_deviation" }) + config.set_key_value(key, new ConfigOptionFloats({ 500, 0, 1000, 0 })); + return config; +} + +// The G-code without the timestamp line and the config block, which echo the config under test. +static std::string gcode_body(const std::string &gcode) +{ + std::string body; + bool in_config = false; + std::istringstream in(gcode); + for (std::string line; std::getline(in, line);) { + if (line.rfind("; CONFIG_BLOCK_START", 0) == 0) + in_config = true; + if (! in_config && line.rfind("; generated by", 0) != 0) + body += line + "\n"; + if (line.rfind("; CONFIG_BLOCK_END", 0) == 0) + in_config = false; + } + return body; +} + +// A filament_map entry the printer has no nozzle for prints on the first nozzle. +TEST_CASE("A filament_map entry outside the nozzle range prints that filament on the first nozzle", "[MultiFilament]") +{ + const std::string nozzle1 = gcode_body(slice({ cube(20) }, two_nozzle_map_config("1,1"))); + const std::string nozzle2 = gcode_body(slice({ cube(20) }, two_nozzle_map_config("1,2"))); + REQUIRE(nozzle1.find("M201 ") != std::string::npos); + // the nozzles are distinguishable in the body + REQUIRE(nozzle1 != nozzle2); + + const std::string bad_value = GENERATE(as(), "0", "3", "-1", "100"); + DYNAMIC_SECTION("filament 2 mapped to nozzle " << bad_value) { + const std::string body = gcode_body(slice({ cube(20) }, two_nozzle_map_config("1," + bad_value))); + CHECK(body == nozzle1); + CHECK(body != nozzle2); + } +} + +// In-range maps are left as they are. +TEST_CASE("A filament_map within the nozzle range is not changed", "[MultiFilament]") +{ + DynamicPrintConfig config = two_nozzle_map_config("1,2"); + Print print; + init_and_process_print({ cube(20) }, print, config); + CHECK(print.config().filament_map.values == std::vector({ 1, 2 })); +} + +// The map value is a firmware tool number, so a value outside the nozzle range must not index the +// per-nozzle first-filament table. +TEST_CASE("A physical_extruder_map entry outside the nozzle range still exports", "[MultiFilament]") +{ + const std::string map = GENERATE(as(), "0,-1", "0,2", "0,100", "-1,0"); + DYNAMIC_SECTION("physical_extruder_map " << map) { + const std::string gcode = slice({ cube(20) }, two_nozzle_map_config("1,2", map)); + CHECK(gcode.find("; CONFIG_BLOCK_END") != std::string::npos); + CHECK(gcode.find("; EXECUTABLE_BLOCK_END") != std::string::npos); + } +}