Skip to content

Should Buffer.concat really return the source buffer when length == 1? #1891

@ronkorving

Description

@ronkorving

Buffer.concat returns a new Buffer instance that contains all given buffers combined. However, if the array of buffers that is passed has length 1, the original buffer from that array is returned as-is. Not a copy. That means that manipulation of Buffer.concat's output has no effect on the source buffers if they had length > 1, yet alters the source buffer when length == 1.

This seems very unpredictable (Array.prototype.concat for example always returns a copy) and is bound to at least confuse people, and at worst cause weird bugs.

Original discussion: #1825 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions