-
-
Notifications
You must be signed in to change notification settings - Fork 19.5k
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
Conversation
Glad to see that someone is looking at FT_MOTION. |
Hi! Thanks for the response. Could you explain to me how a nullptr is not possible? I still don't see it (Sorry, maybe I'm slow). |
You're totally right, I look again at the code. In theory, a nullptr dereference could only occur after a runout block. I never have such issue but it seems possible, so this PR could prevent firmware crash. To avoid a new variable, we can use |
Sounds great to me.
I've invited you to have access, since you are the expert on the FTM code. |
In general we should always be looking at So, the extra cached value is fine. It makes sense to keep values from the With FT Motion, the current planner block only needs to live long enough to get converted to the DIR/STEP queue, so we should add comments in the FTM code to indicate places where the |
Could it be related to [BUG] FT_MOTION produces strange sound #27344? |
Description
While looking through the ft_motion code, looking to fix remaining bugs before the next release, I came across some areas that (I believe) could lead to undefined behavior. It is possible someone with a better understanding of this code can correct me if this is wrong, but from what I can see, there are some cases that exist that could lead to null pointer dereferencing and divide-by-zero cases. I may have added more checks than needed, especially if we know for certain these cases can never happen??
I'm hoping someone will double check this for me.
From what I can see, in the loop() function stepper.current_block can be set to nullptr by discard_planner_block_protected() when a block is finished processing (!blockProcRdy). However, the convertToSteps() function, which might still be processing the last batch of that same block (batchRdyForInterp is true), uses stepper.current_block->extruder to get the extruder number for axis_steps_per_mm. If stepper.current_block is nullptr at this point, it will cause a null pointer dereference, leading to a crash or undefined behavior, which could manifest as violent movement or a complete halt.
The other issue is if FTMotion::loadBlockData() receives a planner block (current_block) where current_block->millimeters (totalLength) is zero or extremely small, calculations like oneOverLength = 1.0f / totalLength or spm = totalLength / current_block->step_event_count can result in Inf or NaN.
If this is somehow impossible due to some other code I somehow missed, then we can close this. If not - here are some fixes.
Requirements
FT_MOTION should be enabled
Benefits
Prevents divide by zero (possible NaN) and handles very small or zero length blocks that would otherwise lead to undefined behavior.
Configurations
N/A
Related Issues
Might fix (needs testing):
UPDATE!
For now, limiting this to the more likely case of possible nullptr dereference. The fix is fairly simple, using a cached extruder index in the problem area.