From d3ae2e76e39fd2b6417e90ffd20ee8f02c7519fc Mon Sep 17 00:00:00 2001 From: VolMax-Studio Date: Mon, 16 Nov 2026 22:13:46 +0100 Subject: [PATCH] Reject out-of-range values when packing narrow integers Signed-off-by: VolMax-Studio --- canopen/objectdictionary/datatypes.py | 15 +++++++-- test/test_od.py | 47 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/canopen/objectdictionary/datatypes.py b/canopen/objectdictionary/datatypes.py index e4afd673..985a6b6a 100644 --- a/canopen/objectdictionary/datatypes.py +++ b/canopen/objectdictionary/datatypes.py @@ -1,3 +1,4 @@ +import operator import struct @@ -83,7 +84,11 @@ def unpack(self, buffer): return super().unpack(buffer + b'\x00' * (super().size - self.size)) def pack(self, *v): - return super().pack(*v)[:self.size] + packed = super().pack(*v) + value = operator.index(v[0]) + if not 0 <= value < (1 << self.width): + raise struct.error("unsigned value out of range for data type") + return packed[:self.size] @property def size(self) -> int: @@ -120,7 +125,13 @@ def unpack(self, buffer): ) def pack(self, *v): - return super().pack(*v)[:self.size] + packed = super().pack(*v) + value = operator.index(v[0]) + lower = -(1 << (self.width - 1)) + upper = 1 << (self.width - 1) + if not lower <= value < upper: + raise struct.error("signed value out of range for data type") + return packed[:self.size] @property def size(self) -> int: diff --git a/test/test_od.py b/test/test_od.py index 439d376f..5d51605f 100644 --- a/test/test_od.py +++ b/test/test_od.py @@ -1,3 +1,4 @@ +import struct import unittest from canopen import objectdictionary as od @@ -5,6 +6,52 @@ class TestDataConversions(unittest.TestCase): + def test_nonstandard_unsigned_width_rejects_overflow(self): + from canopen.objectdictionary.datatypes import UnsignedN + for width in (24, 40, 48, 56): + with self.subTest(width=width): + datatype = UnsignedN(width) + max_value = (1 << width) - 1 + self.assertEqual(datatype.unpack(datatype.pack(max_value))[0], max_value) + self.assertEqual(datatype.unpack(datatype.pack(0))[0], 0) + with self.assertRaises(struct.error): + datatype.pack(1 << width) + with self.assertRaises(struct.error): + datatype.pack(-1) + + def test_nonstandard_signed_width_rejects_overflow(self): + from canopen.objectdictionary.datatypes import IntegerN + for width in (24, 40, 48, 56): + with self.subTest(width=width): + datatype = IntegerN(width) + min_value = -(1 << (width - 1)) + max_value = (1 << (width - 1)) - 1 + self.assertEqual(datatype.unpack(datatype.pack(min_value))[0], min_value) + self.assertEqual(datatype.unpack(datatype.pack(max_value))[0], max_value) + with self.assertRaises(struct.error): + datatype.pack(max_value + 1) + with self.assertRaises(struct.error): + datatype.pack(min_value - 1) + + + def test_variable_encoding_rejects_narrow_width_overflow(self): + unsigned = (od.UNSIGNED24, od.UNSIGNED40, od.UNSIGNED48, od.UNSIGNED56) + signed = (od.INTEGER24, od.INTEGER40, od.INTEGER48, od.INTEGER56) + for width, data_type in zip((24, 40, 48, 56), unsigned): + with self.subTest(kind="unsigned", width=width): + var = od.ODVariable("Narrow unsigned", 0x1000) + var.data_type = data_type + with self.assertRaisesRegex(ValueError, "Value does not fit"): + var.encode_raw(1 << width) + for width, data_type in zip((24, 40, 48, 56), signed): + with self.subTest(kind="signed", width=width): + var = od.ODVariable("Narrow signed", 0x1000) + var.data_type = data_type + with self.assertRaisesRegex(ValueError, "Value does not fit"): + var.encode_raw(1 << (width - 1)) + with self.assertRaisesRegex(ValueError, "Value does not fit"): + var.encode_raw(-(1 << (width - 1)) - 1) + def test_boolean(self): var = od.ODVariable("Test BOOLEAN", 0x1000) var.data_type = od.BOOLEAN