Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix decoding error in Kvaser constructor for non-ASCII product name #1613

Merged
merged 3 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion can/interfaces/kvaser/canlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,8 @@ def get_channel_info(channel):
ctypes.sizeof(number),
)

return f"{name.value.decode('ascii')}, S/N {serial.value} (#{number.value + 1})"
name_decoded = name.value.decode("ascii", errors="replace")
return f"{name_decoded}, S/N {serial.value} (#{number.value + 1})"


init_kvaser_library()
21 changes: 21 additions & 0 deletions test/test_kvaser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
"""

import ctypes
import time
import unittest
from unittest.mock import Mock
Expand Down Expand Up @@ -49,6 +50,26 @@ def test_bus_creation(self):
self.assertTrue(canlib.canOpenChannel.called)
self.assertTrue(canlib.canBusOn.called)

def test_bus_creation_illegal_channel_name(self):
# Test if the bus constructor is able to deal with non-ASCII characters
def canGetChannelDataMock(
channel: ctypes.c_int,
param: ctypes.c_int,
buf: ctypes.c_void_p,
bufsize: ctypes.c_size_t,
):
if param == constants.canCHANNELDATA_DEVDESCR_ASCII:
buf_char_ptr = ctypes.cast(buf, ctypes.POINTER(ctypes.c_char))
for i, char in enumerate(b"hello\x7a\xcb"):
buf_char_ptr[i] = char

canlib.canGetChannelData = canGetChannelDataMock
bus = can.Bus(channel=0, interface="kvaser")

self.assertTrue(bus.channel_info.startswith("hello"))

bus.shutdown()

def test_bus_shutdown(self):
self.bus.shutdown()
self.assertTrue(canlib.canBusOff.called)
Expand Down