Skip to content

Commit 45c7caf

Browse files
authored
Check data length sdo server (#338)
1 parent 559274f commit 45c7caf

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

canopen/node/local.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ def set_data(
100100
if check_writable and not obj.writable:
101101
raise SdoAbortedError(0x06010002)
102102

103+
# Check length matches type (length of od variable is in bits)
104+
if obj.data_type in objectdictionary.NUMBER_TYPES and (
105+
not 8 * len(data) == len(obj)
106+
):
107+
raise SdoAbortedError(0x06070010)
108+
103109
# Try callbacks
104110
for callback in self._write_callbacks:
105111
callback(index=index, subindex=subindex, od=obj, data=data)

test/test_local.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ def test_expedited_download(self):
7171
value = self.local_node.sdo[0x2004].raw
7272
self.assertEqual(value, 0xfeff)
7373

74+
def test_expedited_download_wrong_datatype(self):
75+
# Try to write 32 bit in integer16 type
76+
with self.assertRaises(canopen.SdoAbortedError) as error:
77+
self.remote_node.sdo.download(0x2001, 0x0, bytes([10, 10, 10, 10]))
78+
self.assertEqual(error.exception.code, 0x06070010)
79+
# Try to write normal 16 bit word, should be ok
80+
self.remote_node.sdo.download(0x2001, 0x0, bytes([10, 10]))
81+
value = self.remote_node.sdo.upload(0x2001, 0x0)
82+
self.assertEqual(value, bytes([10, 10]))
83+
7484
def test_segmented_download(self):
7585
self.remote_node.sdo[0x2000].raw = "Another cool device"
7686
value = self.local_node.sdo[0x2000].data

0 commit comments

Comments
 (0)