diff --git a/lib/buffer.js b/lib/buffer.js index 6d4790c04d54701..cda9f735e47aa0a 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1082,7 +1082,8 @@ function _fill(buf, value, offset, end, encoding) { Buffer.prototype.write = function write(string, offset, length, encoding) { // Buffer#write(string); if (offset === undefined) { - // Do nothing + offset = 0; + length = this.length; // Buffer#write(string, encoding) } else if (length === undefined && typeof offset === 'string') { encoding = offset; @@ -1108,16 +1109,19 @@ Buffer.prototype.write = function write(string, offset, length, encoding) { } const len = string.length; - if (len <= 16 && len <= length && (!encoding || encoding === 'ascii' || encoding === 'utf8')) { + if ( + len <= 16 && + len <= length && + (!encoding || encoding === 'ascii' || encoding === 'utf8') + ) { let n = 0; - let pos = offset ?? 0; while (true) { - const code = StringPrototypeCharCodeAt(string, n); + const code = string.charCodeAt(n); if (code >= 128) { break; } - this[pos + n] = code; + this[offset + n] = code; n++; if (n === len) {