Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 174155d

Browse filesBrowse files
shalvahBethGriggs
authored andcommitted
doc: demonstrate dangers of buffer.slice()
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>
1 parent 57ada37 commit 174155d
Copy full SHA for 174155d

File tree

Expand file treeCollapse file tree

1 file changed

+16
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+16
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/buffer.md‎

Copy file name to clipboardExpand all lines: doc/api/buffer.md
+16Lines changed: 16 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.