TCGstorageAPI icon indicating copy to clipboard operation
TCGstorageAPI copied to clipboard

Cannot access mSID - UnicodeDecodeError

Open wyllys66 opened this issue 4 years ago • 3 comments

Trying to access the "sed.mSID" value but it always returns an error:

*** UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 69: invalid start byte

OS: Ubuntu 18.04 Python 3.8.10

The client code (running as root) tries to access the device to query for basic information:

from TCGstorageAPI import tcgapi

sed = tcgapi.Sed(device)
print(f'WWN: {sed.wwn}')
print(f'mSID: {sed.mSID}')

----
5764824129321851479
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/TCGstorageAPI/tcgapi.py", line 276, in mSID
    return self.__pysed.mSID
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 69: invalid start byte

This code worked fine before the Python3 conversion. Any suggestions?

wyllys66 avatar Aug 10 '21 21:08 wyllys66

Following up: The bug is in the TcgDrive.cpp Session::dumpPacket function. dump packet tries to turn the packet data into a C-String using "c_str()", however, the packet data is not guaranteed to be UTF-8 (the default for c_str()) and thus will abort when it hits a character that cannot be decoded (0xFF, 0xFE, etc).

Workaround: set "_debugPackets = False" in the python client code.

Fix: Fix the Session::dumpPackets function to properly display the hex bytes from the packet dump without using std::string.c_str().

wyllys66 avatar Aug 13 '21 17:08 wyllys66

TCGstorageAPI/tcgapi.py does not expose the _debugPackets value as a settable property.

So, client code has to use a hacky solution like:

sed._Sed__pysed._debugPackets = False

So, in addition to the dumpPacket problem above, tcgapi.py has a bug in that it doesn't expose the API to enable/disable packet dumps.

wyllys66 avatar Aug 13 '21 17:08 wyllys66

Pull request submitted with fixes.

wyllys66 avatar Aug 16 '21 14:08 wyllys66

Merged pull request

Keerthana-Bidarakoppa avatar Aug 22 '24 22:08 Keerthana-Bidarakoppa