-
Notifications
You must be signed in to change notification settings - Fork 18.7k
add integer bound check #28790
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
base: master
Are you sure you want to change the base?
add integer bound check #28790
Conversation
It is strange, but not impossible, that this should crash. What platform are you running on and what message are you trying to send? The check needs to be moved to only be done if |
I'm not 100% sure what you mean by platform. I'm only using a simulation which I start with: I encounter the problem if I send the command_long over mavlink using a self written python program with the following parameters:
These parameter values are random, but they should not lead to a crash. I also noticed the same problems using different command, e.g. 195-MAV_CMD_DO_SET_ROI_LOCATION The stack trace is the following:
Thank you for your recommendation regarding the position of the check. I will adjust the code accordingly. |
0074020
to
29928fd
Compare
Thanks for providing the command you used. By platform I mean processor type, operating system name and version, compiler name and version, etc. Ardupilot SITL is usually run on Ubuntu x86_64 so that's the least likely to cause problems. Thanks also for updating the code, but there are a lot of casts in Ardupilot so it's concerning that this one should crash. This is undefined behavior so we are clearly in the wrong, but I'd like to be able to replicate the problem myself and better determine impact on common platforms and what other changes we might need to make. |
Well I expected this to be a semi-exotic platform but sure enough it's easily triggerable on x86_64 (using my Nix flake). You can use mavproxy and the command It also happens on a non-debug build (you selected a debug build with I will discuss this with the team and see if this is the right fix and if we need to do more.
|
I think @peterbarker should look at this, though I think the fix is correct. I would have used constrain_float() though. |
Note that we deliberately ignore SIGFPE on non-SITL. If you want to see what a real flight controller would do in a situation like this then set "SIM_FLOAT_EXCEPT = 0" and SITL will ignore FPE in the same way a real flight controller does. |
Much larger change, but we should probably reject the command in this case. |
29928fd
to
8561dfb
Compare
Done. |
I agree with that. But I'm not sure if I can implement this by my own. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done properly this would be a bool
function with an int32&
return parameter.
OTOH, I didn't do that when I created the function for its intended purpose, cope with NaNs
in the input data.
I'm well-aware of this particular issue. I would note that we do get reports of this happening from time-to-time - but that means that people are actually passing in bad values into SITL from their external control systems. If we merge this then we will no longer provide the rather brutal feedback that we currently do, which is to segfault. That increases the chances that someone's external control software will pass through SITL testing without catching bugs, which is bad.
Better to modify the call chain to be able to return a MAV_RESULT
so we can detect these issues and pass a failure message back to the GCS.
return param *1e7; | ||
float convertedValue = param *1e7; | ||
if (convertedValue < INT32_MIN || convertedValue > INT32_MAX) { | ||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't great behaviour; we don't want vehicles deciding to fly to 0,0 because someone's screwed up on the GCS side and we interpret their bad data as 0,0. We should be rejecting the command.
@@ -5151,7 +5151,11 @@ static int32_t convert_COMMAND_LONG_loc_param(float param, bool stores_location) | |||
} | |||
|
|||
if (stores_location) { | |||
return param *1e7; | |||
float convertedValue = param *1e7; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
float convertedValue = param *1e7; | |
const float convertedValue = param *1e7; |
I've created #28866 to replace this one. I have added @RamReso as a My PR doesn't squash the value to zero when invalid, instead returning a useful return code to the GCS. It also more-tightly validates the latitude/longitudes being passed in - instead of ensure it fits into an int32_t it instead checks they're valid lat/lng, which is a strictly stronger check. @RamReso could you check my PR to see if you like it, please? |
When sending mavlink command_long messages with some arbitrary parameters, I noticed random crashes in the simulation. I looked into it and found, that the simulation always crashes, when I send a parameter >215 or <-215 for a coordinate. This was due to a integer over/underflow in the conversion from long to int.
I added the necessary checks to the converting function and now I do not longer notice the problem.