Fix out-of-bounds config reads in the Type1 prime tower (#15183)

Several per-filament and per-extruder options default to a single value. When a config lists fewer values than there are filaments or nozzles, the Type1 prime tower and the pre-heat injector read past the end of the vector, and the same scene could slice to different G-code from one run to the next.

- Read `filament_change_length`, `filament_change_length_nc` and `hotend_cooling_rate` with the same fallback to the first value that `get_at()` uses.
- `is_nil(idx)` falls back to the first value like `get_at()` instead of reading past the end.
- Skip the pre-cooling and last-layer reads when `get_extruder_id()` returns -1, and when no wall filament matches the selected category.
- Bounds-check the pre-heat injector's per-filament and per-extruder reads, and use the extruder index when `physical_extruder_map` is too short.

Configs that list a value for every filament and nozzle produce the same G-code as before.

Adds a test that nozzle-change ramming extrudes the same length in both directions when `filament_change_length` has one value for two filaments.
This commit is contained in:
Eric Lin authored and GitHub committed 2026-10-24 16:21:32 -05:00
1 parent f2037c1d48
commit a07bc8ff5b
4 files changed
+127 -25

No files matched your search

+14 -6
View File
@@ -2203,6 +2203,14 @@ void GCodeProcessor::PreCoolingInjector::build_extruder_free_blocks(const std::v
build_by_extruder_blocks(extruder_usage_blocks);
}
// ConfigOptionVector::get_at() for the vectors copied out of the config.
template<class T> static T value_at_or_front(const std::vector<T>& v, int i, T fallback)
{
if (v.empty())
return fallback;
return i >= 0 && i < (int) v.size() ? v[i] : v.front();
}
// The core injector. Measures the idle-window duration from move.time[valid_machine_id], bails if it is
// below the threshold, and emits pre-cool M104 at the window start and pre-heat M104 (relocated out of
// SKIPPABLE blocks) at the back-solved heating-start move.
@@ -2214,13 +2222,13 @@ void GCodeProcessor::PreCoolingInjector::inject_cooling_heating_command(TimeProc
};
auto is_pre_cooling_valid = [&nozzle_temps = this->filament_nozzle_temps, &pre_cooling_temps = this->filament_pre_cooling_temps](int idx) -> bool {
if (idx < 0)
if (idx < 0 || idx >= (int) pre_cooling_temps.size() || idx >= (int) nozzle_temps.size())
return false;
return pre_cooling_temps[idx] > 0 && pre_cooling_temps[idx] < nozzle_temps[idx];
};
auto get_partial_free_cooling_thres = [&](int idx) -> float {
if (idx < 0)
if (idx < 0 || idx >= (int) filament_nozzle_temps.size() || idx >= (int) filament_pre_cooling_temps.size())
return 30.f;
float temp_in_tower = filament_nozzle_temps[idx];
return temp_in_tower - (float) (filament_pre_cooling_temps[idx]);
@@ -2319,8 +2327,8 @@ void GCodeProcessor::PreCoolingInjector::inject_cooling_heating_command(TimeProc
return;
int extruder_id = get_valid_extruder_id(block.last_nozzle_id);
float ext_heating_rate = heating_rate[extruder_id];
float ext_cooling_rate = cooling_rate[extruder_id];
float ext_heating_rate = value_at_or_front(heating_rate, extruder_id, 1.0);
float ext_cooling_rate = value_at_or_front(cooling_rate, extruder_id, 1.0);
auto add_M104_lines = [&](int gcode_id, int target_extruder, int target_temp, int target_filament, bool skippable, int next_filament_idx, int next_nozzle_id, TimeProcessor::InsertLineType type, const std::string& comment = std::string()) {
auto format_line_M104 = [&](int target_extruder, int target_temp, int target_filament, bool skippable, int next_filament_idx, int next_nozzle_id, const std::string& comment = std::string()) -> std::vector<std::string> {
@@ -2330,7 +2338,7 @@ void GCodeProcessor::PreCoolingInjector::inject_cooling_heating_command(TimeProc
std::string m632_line = "M632 S" + std::to_string(next_filament_idx);
if (support_dynamic_nozzle_map)
m632_line += " H" + std::to_string(next_nozzle_id);
if (extruder_max_nozzle_count[target_extruder] > 1)
if (value_at_or_front(extruder_max_nozzle_count, target_extruder, 1) > 1)
m632_line += " N R";
m632_line += " W\n";
buffer.emplace_back(std::move(m632_line));
@@ -2340,7 +2348,7 @@ void GCodeProcessor::PreCoolingInjector::inject_cooling_heating_command(TimeProc
if (handle_hotend_as_extruder) {
M104_line += (" I" + std::to_string(target_filament == -1 ? next_filament_idx : target_filament));
} else if (target_extruder != -1) {
M104_line += (" T" + std::to_string(physical_extruder_map[target_extruder]));
M104_line += (" T" + std::to_string(target_extruder < (int) physical_extruder_map.size() ? physical_extruder_map[target_extruder] : target_extruder));
}
M104_line += " S" + std::to_string(target_temp);