From babaaaf6a443442998b490be836809cbf0b058ae Mon Sep 17 00:00:00 2001 From: Shalvah Date: Fri, 21 Jan 2022 13:57:08 +0100 Subject: [PATCH] doc: Demonstrate the dangers of `buffer.slice()` --- doc/api/buffer.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/doc/api/buffer.md b/doc/api/buffer.md index 80a97ababd2d06..4259f3a76bde96 100644 --- a/doc/api/buffer.md +++ b/doc/api/buffer.md @@ -3427,6 +3427,15 @@ console.log(copiedBuf.toString()); console.log(buf.toString()); // Prints: buffer + +// With buffer.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 @@ -3441,6 +3450,15 @@ console.log(copiedBuf.toString()); console.log(buf.toString()); // Prints: buffer + +// With buffer.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()`