Skip to content

Commit e669d6b

Browse files
committed
Fix issue with truncating unknown data types
1 parent 24df6e8 commit e669d6b

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

canopen/objectdictionary/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from canopen.objectdictionary.datatypes import *
1010
from canopen.objectdictionary.datatypes_24bit import Integer24, Unsigned24
11+
from canopen.utils import pretty_index
1112

1213
logger = logging.getLogger(__name__)
1314

@@ -389,6 +390,13 @@ def decode_raw(self, data: bytes) -> Union[int, float, str, bytes, bytearray]:
389390
# Is this correct?
390391
return data.rstrip(b"\x00").decode("utf_16_le", errors="ignore")
391392
elif self.data_type in self.STRUCT_TYPES:
393+
size = self.STRUCT_TYPES[self.data_type].size
394+
if len(data) > size:
395+
logger.warning("Excessive data in %s. Data type 0x%X expects %s bytes, got %s",
396+
pretty_index(self.index, self.subindex), self.data_type,
397+
size, len(data))
398+
# Truncate the data to the expected size
399+
data = data[:size]
392400
try:
393401
value, = self.STRUCT_TYPES[self.data_type].unpack(data)
394402
return value

canopen/sdo/client.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,7 @@ def upload(self, index: int, subindex: int) -> bytes:
115115
When node responds with an error.
116116
"""
117117
with self.open(index, subindex, buffering=0) as fp:
118-
response_size = fp.size
119-
data = fp.read()
120-
121-
# If size is available through variable in OD, then use the smaller of the two sizes.
122-
# Some devices send U32/I32 even if variable is smaller in OD
123-
var = self.od.get_variable(index, subindex)
124-
if var is not None:
125-
# Found a matching variable in OD
126-
# If this is a data type (string, domain etc) the size is
127-
# unknown anyway so keep the data as is
128-
if var.data_type not in objectdictionary.DATA_TYPES:
129-
# Get the size in bytes for this variable
130-
var_size = len(var) // 8
131-
if response_size is None or var_size < response_size:
132-
# Truncate the data to specified size
133-
data = data[0:var_size]
134-
return data
118+
return fp.read()
135119

136120
def download(
137121
self,

0 commit comments

Comments
 (0)