Skip to content

Commit 174155d

Browse files
shalvahBethGriggs
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 57ada37 commit 174155d

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

doc/api/buffer.md

+16
Original file line numberDiff line numberDiff line change
@@ -3425,6 +3425,14 @@ console.log(copiedBuf.toString());
34253425

34263426
console.log(buf.toString());
34273427
// Prints: buffer
3428+
3429+
// With buf.slice(), the original buffer is modified.
3430+
const notReallyCopiedBuf = buf.slice();
3431+
notReallyCopiedBuf[0]++;
3432+
console.log(notReallyCopiedBuf.toString());
3433+
// Prints: cuffer
3434+
console.log(buf.toString());
3435+
// Also prints: cuffer (!)
34283436
```
34293437

34303438
```cjs
@@ -3439,6 +3447,14 @@ console.log(copiedBuf.toString());
34393447

34403448
console.log(buf.toString());
34413449
// Prints: buffer
3450+
3451+
// With buf.slice(), the original buffer is modified.
3452+
const notReallyCopiedBuf = buf.slice();
3453+
notReallyCopiedBuf[0]++;
3454+
console.log(notReallyCopiedBuf.toString());
3455+
// Prints: cuffer
3456+
console.log(buf.toString());
3457+
// Also prints: cuffer (!)
34423458
```
34433459

34443460
### `buf.swap16()`

0 commit comments

Comments
 (0)