Skip to content

Commit 0e7a534

Browse files
yashhzdamilcarlucas
authored andcommitted
fix(params): reject non-finite values (inf, nan) in parameter files
float() silently accepts "inf", "-inf", "nan", and "infinity" as valid values. When loading .param files, these non-finite values were stored in the parameter dictionary without validation and could be uploaded to the flight controller, causing undefined firmware behavior. Added math.isfinite() check in _validate_parameter() to reject non-finite values with a clear error message. Added BDD test covering all non-finite value variants. Signed-off-by: Yash Goel <yashhzd@users.noreply.github.com> Signed-off-by: YASH GOEL <yashhzd@YASHs-Mac-Studio.local>
1 parent 66b7652 commit 0e7a534

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

ardupilot_methodic_configurator/data_model_par_dict.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import logging
1313
import re
14+
from math import isfinite as math_isfinite
1415
from os import path as os_path
1516
from os import popen as os_popen
1617
from sys import exc_info as sys_exc_info
@@ -197,9 +198,16 @@ def _validate_parameter( # pylint: disable=too-many-arguments, too-many-positio
197198
raise SystemExit(msg)
198199
try:
199200
fvalue = float(value)
201+
if not math_isfinite(fvalue):
202+
msg = _(
203+
"Non-finite parameter value {value!r} (parsed as {fvalue}) for {parameter_name} in {param_file} line {i}"
204+
).format(value=value, fvalue=fvalue, parameter_name=parameter_name, param_file=param_file, i=i)
205+
raise SystemExit(msg)
200206
parameter_dict[parameter_name] = Par(fvalue, comment)
201207
except ValueError as exc:
202-
msg = _("Invalid parameter value {value} in {param_file} line {i}").format(value=value, param_file=param_file, i=i)
208+
msg = _("Invalid parameter value {value!r} in {param_file} line {i}").format(
209+
value=value, param_file=param_file, i=i
210+
)
203211
raise SystemExit(msg) from exc
204212
except OSError as exc:
205213
_exc_type, exc_value, exc_traceback = sys_exc_info()

tests/test_data_model_par_dict.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pytest
2020

2121
import ardupilot_methodic_configurator.data_model_par_dict as par_dict_module
22+
from ardupilot_methodic_configurator import _
2223
from ardupilot_methodic_configurator.data_model_par_dict import Par, ParDict, is_within_tolerance, validate_param_name
2324

2425
# pylint: disable=redefined-outer-name, too-many-lines
@@ -1495,3 +1496,27 @@ def test_parameter_validation_rejects_invalid_values(self) -> None:
14951496
ParDict.load_param_file_into_dict(f.name)
14961497

14971498
os.unlink(f.name)
1499+
1500+
def test_parameter_validation_rejects_non_finite_values(self) -> None:
1501+
"""
1502+
Parameter validation rejects infinite and NaN values.
1503+
1504+
GIVEN: A user has a parameter file containing non-finite values (inf, -inf, nan)
1505+
WHEN: They try to load the parameter file
1506+
THEN: A SystemExit should be raised indicating the value is non-finite
1507+
"""
1508+
non_finite_values = ["inf", "-inf", "nan", "infinity", "-infinity"]
1509+
1510+
for bad_value in non_finite_values:
1511+
content = f"VALID_PARAM,{bad_value}"
1512+
1513+
with tempfile.NamedTemporaryFile(mode="w", suffix=".param", delete=False) as f:
1514+
f.write(content)
1515+
f.flush()
1516+
file_path = f.name
1517+
1518+
try:
1519+
with pytest.raises(SystemExit, match=_("Non-finite parameter value {value}").format(value=bad_value)):
1520+
ParDict.load_param_file_into_dict(file_path)
1521+
finally:
1522+
os.unlink(file_path)

0 commit comments

Comments
 (0)