Skip to content

Buffer.slice unexpected behavior #9096

@rideg

Description

@rideg
  • Version: v6.7.0
  • Platform: Linux 3.13.0-96-generic Ubuntu SMP x86_64 GNU/Linux
  • Subsystem: NodeBuffer

It seems if one provides a non integer boundary to the slice function then the later slice calls on the same buffer provide wrong chunks.

Test case to reproduce

it("Should slice Buffer", () => {
    let original = new Buffer("1234567890ABCDEFG", "ascii");
    let chunk1 = original.slice(0, original.length / 3);
    let chunk2 = original.slice(original.length / 3);
    let result = Buffer.concat([chunk2, chunk1]);
    expect(result).to.be.eqls(original);
});

And the result is:

AssertionError: expected 
|Buffer 31 32 33 34 35 36 37 38 39 30 41 42 43 44 45 46| to deeply equal |Buffer 31 32 33 34 35 36 37 38 39 30 41 42 43 44 45 46 `**47**`|

In this test case the result buffer misses the last byte from the original one.

I am not exactly sure what should be the correct behavior but I would be fine either with throwing an exception or round/ceil/floor the provided boundary value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions