Permalink
Browse files

buffer: fix backwards incompatibility

4a86803 introduced a backwards incompatibility by accident
and was not caught due to an existing test that wasn't strict enough.

This commit fixes both the backwards incompatibility and the test.

PR-URL: #12439
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information...
mscdex authored and jasnell committed Apr 16, 2017
1 parent 2519757 commit 7cd0d4f644e304d4c8239dca532ca4de50d981ba
Showing with 3 additions and 5 deletions.
  1. +1 −4 lib/buffer.js
  2. +2 −1 test/parallel/test-buffer-alloc.js
View
@@ -456,10 +456,7 @@ function byteLength(string, encoding) {
return len >>> 1;
break;
}
if (mustMatch)
throw new TypeError('Unknown encoding: ' + encoding);
else
return binding.byteLengthUtf8(string);
return (mustMatch ? -1 : binding.byteLengthUtf8(string));
}
Buffer.byteLength = byteLength;
@@ -885,7 +885,8 @@ assert.throws(() => Buffer.allocUnsafe(8).writeFloatLE(0.0, -1), RangeError);
}
// Regression test for #5482: should throw but not assert in C++ land.
assert.throws(() => Buffer.from('', 'buffer'), TypeError);
assert.throws(() => Buffer.from('', 'buffer'),
/^TypeError: "encoding" must be a valid string encoding$/);
// Regression test for #6111. Constructing a buffer from another buffer
// should a) work, and b) not corrupt the source buffer.

0 comments on commit 7cd0d4f

Please sign in to comment.