Skip to content

Commit

Permalink
doc: demonstrate dangers of buffer.slice()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
shalvah authored and BethGriggs committed Jan 24, 2022
1 parent 57ada37 commit 174155d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions doc/api/buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,14 @@ console.log(copiedBuf.toString());

console.log(buf.toString());
// Prints: buffer

// With buf.slice(), the original buffer is modified.
const notReallyCopiedBuf = buf.slice();
notReallyCopiedBuf[0]++;
console.log(notReallyCopiedBuf.toString());
// Prints: cuffer
console.log(buf.toString());
// Also prints: cuffer (!)
```

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

console.log(buf.toString());
// Prints: buffer

// With buf.slice(), the original buffer is modified.
const notReallyCopiedBuf = buf.slice();
notReallyCopiedBuf[0]++;
console.log(notReallyCopiedBuf.toString());
// Prints: cuffer
console.log(buf.toString());
// Also prints: cuffer (!)
```

### `buf.swap16()`
Expand Down

0 comments on commit 174155d

Please sign in to comment.