Skip to content

Commit

Permalink
[Fix] Uint8Array.fromHex: avoid invoking toString on a non-string `…
Browse files Browse the repository at this point in the history
…string` argument

Also:
 - add test coverage from tc39/test262#3994
  • Loading branch information
ljharb committed Feb 28, 2024
1 parent aae0940 commit 85c927f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Uint8Array.fromHex/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = function fromHex(string) {
}

if (typeof string !== 'string') {
throw new $TypeError('`string` is not a string: ' + string); // step 1
throw new $TypeError('`string` is not a string: ' + typeof string); // step 1
}

var result = FromHex(string); // step 2
Expand Down
57 changes: 57 additions & 0 deletions test/Uint8Array.fromHex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

var defineProperties = require('define-properties');
var test = require('tape');
var forEach = require('for-each');
var getProto = require('es-abstract/helpers/getProto');

var index = require('../Uint8Array.fromHex');
var impl = require('../Uint8Array.fromHex/implementation');
Expand Down Expand Up @@ -52,6 +54,61 @@ module.exports = {
'hex (' + hex + ') produces expected bytes (' + expected.join(', ') + ')'
);

var illegal = [
'a.a',
'aa^',
'a a',
'a\ta',
'a\x0Aa',
'a\x0Ca',
'a\x0Da',
'a\u00A0a', // nbsp
'a\u2009a', // thin space
'a\u2028a' // line separator
];
forEach(illegal, function (value) {
st['throws'](
function () { method(value); },
SyntaxError
);
});

var cases = [
['', []],
['66', [102]],
['666f', [102, 111]],
['666F', [102, 111]],
['666f6f', [102, 111, 111]],
['666F6f', [102, 111, 111]],
['666f6f62', [102, 111, 111, 98]],
['666f6f6261', [102, 111, 111, 98, 97]],
['666f6f626172', [102, 111, 111, 98, 97, 114]]
];
forEach(cases, function (pair) {
var arr = method(pair[0]);
st.equal(getProto(arr), Uint8Array.prototype, 'decoding ' + pair[0]);
st.deepEqual(arr, new Uint8Array(pair[1]), 'decoding ' + pair[0]);
});

st.test('test262: test/built-ins/Uint8Array/fromHex/string-coercion.js', function (s2t) {
var throwyToString = {};
var results = s2t.intercept(
throwyToString,
'toString',
{
value: function () { throw new EvalError('toString called'); }
}
);

s2t['throws'](
function () { method(throwyToString); },
TypeError
);
s2t.deepEqual(results(), []);

s2t.end();
});

st.end();
});
},
Expand Down

0 comments on commit 85c927f

Please sign in to comment.