Skip to content

Commit b99a372

Browse files
addaleaxFishrock123
authored andcommitted
buffer: fix single-character string filling
Fix the fast path for `buffer.fill()` with a single-character string. The fast path only works for strings that are equivalent to a single-byte buffer, but that condition was not checked properly for the `utf8` or `utf16le` encodings and is always true for the `latin1` encoding. This change fixes these problems. Fixes: #9836 PR-URL: #9837 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent d8b6723 commit b99a372

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

lib/buffer.js

+15-11
Original file line numberDiff line numberDiff line change
@@ -672,23 +672,27 @@ Buffer.prototype.fill = function fill(val, start, end, encoding) {
672672
encoding = end;
673673
end = this.length;
674674
}
675-
if (val.length === 1) {
676-
var code = val.charCodeAt(0);
677-
if (code < 256)
678-
val = code;
679-
}
680-
if (val.length === 0) {
681-
// Previously, if val === '', the Buffer would not fill,
682-
// which is rather surprising.
683-
val = 0;
684-
}
675+
685676
if (encoding !== undefined && typeof encoding !== 'string') {
686677
throw new TypeError('encoding must be a string');
687678
}
688-
if (typeof encoding === 'string' && !Buffer.isEncoding(encoding)) {
679+
var normalizedEncoding = internalUtil.normalizeEncoding(encoding);
680+
if (normalizedEncoding === undefined) {
689681
throw new TypeError('Unknown encoding: ' + encoding);
690682
}
691683

684+
if (val.length === 0) {
685+
// Previously, if val === '', the Buffer would not fill,
686+
// which is rather surprising.
687+
val = 0;
688+
} else if (val.length === 1) {
689+
var code = val.charCodeAt(0);
690+
if ((normalizedEncoding === 'utf8' && code < 128) ||
691+
normalizedEncoding === 'latin1') {
692+
// Fast path: If `val` fits into a single byte, use that numeric value.
693+
val = code;
694+
}
695+
}
692696
} else if (typeof val === 'number') {
693697
val = val & 255;
694698
}

test/parallel/test-buffer-fill.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,37 @@ assert.throws(() => {
395395
buf.fill('');
396396
}, /^RangeError: out of range index$/);
397397

398-
399398
assert.deepStrictEqual(
400399
Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'),
401400
Buffer.from('61006200610062006100620061006200', 'hex'));
402401

403402
assert.deepStrictEqual(
404403
Buffer.allocUnsafeSlow(15).fill('ab', 'utf16le'),
405404
Buffer.from('610062006100620061006200610062', 'hex'));
405+
406+
assert.deepStrictEqual(
407+
Buffer.allocUnsafeSlow(16).fill('ab', 'utf16le'),
408+
Buffer.from('61006200610062006100620061006200', 'hex'));
409+
assert.deepStrictEqual(
410+
Buffer.allocUnsafeSlow(16).fill('a', 'utf16le'),
411+
Buffer.from('61006100610061006100610061006100', 'hex'));
412+
413+
assert.strictEqual(
414+
Buffer.allocUnsafeSlow(16).fill('a', 'utf16le').toString('utf16le'),
415+
'a'.repeat(8));
416+
assert.strictEqual(
417+
Buffer.allocUnsafeSlow(16).fill('a', 'latin1').toString('latin1'),
418+
'a'.repeat(16));
419+
assert.strictEqual(
420+
Buffer.allocUnsafeSlow(16).fill('a', 'utf8').toString('utf8'),
421+
'a'.repeat(16));
422+
423+
assert.strictEqual(
424+
Buffer.allocUnsafeSlow(16).fill('Љ', 'utf16le').toString('utf16le'),
425+
'Љ'.repeat(8));
426+
assert.strictEqual(
427+
Buffer.allocUnsafeSlow(16).fill('Љ', 'latin1').toString('latin1'),
428+
'\t'.repeat(16));
429+
assert.strictEqual(
430+
Buffer.allocUnsafeSlow(16).fill('Љ', 'utf8').toString('utf8'),
431+
'Љ'.repeat(8));

0 commit comments

Comments
 (0)