diff --git a/python/cabledyn/objects.py b/python/cabledyn/objects.py index 12a8c0b3..083e25b3 100644 --- a/python/cabledyn/objects.py +++ b/python/cabledyn/objects.py @@ -494,6 +494,9 @@ def eval_channel(model: CableDyn, token: str) -> float: """Evaluate one OUTPUTS token (the caller holds the model lock).""" if not isinstance(token, str): raise TypeError(f"channel token must be a string; got {type(token).__name__}") + if "\0" in token: + # the library ends a token at its first NUL, so "FairTen1\0x" would read as FairTen1 + raise ValueError(f"channel token must not contain a NUL character; got {token!r}") try: raw = token.strip().encode("ascii") except UnicodeEncodeError: diff --git a/python/tests/test_objects.py b/python/tests/test_objects.py index 6486fc45..99de88b4 100644 --- a/python/tests/test_objects.py +++ b/python/tests/test_objects.py @@ -562,6 +562,16 @@ def test_channel_tokens_must_be_ascii(tmp_path): model.channel("FairTen\u00b91") +@pytest.mark.parametrize("token", ["FairTen1\x00", "FairTen1\x00x", "\x00FairTen1"]) +def test_channel_tokens_must_not_hold_a_nul(tmp_path, token): + # the library reads a token up to its first NUL: "FairTen1\0x" would read as FairTen1 + with CableDyn(_held_chain(tmp_path)) as model: + with pytest.raises(ValueError, match="NUL"): + model.channel(token) + with pytest.raises(ValueError, match="NUL"): + model.channels(["FairTen1", token]) + + def test_zero_length_rod_axis_is_a_value_error(tmp_path, monkeypatch): deck = _write(tmp_path, "rod.dat", _ROD_DECK) with CableDyn(deck) as model: