From 19982a5923172d034510b264eec8816e715cffd7 Mon Sep 17 00:00:00 2001 From: Ian Bassi Date: Wed, 18 Nov 2026 21:52:08 -0300 Subject: [PATCH] Guard the lookup of the printed layer count ToolOrdering::tools_for_layer() only asserts that the Z it is given is in the tool ordering, so a release build would read past its end if the layer lists ever disagreed. A layer the tool ordering doesn't hold is now counted, as every layer was before. The comment now says what the count leaves out. --- src/libslic3r/GCode.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index a535f253cd..f25d221227 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -3508,7 +3508,8 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato // object's empty lead-in), so they must not be counted here either or the layer // count in the file disagrees with its layer changes. const bool belt = print.config().belt_printer.value; - // Elsewhere process_layer() skips a layer its tool ordering gives no extruder, such as a tip too thin to print. + // Counts the print_z that process_layer() prints. It skips a layer whose tool ordering has no extruder, such as + // the tip of a cone too thin for a wall. const auto count_printed_layers = [belt](const auto &objects, const ToolOrdering &tool_ordering) { std::vector zs; for (const PrintObject *object : objects) { @@ -3525,7 +3526,13 @@ void GCode::_do_export(Print& print, GCodeOutputStream &file, ThumbnailsGenerato } if (! belt) zs.erase(std::remove_if(zs.begin(), zs.end(), [&tool_ordering](coordf_t z) { - return tool_ordering.empty() || tool_ordering.tools_for_layer(z).extruders.empty(); + if (tool_ordering.empty()) + return true; + // The lookup only asserts that z is in the tool ordering. A layer that is not is counted. + if (z > (tool_ordering.end() - 1)->print_z + EPSILON) + return false; + const LayerTools &layer_tools = tool_ordering.tools_for_layer(z); + return std::abs(layer_tools.print_z - z) < EPSILON && layer_tools.extruders.empty(); }), zs.end()); if (zs.empty())