buffer: ensure zero-fill for Buffer.alloc(size,'')

This is applicable to v4.x only.

Native Fill method is called from Buffer.alloc and from Buffer#fill,
the second one is not affected by this, as Buffer#fill only calls the
native method on either numbers as the second argument or
non-zero-length strings.

Fixes: https://github.com/nodejs-private/security/issues/192
PR-URL: https://github.com/nodejs-private/node-private/pull/118
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Vladimir de Turckheim <vlad2t@hotmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Сковорода Никита Андреевич 2018-04-24 07:03:19 +03:00
parent 215b42132b
commit 9e5fe8eebd
2 changed files with 23 additions and 1 deletions

View File

@ -612,8 +612,10 @@ void Fill(const FunctionCallbackInfo<Value>& args) {
size_t in_there = str_length; size_t in_there = str_length;
char* ptr = ts_obj_data + start + str_length; char* ptr = ts_obj_data + start + str_length;
if (str_length == 0) if (str_length == 0) {
memset(ts_obj_data + start, 0, length);
return; return;
}
memcpy(ts_obj_data + start, *str, MIN(str_length, length)); memcpy(ts_obj_data + start, *str, MIN(str_length, length));

View File

@ -0,0 +1,20 @@
'use strict';
require('../common');
const assert = require('assert');
for (const fill of [
'',
[],
Buffer.from(''),
new Uint8Array(0),
{ toString: () => '' },
{ toString: () => '', length: 10 }
]) {
for (let i = 0; i < 50; i++) {
const buf = Buffer.alloc(100, fill);
assert.strictEqual(buf.length, 100);
for (let n = 0; n < buf.length; n++)
assert.strictEqual(buf[n], 0);
}
}