Skip to content

Commit

Permalink
Filter out null bytes from the firmware version returned by the device
Browse files Browse the repository at this point in the history
  • Loading branch information
monkbroc committed Sep 27, 2023
1 parent 5a1dd1f commit e5938ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/device-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}

Expand Down
6 changes: 6 additions & 0 deletions src/device-base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e5938ac

Please sign in to comment.