From 79b4e7e4697b3cd78506697489c0128f2527a50b Mon Sep 17 00:00:00 2001 From: SounLabs <159518213+SounLabs@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:27:53 +0900 Subject: [PATCH] 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 --- src/modules/rc_update/RCUpdateTest.cpp | 13 +++++++++++++ src/modules/rc_update/rc_update.cpp | 16 +++++++++++++++- src/modules/rc_update/rc_update.h | 10 ++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/modules/rc_update/RCUpdateTest.cpp b/src/modules/rc_update/RCUpdateTest.cpp index 2b51dbaca50..7768fe99d56 100644 --- a/src/modules/rc_update/RCUpdateTest.cpp +++ b/src/modules/rc_update/RCUpdateTest.cpp @@ -32,6 +32,7 @@ ****************************************************************************/ #include +#include #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); +} diff --git a/src/modules/rc_update/rc_update.cpp b/src/modules/rc_update/rc_update.cpp index aa237932452..d8bbbdc864e 100644 --- a/src/modules/rc_update/rc_update.cpp +++ b/src/modules/rc_update/rc_update.cpp @@ -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); + } } } } diff --git a/src/modules/rc_update/rc_update.h b/src/modules/rc_update/rc_update.h index 7fa0fdd6eab..4106d1d1083 100644 --- a/src/modules/rc_update/rc_update.h +++ b/src/modules/rc_update/rc_update.h @@ -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(math::constrain(roundf(value), -2147483648.f, 2147483520.f)); +} + /** ** class RCUpdate *