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 danielleadams committed Mar 14, 2022
1 parent cdbe291 commit 0388b9a
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 @@ -3409,6 +3409,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 @@ -3423,6 +3431,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 0388b9a

Please sign in to comment.