Skip to content

Commit 0879394

Browse files
authored
doc: demonstrate dangers of buffer.slice()
PR-URL: #41628 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Mestery <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 870044f commit 0879394

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

doc/api/buffer.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3427,6 +3427,14 @@ console.log(copiedBuf.toString());
34273427

34283428
console.log(buf.toString());
34293429
// Prints: buffer
3430+
3431+
// With buf.slice(), the original buffer is modified.
3432+
const notReallyCopiedBuf = buf.slice();
3433+
notReallyCopiedBuf[0]++;
3434+
console.log(notReallyCopiedBuf.toString());
3435+
// Prints: cuffer
3436+
console.log(buf.toString());
3437+
// Also prints: cuffer (!)
34303438
```
34313439

34323440
```cjs
@@ -3441,6 +3449,14 @@ console.log(copiedBuf.toString());
34413449

34423450
console.log(buf.toString());
34433451
// Prints: buffer
3452+
3453+
// With buf.slice(), the original buffer is modified.
3454+
const notReallyCopiedBuf = buf.slice();
3455+
notReallyCopiedBuf[0]++;
3456+
console.log(notReallyCopiedBuf.toString());
3457+
// Prints: cuffer
3458+
console.log(buf.toString());
3459+
// Also prints: cuffer (!)
34443460
```
34453461

34463462
### `buf.swap16()`

0 commit comments

Comments
 (0)