Skip to content

FT_MOTION Fix possible nullptr dereference #27870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Marlin/src/lcd/extui/mks_ui/draw_number_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ static void set_value_confirm() {
#if HAS_Z_AXIS
case ZMaxFeedRate: planner.settings.max_feedrate_mm_s[Z_AXIS] = atof(key_value); break;
#endif
#if HAS_E0_AXIS
#if HAS_EXTRUDERS
case E0MaxFeedRate: planner.settings.max_feedrate_mm_s[E_AXIS] = atof(key_value); break;
#endif
#if HAS_E1_AXIS
#if HAS_MULTI_EXTRUDER
case E1MaxFeedRate: planner.settings.max_feedrate_mm_s[E_AXIS_N(1)] = atof(key_value); break;
#endif

Expand Down
27 changes: 18 additions & 9 deletions Marlin/src/module/ft_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ xyze_long_t FTMotion::steps = { 0 }; // Step count accumulator.

uint32_t FTMotion::interpIdx = 0; // Index of current data point being interpolated.

#if ENABLED(DISTINCT_E_FACTORS)
uint8_t FTMotion::block_extruder_axis; // Cached E Axis from last-fetched block
#else
constexpr uint8_t FTMotion::block_extruder_axis;
#endif

// Shaping variables.
#if HAS_FTM_SHAPING
FTMotion::shaping_t FTMotion::shaping = {
Expand Down Expand Up @@ -143,6 +149,12 @@ void FTMotion::loop() {
continue;
}
loadBlockData(stepper.current_block);

// If the endstop is already pressed, endstop interrupts won't invoke
// endstop_triggered and the move will grind. So check here for a
// triggered endstop, which shortly marks the block for discard.
endstops.update();

blockProcRdy = true;
// Some kinematics track axis motion in HX, HY, HZ
#if ANY(CORE_IS_XY, CORE_IS_XZ, MARKFORGED_XY, MARKFORGED_YX)
Expand Down Expand Up @@ -389,6 +401,7 @@ void FTMotion::reset() {
#endif

TERN_(HAS_EXTRUDERS, e_raw_z1 = e_advanced_z1 = 0.0f);
TERN_(DISTINCT_E_FACTORS, block_extruder_axis = E_AXIS);

axis_move_end_ti.reset();
}
Expand Down Expand Up @@ -453,13 +466,15 @@ void FTMotion::init() {

// Load / convert block data from planner to fixed-time control variables.
void FTMotion::loadBlockData(block_t * const current_block) {
// Cache the extruder index for this block
TERN_(DISTINCT_E_FACTORS, block_extruder_axis = E_AXIS_N(current_block->extruder));

const float totalLength = current_block->millimeters,
oneOverLength = 1.0f / totalLength;

startPosn = endPosn_prevBlock;
const xyze_pos_t moveDist = LOGICAL_AXIS_ARRAY(
current_block->steps.e * planner.mm_per_step[E_AXIS_N(current_block->extruder)] * (current_block->direction_bits.e ? 1 : -1),
current_block->steps.e * planner.mm_per_step[block_extruder_axis] * (current_block->direction_bits.e ? 1 : -1),
current_block->steps.x * planner.mm_per_step[X_AXIS] * (current_block->direction_bits.x ? 1 : -1),
current_block->steps.y * planner.mm_per_step[Y_AXIS] * (current_block->direction_bits.y ? 1 : -1),
current_block->steps.z * planner.mm_per_step[Z_AXIS] * (current_block->direction_bits.z ? 1 : -1),
Expand Down Expand Up @@ -568,12 +583,6 @@ void FTMotion::loadBlockData(block_t * const current_block) {
#endif
TERN_(HAS_Z_AXIS, _SET_MOVE_END(Z));
SECONDARY_AXIS_MAP(_SET_MOVE_END);

// If the endstop is already pressed, endstop interrupts won't invoke
// endstop_triggered and the move will grind. So check here for a
// triggered endstop, which shortly marks the block for discard.
endstops.update();

}

// Generate data points of the trajectory.
Expand Down Expand Up @@ -721,7 +730,7 @@ void FTMotion::convertToSteps(const uint32_t idx) {
#if ENABLED(STEPS_ROUNDING)
#define TOSTEPS(A,B) int32_t(trajMod.A[idx] * planner.settings.axis_steps_per_mm[B] + (trajMod.A[idx] < 0.0f ? -0.5f : 0.5f))
const xyze_long_t steps_tar = LOGICAL_AXIS_ARRAY(
TOSTEPS(e, E_AXIS_N(stepper.current_block->extruder)), // May be eliminated if guaranteed positive.
TOSTEPS(e, block_extruder_axis), // May be eliminated if guaranteed positive.
TOSTEPS(x, X_AXIS), TOSTEPS(y, Y_AXIS), TOSTEPS(z, Z_AXIS),
TOSTEPS(i, I_AXIS), TOSTEPS(j, J_AXIS), TOSTEPS(k, K_AXIS),
TOSTEPS(u, U_AXIS), TOSTEPS(v, V_AXIS), TOSTEPS(w, W_AXIS)
Expand All @@ -730,7 +739,7 @@ void FTMotion::convertToSteps(const uint32_t idx) {
#else
#define TOSTEPS(A,B) int32_t(trajMod.A[idx] * planner.settings.axis_steps_per_mm[B]) - steps.A
xyze_long_t delta = LOGICAL_AXIS_ARRAY(
TOSTEPS(e, E_AXIS_N(stepper.current_block->extruder)),
TOSTEPS(e, block_extruder_axis),
TOSTEPS(x, X_AXIS), TOSTEPS(y, Y_AXIS), TOSTEPS(z, Z_AXIS),
TOSTEPS(i, I_AXIS), TOSTEPS(j, J_AXIS), TOSTEPS(k, K_AXIS),
TOSTEPS(u, U_AXIS), TOSTEPS(v, V_AXIS), TOSTEPS(w, W_AXIS)
Expand Down
6 changes: 6 additions & 0 deletions Marlin/src/module/ft_motion.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ class FTMotion {

static xyze_long_t steps;

#if ENABLED(DISTINCT_E_FACTORS)
static uint8_t block_extruder_axis; // Cached extruder axis index
#else
static constexpr uint8_t block_extruder_axis = E_AXIS;
#endif

// Shaping variables.
#if HAS_FTM_SHAPING

Expand Down
Loading