Skip to content

Commit

Permalink
Added proper string sizing incl. null terminator
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoRoos committed Sep 13, 2024
1 parent 904bc39 commit c08691d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pyads/pyads_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,10 @@ def adsSyncReadReqEx2(
index_group_c = ctypes.c_ulong(index_group)
index_offset_c = ctypes.c_ulong(index_offset)

if type_is_string(data_type):
data = (STRING_BUFFER * PLCTYPE_STRING)()
elif type_is_wstring(data_type):
if type_is_wstring(data_type):
data = (STRING_BUFFER * ctypes.c_uint8)()
else:
# Regular string types already contain size too, rely on this type:
data = data_type()

data_pointer = ctypes.pointer(data)
Expand All @@ -861,11 +860,11 @@ def adsSyncReadReqEx2(
if error_code:
raise ADSError(error_code)

# If we're reading a value of predetermined size (anything but a string or wstring),
# validate that the correct number of bytes were read
# If we're reading a value of predetermined size (anything but wstring, regular
# strings also contain size), validate that the correct number of bytes were read
if (
check_length
and not(type_is_string(data_type) or type_is_wstring(data_type))
and not type_is_wstring(data_type)
and bytes_read.value != data_length.value
):
raise RuntimeError(
Expand Down
4 changes: 4 additions & 0 deletions pyads/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ def get_type_from_str(type_str: str) -> Optional[Type[PLCDataType]]:
scalar_type = AdsSymbol.get_type_from_str(scalar_type_str)

if scalar_type:
if scalar_type == constants.PLCTYPE_STRING:
# E.g. `STRING(80)` actually has a size of 81 including the string
# terminator, so add one to the size
size = size + 1
return scalar_type * size

# We allow unmapped types at this point - Instead we will throw an
Expand Down

0 comments on commit c08691d

Please sign in to comment.