From: Tormod V. <lis...@gm...> - 2012-03-28 21:47:21
|
From: Tormod Volden <deb...@gm...> Use the length as reported by the first byte of the descriptor. Otherwise (at least for some backend/kernel combinations) the buffer length provided by the user would be used instead, and NUL characters would be appended to the returned string. This also makes the legacy getString() function compatible with pyusb 0.4. Signed-off-by: Tormod Volden <deb...@gm...> --- Hi, Another patch, originally to fix legacy compatibility issues but probably makes sense for "native" use as well. It could be discussed whether get_descriptor() should be fixed instead to only return the number of bytes defined by the descriptor itself, in which case this patch would not be needed. That would be more intrusive though. On the other hand it seems that this bug does not appear on all my systems although I am pretty sure pyusb and libusb versions are the same, so I wonder if the different linux kernel versions make up for the difference. In any case, this patch should not hurt. Best regards, Tormod usb/util.py | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/usb/util.py b/usb/util.py index 1f8cee3..bb8fc65 100644 --- a/usb/util.py +++ b/usb/util.py @@ -224,7 +224,7 @@ def get_string(dev, length, index, langid = None): dev is the Device object to which the request will be sent to. - length is the length of string in number of characters. + length is the maximum length of the string in number of characters. index is the string descriptor index and langid is the Language ID of the descriptor. If langid is omitted, the string descriptor @@ -257,4 +257,4 @@ def get_string(dev, length, index, langid = None): index, langid ) - return buf[2:].tostring().decode('utf-16-le') + return buf[2:buf[0]].tostring().decode('utf-16-le') -- 1.7.0.4 |