Skip to content

Commit 682573c

Browse files
committed
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: #12012 Fixes: #3770 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent a6f9494 commit 682573c

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/node_buffer.cc

-6
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,6 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
615615
enc == UTF8 ? str_obj->Utf8Length() :
616616
enc == UCS2 ? str_obj->Length() * sizeof(uint16_t) : str_obj->Length();
617617

618-
if (enc == HEX && str_length % 2 != 0)
619-
return env->ThrowTypeError("Invalid hex string");
620-
621618
if (str_length == 0)
622619
return;
623620

@@ -685,9 +682,6 @@ void StringWrite(const FunctionCallbackInfo<Value>& args) {
685682

686683
Local<String> str = args[0]->ToString(env->isolate());
687684

688-
if (encoding == HEX && str->Length() % 2 != 0)
689-
return env->ThrowTypeError("Invalid hex string");
690-
691685
size_t offset;
692686
size_t max_length;
693687

test/parallel/test-buffer-alloc.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -510,11 +510,13 @@ assert.strictEqual(Buffer.from('=bad'.repeat(1e4), 'base64').length, 0);
510510
}
511511
}
512512

513-
// Test single hex character throws TypeError
514-
// - https://github.com/nodejs/node/issues/6770
515-
assert.throws(() => Buffer.from('A', 'hex'), TypeError);
513+
// Test single hex character is discarded.
514+
assert.strictEqual(Buffer.from('A', 'hex').length, 0);
516515

517-
// Test single base64 char encodes as 0
516+
// Test that if a trailing character is discarded, rest of string is processed.
517+
assert.deepStrictEqual(Buffer.from('Abx', 'hex'), Buffer.from('Ab', 'hex'));
518+
519+
// Test single base64 char encodes as 0.
518520
assert.strictEqual(Buffer.from('A', 'base64').length, 0);
519521

520522

test/parallel/test-buffer-fill.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,21 @@ testBufs('c8a26161', 8, 1, 'hex');
132132
testBufs('61c8b462c8b563c8b6', 4, -1, 'hex');
133133
testBufs('61c8b462c8b563c8b6', 4, 1, 'hex');
134134
testBufs('61c8b462c8b563c8b6', 12, 1, 'hex');
135-
// Make sure this operation doesn't go on forever
136-
buf1.fill('yKJh', 'hex');
137-
assert.throws(() =>
138-
buf1.fill('\u0222', 'hex'), /^TypeError: Invalid hex string$/);
139135

136+
{
137+
const buf = Buffer.allocUnsafe(SIZE);
138+
assert.doesNotThrow(() => {
139+
// Make sure this operation doesn't go on forever.
140+
buf.fill('yKJh', 'hex');
141+
});
142+
}
143+
144+
{
145+
const buf = Buffer.allocUnsafe(SIZE);
146+
assert.doesNotThrow(() => {
147+
buf.fill('\u0222', 'hex');
148+
});
149+
}
140150

141151
// BASE64
142152
testBufs('YWJj', 'ucs2');

0 commit comments

Comments
 (0)