From 0e1f8bd08041b8aa6481ad922ff69bd843a642d8 Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Mon, 6 Feb 2023 13:44:01 +0100 Subject: [PATCH] Fixed an UB in CoolingBuffer.cpp: uninitialized var in case that std::from_chars fails: This fixes CoolingBuffer behavior wrt. 2.5.0 in cases where custom G-Codes contained lines starting with "T". Those were previously considered as valid toolchanges. In 2.6.0-alpha3, this scenario led to a hard crash: #9470, #9550 --- src/libslic3r/GCode/CoolingBuffer.cpp | 33 ++++++++++++++------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/libslic3r/GCode/CoolingBuffer.cpp b/src/libslic3r/GCode/CoolingBuffer.cpp index f8e1dc6d7..fb2d12a68 100644 --- a/src/libslic3r/GCode/CoolingBuffer.cpp +++ b/src/libslic3r/GCode/CoolingBuffer.cpp @@ -461,23 +461,24 @@ std::vector CoolingBuffer::parse_layer_gcode(const std:: } active_speed_modifier = size_t(-1); } else if (boost::starts_with(sline, m_toolchange_prefix)) { - unsigned int new_extruder; + unsigned int new_extruder = 0; auto res = std::from_chars(sline.data() + m_toolchange_prefix.size(), sline.data() + sline.size(), new_extruder); - // Only change extruder in case the number is meaningful. User could provide an out-of-range index through custom gcodes - those shall be ignored. - if (new_extruder < map_extruder_to_per_extruder_adjustment.size()) { - if (new_extruder != current_extruder) { - // Switch the tool. - line.type = CoolingLine::TYPE_SET_TOOL; - current_extruder = new_extruder; - adjustment = &per_extruder_adjustments[map_extruder_to_per_extruder_adjustment[current_extruder]]; + if (res.ec != std::errc::invalid_argument) { + // Only change extruder in case the number is meaningful. User could provide an out-of-range index through custom gcodes - those shall be ignored. + if (new_extruder < map_extruder_to_per_extruder_adjustment.size()) { + if (new_extruder != current_extruder) { + // Switch the tool. + line.type = CoolingLine::TYPE_SET_TOOL; + current_extruder = new_extruder; + adjustment = &per_extruder_adjustments[map_extruder_to_per_extruder_adjustment[current_extruder]]; + } + } + else { + // Only log the error in case of MM printer. Single extruder printers likely ignore any T anyway. + if (map_extruder_to_per_extruder_adjustment.size() > 1) + BOOST_LOG_TRIVIAL(error) << "CoolingBuffer encountered an invalid toolchange, maybe from a custom gcode: " << sline; } } - else { - // Only log the error in case of MM printer. Single extruder printers likely ignore any T anyway. - if (map_extruder_to_per_extruder_adjustment.size() > 1) - BOOST_LOG_TRIVIAL(error) << "CoolingBuffer encountered an invalid toolchange, maybe from a custom gcode: " << sline; - } - } else if (boost::starts_with(sline, ";_BRIDGE_FAN_START")) { line.type = CoolingLine::TYPE_BRIDGE_FAN_START; } else if (boost::starts_with(sline, ";_BRIDGE_FAN_END")) { @@ -785,9 +786,9 @@ std::string CoolingBuffer::apply_layer_cooldown( if (line_start > pos) new_gcode.append(pos, line_start - pos); if (line->type & CoolingLine::TYPE_SET_TOOL) { - unsigned int new_extruder; + unsigned int new_extruder = 0; auto res = std::from_chars(line_start + m_toolchange_prefix.size(), line_end, new_extruder); - if (new_extruder != m_current_extruder) { + if (res.ec != std::errc::invalid_argument && new_extruder != m_current_extruder) { m_current_extruder = new_extruder; change_extruder_set_fan(); }