@@ -52,13 +52,16 @@ In versions of Node.js prior to v6, `Buffer` instances were created using the
52
52
differently based on what arguments are provided:
53
53
54
54
* Passing a number as the first argument to ` Buffer() ` (e.g. ` new Buffer(10) ` ),
55
- allocates a new ` Buffer ` object of the specified size. The memory allocated
56
- for such ` Buffer ` instances is * not* initialized and * can contain sensitive
57
- data* . Such ` Buffer ` instances * must* be initialized * manually* by using either
58
- [ ` buf.fill(0) ` ] [ `buf.fill()` ] or by writing to the ` Buffer ` completely. While
59
- this behavior is * intentional* to improve performance, development experience
60
- has demonstrated that a more explicit distinction is required between creating
61
- a fast-but-uninitialized ` Buffer ` versus creating a slower-but-safer ` Buffer ` .
55
+ allocates a new ` Buffer ` object of the specified size. Prior to Node.js 8.0.0,
56
+ the memory allocated for such ` Buffer ` instances is * not* initialized and
57
+ * can contain sensitive data* . Such ` Buffer ` instances * must* be subsequently
58
+ initialized by using either [ ` buf.fill(0) ` ] [ `buf.fill()` ] or by writing to the
59
+ ` Buffer ` completely. While this behavior is * intentional* to improve
60
+ performance, development experience has demonstrated that a more explicit
61
+ distinction is required between creating a fast-but-uninitialized ` Buffer `
62
+ versus creating a slower-but-safer ` Buffer ` . Starting in Node.js 8.0.0,
63
+ ` Buffer(num) ` and ` new Buffer(num) ` will return a ` Buffer ` with initialized
64
+ memory.
62
65
* Passing a string, array, or ` Buffer ` as the first argument copies the
63
66
passed object's data into the ` Buffer ` .
64
67
* Passing an [ ` ArrayBuffer ` ] returns a ` Buffer ` that shares allocated memory with
@@ -427,6 +430,9 @@ console.log(buf2.toString());
427
430
<!-- YAML
428
431
deprecated: v6.0.0
429
432
changes:
433
+ - version: v8.0.0
434
+ pr-url: https://github.com/nodejs/node/pull/12141
435
+ description: new Buffer(size) will return zero-filled memory by default.
430
436
- version: v7.2.1
431
437
pr-url: https://github.com/nodejs/node/pull/9529
432
438
description: Calling this constructor no longer emits a deprecation warning.
@@ -444,21 +450,17 @@ Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
444
450
[ ` buffer.kMaxLength ` ] or smaller than 0, a [ ` RangeError ` ] will be thrown.
445
451
A zero-length ` Buffer ` will be created if ` size ` is 0.
446
452
447
- Unlike [ ` ArrayBuffers ` ] [ `ArrayBuffer` ] , the underlying memory for ` Buffer ` instances
448
- created in this way is * not initialized* . The contents of a newly created ` Buffer `
449
- are unknown and * could contain sensitive data* . Use
450
- [ ` Buffer.alloc(size) ` ] [ `Buffer.alloc()` ] instead to initialize a ` Buffer ` to zeroes.
453
+ Prior to Node.js 8.0.0, the underlying memory for ` Buffer ` instances
454
+ created in this way is * not initialized* . The contents of a newly created
455
+ ` Buffer ` are unknown and * may contain sensitive data* . Use
456
+ [ ` Buffer.alloc(size) ` ] [ `Buffer.alloc()` ] instead to initialize a ` Buffer `
457
+ to zeroes.
451
458
452
459
Example:
453
460
454
461
``` js
455
462
const buf = new Buffer (10 );
456
463
457
- // Prints: (contents may vary): <Buffer 48 21 4b 00 00 00 00 00 30 dd>
458
- console .log (buf);
459
-
460
- buf .fill (0 );
461
-
462
464
// Prints: <Buffer 00 00 00 00 00 00 00 00 00 00>
463
465
console .log (buf);
464
466
```
@@ -2595,7 +2597,7 @@ Allocates a new `Buffer` of `size` bytes. If the `size` is larger than
2595
2597
A zero-length ` Buffer ` will be created if ` size ` is 0.
2596
2598
2597
2599
The underlying memory for ` SlowBuffer ` instances is * not initialized* . The
2598
- contents of a newly created ` SlowBuffer ` are unknown and could contain
2600
+ contents of a newly created ` SlowBuffer ` are unknown and may contain
2599
2601
sensitive data. Use [ ` buf.fill(0) ` ] [ `buf.fill()` ] to initialize a ` SlowBuffer ` to zeroes.
2600
2602
2601
2603
Example:
0 commit comments