From a62bba25087163ad317968f7950e40385ebf2b9e Mon Sep 17 00:00:00 2001 From: Lukas Matena Date: Tue, 10 Sep 2019 11:46:18 +0200 Subject: [PATCH 1/3] CoolingBuffer.cpp: Fixed a crash when encountering an invalid toolchange This can happen if the user enters invalid toolchange through the custom gcodes Such toolchange is now simply ignored by the CoolingBuffer, exporting gcode is NOT stopped, a log error is emitted --- src/libslic3r/GCode/CoolingBuffer.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/libslic3r/GCode/CoolingBuffer.cpp b/src/libslic3r/GCode/CoolingBuffer.cpp index 1aa15ef33..b00bc73eb 100644 --- a/src/libslic3r/GCode/CoolingBuffer.cpp +++ b/src/libslic3r/GCode/CoolingBuffer.cpp @@ -2,6 +2,7 @@ #include "CoolingBuffer.hpp" #include #include +#include #include #include @@ -415,13 +416,22 @@ std::vector CoolingBuffer::parse_layer_gcode(const std:: line.type = CoolingLine::TYPE_EXTRUDE_END; active_speed_modifier = size_t(-1); } else if (boost::starts_with(sline, toolchange_prefix)) { - // Switch the tool. - line.type = CoolingLine::TYPE_SET_TOOL; unsigned int new_extruder = (unsigned int)atoi(sline.c_str() + toolchange_prefix.size()); - if (new_extruder != current_extruder) { - current_extruder = new_extruder; - adjustment = &per_extruder_adjustments[map_extruder_to_per_extruder_adjustment[current_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]]; + } } + 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")) { From 44c3493f7dbfe0538d27a5ce3501c0455e4aa1c1 Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Wed, 11 Sep 2019 08:39:29 +0200 Subject: [PATCH 2/3] GCodeAnalyzer: Fixed a crash when encountering an invalid toolchange --- src/libslic3r/GCode.cpp | 5 +++++ src/libslic3r/GCode/Analyzer.cpp | 11 ++++++++++- src/libslic3r/GCode/Analyzer.hpp | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/libslic3r/GCode.cpp b/src/libslic3r/GCode.cpp index 97da766c9..819dcc56e 100644 --- a/src/libslic3r/GCode.cpp +++ b/src/libslic3r/GCode.cpp @@ -802,6 +802,11 @@ void GCode::_do_export(Print &print, FILE *file) } m_analyzer.set_extruder_offsets(extruder_offsets); + // send extruders count to analyzer to allow it to detect invalid extruder idxs + const ConfigOptionStrings* extruders_opt = dynamic_cast(print.config().option("extruder_colour")); + const ConfigOptionStrings* filamemts_opt = dynamic_cast(print.config().option("filament_colour")); + m_analyzer.set_extruders_count(std::max((unsigned int)extruders_opt->values.size(), (unsigned int)filamemts_opt->values.size())); + // tell analyzer about the gcode flavor m_analyzer.set_gcode_flavor(print.config().gcode_flavor); diff --git a/src/libslic3r/GCode/Analyzer.cpp b/src/libslic3r/GCode/Analyzer.cpp index b28aa558e..89cf9924f 100644 --- a/src/libslic3r/GCode/Analyzer.cpp +++ b/src/libslic3r/GCode/Analyzer.cpp @@ -107,6 +107,11 @@ void GCodeAnalyzer::set_extruder_offsets(const GCodeAnalyzer::ExtruderOffsetsMap m_extruder_offsets = extruder_offsets; } +void GCodeAnalyzer::set_extruders_count(unsigned int count) +{ + m_extruders_count = count; +} + void GCodeAnalyzer::set_gcode_flavor(const GCodeFlavor& flavor) { m_gcode_flavor = flavor; @@ -131,6 +136,7 @@ void GCodeAnalyzer::reset() m_moves_map.clear(); m_extruder_offsets.clear(); + m_extruders_count = 1; } const std::string& GCodeAnalyzer::process_gcode(const std::string& gcode) @@ -520,7 +526,10 @@ void GCodeAnalyzer::_processT(const std::string& cmd) unsigned int id = (unsigned int)::strtol(cmd.substr(1).c_str(), nullptr, 10); if (_get_extruder_id() != id) { - _set_extruder_id(id); + if (id >= m_extruders_count) + BOOST_LOG_TRIVIAL(error) << "GCodeAnalyzer encountered an invalid toolchange, maybe from a custom gcode."; + else + _set_extruder_id(id); // stores tool change move _store_move(GCodeMove::Tool_change); diff --git a/src/libslic3r/GCode/Analyzer.hpp b/src/libslic3r/GCode/Analyzer.hpp index 47f4e92aa..0372d9da7 100644 --- a/src/libslic3r/GCode/Analyzer.hpp +++ b/src/libslic3r/GCode/Analyzer.hpp @@ -108,6 +108,7 @@ private: GCodeReader m_parser; TypeToMovesMap m_moves_map; ExtruderOffsetsMap m_extruder_offsets; + unsigned int m_extruders_count; GCodeFlavor m_gcode_flavor; // The output of process_layer() @@ -117,6 +118,7 @@ public: GCodeAnalyzer(); void set_extruder_offsets(const ExtruderOffsetsMap& extruder_offsets); + void set_extruders_count(unsigned int count); void set_gcode_flavor(const GCodeFlavor& flavor); From 7e5043961a3f50f93d2ce9d47bc7fcafd32a93bd Mon Sep 17 00:00:00 2001 From: Enrico Turri Date: Wed, 11 Sep 2019 08:51:36 +0200 Subject: [PATCH 3/3] Added missing include --- src/libslic3r/GCode/Analyzer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libslic3r/GCode/Analyzer.cpp b/src/libslic3r/GCode/Analyzer.cpp index 89cf9924f..5250c90b0 100644 --- a/src/libslic3r/GCode/Analyzer.cpp +++ b/src/libslic3r/GCode/Analyzer.cpp @@ -7,6 +7,8 @@ #include "../Utils.hpp" #include "Print.hpp" +#include + #include "Analyzer.hpp" #include "PreviewData.hpp"