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 <benjamingr@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
  • 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
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.