From e5938ac1515462e0673d7530e1afb19f4492d321 Mon Sep 17 00:00:00 2001 From: Julien Vanier Date: Wed, 27 Sep 2023 16:14:56 -0400 Subject: [PATCH] Filter out null bytes from the firmware version returned by the device --- src/device-base.js | 4 +++- src/device-base.test.js | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/device-base.js b/src/device-base.js index 3fd99b2b..ad69f9b5 100644 --- a/src/device-base.js +++ b/src/device-base.js @@ -715,7 +715,9 @@ class DeviceBase extends EventEmitter { wLength: proto.MIN_WLENGTH }; return this._dev.transferIn(setup).then(data => { - return data.toString(); + // filter out null bytes + const nullPos = data.indexOf(0); + return (nullPos === -1 ? data : data.slice(0, nullPos)).toString(); }); } diff --git a/src/device-base.test.js b/src/device-base.test.js index b9d215c8..3576e194 100644 --- a/src/device-base.test.js +++ b/src/device-base.test.js @@ -267,6 +267,12 @@ describe('device-base', () => { expect(dev.firmwareVersion).to.equal('1.0.0'); }); + it('filters out null characters from the firmware version string', async () => { + usbDev.options.firmwareVersion = '1.0.0\x00'; + await dev.open(); + expect(dev.firmwareVersion).to.equal('1.0.0'); + }); + it('filters out non-printable ASCII characters from the device ID string', async () => { usbDev.options.serialNumber = '222222222222222222222222\x00'; await dev.open();