From 2dae873b5837dc40e9cb1fb2d4de57c6ed990d23 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 7 Aug 2016 22:08:07 -0700 Subject: [PATCH] Fix: treat numbers as unsigned 8-bit for .indexOf and .includes Caught by the new node.js tests --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index de76c7a..3b26822 100644 --- a/index.js +++ b/index.js @@ -708,6 +708,9 @@ Buffer.prototype.indexOf = function indexOf (val, byteOffset, encoding) { return arrayIndexOf(this, val, byteOffset, encoding) } if (typeof val === 'number') { + // Numbers will be interpreted as unsigned 8-bit integer values between + // `0` and `255`. + val &= 0xFF if (Buffer.TYPED_ARRAY_SUPPORT && Uint8Array.prototype.indexOf === 'function') { return Uint8Array.prototype.indexOf.call(this, val, byteOffset) } @@ -735,7 +738,7 @@ function hexWrite (buf, string, offset, length) { // must be an even number of digits var strLen = string.length - if (strLen % 2 !== 0) throw new Error('Invalid hex string') + if (strLen % 2 !== 0) throw new TypeError('Invalid hex string') if (length > strLen / 2) { length = strLen / 2 -- 2.34.1