Skip to content

Commit d732904

Browse files
committed
buffer: Fixing the special case in Buffer.concat
If the number of elements to concat is 1, then `Buffer` used to return the only element as it is. But, this is not consistent with `Array`'s `concat`. This patch makes sure that we create a new `Buffer` object.
1 parent f500e18 commit d732904

File tree

3 files changed

+3
-8
lines changed

3 files changed

+3
-8
lines changed

doc/api/buffer.markdown

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ the list together.
130130
If the list has no items, or if the totalLength is 0, then it returns a
131131
zero-length buffer.
132132

133-
If the list has exactly one item, then the first item of the list is
134-
returned.
135-
136-
If the list has more than one item, then a new Buffer is created.
137-
138133
If totalLength is not provided, it is read from the buffers in the list.
139134
However, this adds an additional loop to the function, so it is faster
140135
to provide the length explicitly.

lib/buffer.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,6 @@ Buffer.concat = function(list, length) {
250250

251251
if (list.length === 0)
252252
return new Buffer(0);
253-
else if (list.length === 1)
254-
return list[0];
255253

256254
if (length === undefined) {
257255
length = 0;

test/parallel/test-buffer-concat.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ var flatLongLen = Buffer.concat(long, 40);
1414

1515
assert(flatZero.length === 0);
1616
assert(flatOne.toString() === 'asdf');
17-
assert(flatOne === one[0]);
17+
// A special case where concat used to return the first item,
18+
// if the length is one. This check is to make sure that we don't do that.
19+
assert(flatOne !== one[0]);
1820
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
1921
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
2022

0 commit comments

Comments
 (0)