mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-24 16:01:53 +08:00
fix(rc_update): store RC-tuned INT32 params as integers (#29067)
set_params_from_rc() computed a float and passed its address to param_set() regardless of the target type. For a PARAM_TYPE_INT32 target the setter copies the four input bytes directly, storing the float's raw IEEE-754 bit pattern instead of the numeric value. Non-finite input is now rejected and finite input is rounded and clamped into the INT32 range (float_to_param_int32) before the cast, so the intended integer is stored without undefined behaviour. The RC_MAP_FLTMODE out-of-bounds read this PR originally also fixed is now handled upstream for every RC_MAP_* function (getChannelIndex() returns -1 for an out-of-range mapping), so that part is dropped on rebase onto main. Add an RCUpdate regression test for float_to_param_int32 rounding and clamping. Assisted-by: Claude:claude-opus-4-8
This commit is contained in:
1 parent
700452758a
commit
79b4e7e469
3 files changed
+38
-1
No files matched your search
@@ -32,6 +32,7 @@
|
||||
****************************************************************************/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstdint>
|
||||
#include "rc_update.h"
|
||||
|
||||
using namespace rc_update;
|
||||
@@ -344,3 +345,15 @@ TEST_F(RCUpdateTest, PayloadPower3WaySwitchNegativeThresholds)
|
||||
checkPayloadPowerSwitch(-0.501f, -0.75f, -0.25f, 1); // Slightly above on_threshold -> SWITCH_POS_ON
|
||||
checkPayloadPowerSwitch(-1.f, -0.75f, -0.25f, 1); // Above on_threshold -> SWITCH_POS_ON
|
||||
}
|
||||
|
||||
TEST(RCUpdateParamConversionTest, FloatToInt32RoundsAndClamps)
|
||||
{
|
||||
// Nominal values round to the nearest integer instead of storing raw float bytes
|
||||
EXPECT_EQ(float_to_param_int32(2.f), 2);
|
||||
EXPECT_EQ(float_to_param_int32(2.6f), 3);
|
||||
EXPECT_EQ(float_to_param_int32(-3.4f), -3);
|
||||
|
||||
// Out-of-range values are clamped into the representable INT32 range (no UB)
|
||||
EXPECT_EQ(float_to_param_int32(1e20f), 2147483520);
|
||||
EXPECT_EQ(float_to_param_int32(-1e20f), INT32_MIN);
|
||||
}
|
||||
@@ -348,7 +348,21 @@ void RCUpdate::set_params_from_rc()
|
||||
_rc_parameter_map.value0[i] + _rc_parameter_map.scale[i] * rc_val,
|
||||
_rc_parameter_map.value_min[i], _rc_parameter_map.value_max[i]);
|
||||
|
||||
param_set(_parameter_handles.rc_param[i], ¶m_val);
|
||||
const param_t handle = _parameter_handles.rc_param[i];
|
||||
|
||||
// RC tuning always computes a float. Convert to the target type so an
|
||||
// INT32 parameter stores the numeric value, not the raw float bytes.
|
||||
if (param_type(handle) == PARAM_TYPE_INT32) {
|
||||
// Reject non-finite input, then convert so an INT32 parameter stores
|
||||
// the numeric value instead of the raw float bytes.
|
||||
if (PX4_ISFINITE(param_val)) {
|
||||
const int32_t int_val = float_to_param_int32(param_val);
|
||||
param_set(handle, &int_val);
|
||||
}
|
||||
|
||||
} else {
|
||||
param_set(handle, ¶m_val);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,6 +65,16 @@ using namespace time_literals;
|
||||
namespace rc_update
|
||||
{
|
||||
|
||||
/**
|
||||
* Convert an RC-tuned value to a well-defined INT32: rounds to nearest and
|
||||
* clamps into the representable INT32 float range so the cast is defined for
|
||||
* any finite input. Callers must reject non-finite values first.
|
||||
*/
|
||||
inline int32_t float_to_param_int32(float value)
|
||||
{
|
||||
return static_cast<int32_t>(math::constrain(roundf(value), -2147483648.f, 2147483520.f));
|
||||
}
|
||||
|
||||
/**
|
||||
** class RCUpdate
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user