buffer: remove error for malformatted hex string
Remove error message when a hex string of an incorrect length is sent to .write() or .fill(). PR-URL: https://github.com/nodejs/node/pull/12012 Fixes: https://github.com/nodejs/node/issues/3770 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
This commit is contained in:
parent
a6f94942b0
commit
682573c11d
@ -615,9 +615,6 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
|
|||||||
enc == UTF8 ? str_obj->Utf8Length() :
|
enc == UTF8 ? str_obj->Utf8Length() :
|
||||||
enc == UCS2 ? str_obj->Length() * sizeof(uint16_t) : str_obj->Length();
|
enc == UCS2 ? str_obj->Length() * sizeof(uint16_t) : str_obj->Length();
|
||||||
|
|
||||||
if (enc == HEX && str_length % 2 != 0)
|
|
||||||
return env->ThrowTypeError("Invalid hex string");
|
|
||||||
|
|
||||||
if (str_length == 0)
|
if (str_length == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
@ -685,9 +682,6 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
|
|||||||
|
|
||||||
Local<String> str = args[0]->ToString(env->isolate());
|
Local<String> str = args[0]->ToString(env->isolate());
|
||||||
|
|
||||||
if (encoding == HEX && str->Length() % 2 != 0)
|
|
||||||
return env->ThrowTypeError("Invalid hex string");
|
|
||||||
|
|
||||||
size_t offset;
|
size_t offset;
|
||||||
size_t max_length;
|
size_t max_length;
|
||||||
|
|
||||||
|
@ -510,11 +510,13 @@ assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0);
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test single hex character throws TypeError
|
// Test single hex character is discarded.
|
||||||
// - https://github.com/nodejs/node/issues/6770
|
assert.strictEqual(Buffer.from('A', 'hex').length, 0);
|
||||||
assert.throws(() => Buffer.from('A', 'hex'), TypeError);
|
|
||||||
|
|
||||||
// Test single base64 char encodes as 0
|
// Test that if a trailing character is discarded, rest of string is processed.
|
||||||
|
assert.deepStrictEqual(Buffer.from('Abx', 'hex'), Buffer.from('Ab', 'hex'));
|
||||||
|
|
||||||
|
// Test single base64 char encodes as 0.
|
||||||
assert.strictEqual(Buffer.from('A', 'base64').length, 0);
|
assert.strictEqual(Buffer.from('A', 'base64').length, 0);
|
||||||
|
|
||||||
|
|
||||||
|
@ -132,11 +132,21 @@ testBufs('c8a26161', 8, 1, 'hex');
|
|||||||
testBufs('61c8b462c8b563c8b6', 4, -1, 'hex');
|
testBufs('61c8b462c8b563c8b6', 4, -1, 'hex');
|
||||||
testBufs('61c8b462c8b563c8b6', 4, 1, 'hex');
|
testBufs('61c8b462c8b563c8b6', 4, 1, 'hex');
|
||||||
testBufs('61c8b462c8b563c8b6', 12, 1, 'hex');
|
testBufs('61c8b462c8b563c8b6', 12, 1, 'hex');
|
||||||
// Make sure this operation doesn't go on forever
|
|
||||||
buf1.fill('yKJh', 'hex');
|
|
||||||
assert.throws(() =>
|
|
||||||
buf1.fill('\u0222', 'hex'), /^TypeError: Invalid hex string$/);
|
|
||||||
|
|
||||||
|
{
|
||||||
|
const buf = Buffer.allocUnsafe(SIZE);
|
||||||
|
assert.doesNotThrow(() => {
|
||||||
|
// Make sure this operation doesn't go on forever.
|
||||||
|
buf.fill('yKJh', 'hex');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const buf = Buffer.allocUnsafe(SIZE);
|
||||||
|
assert.doesNotThrow(() => {
|
||||||
|
buf.fill('\u0222', 'hex');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// BASE64
|
// BASE64
|
||||||
testBufs('YWJj', 'ucs2');
|
testBufs('YWJj', 'ucs2');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user