Skip to content

Commit 0388b9a

Browse files
shalvahdanielleadams
authored andcommitted
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 cdbe291 commit 0388b9a

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
@@ -3409,6 +3409,14 @@ console.log(copiedBuf.toString());
34093409

34103410
console.log(buf.toString());
34113411
// Prints: buffer
3412+
3413+
// With buf.slice(), the original buffer is modified.
3414+
const notReallyCopiedBuf = buf.slice();
3415+
notReallyCopiedBuf[0]++;
3416+
console.log(notReallyCopiedBuf.toString());
3417+
// Prints: cuffer
3418+
console.log(buf.toString());
3419+
// Also prints: cuffer (!)
34123420
```
34133421

34143422
```cjs
@@ -3423,6 +3431,14 @@ console.log(copiedBuf.toString());
34233431

34243432
console.log(buf.toString());
34253433
// Prints: buffer
3434+
3435+
// With buf.slice(), the original buffer is modified.
3436+
const notReallyCopiedBuf = buf.slice();
3437+
notReallyCopiedBuf[0]++;
3438+
console.log(notReallyCopiedBuf.toString());
3439+
// Prints: cuffer
3440+
console.log(buf.toString());
3441+
// Also prints: cuffer (!)
34263442
```
34273443

34283444
### `buf.swap16()`

0 commit comments

Comments
 (0)