Skip to content

fs.read at position 0 followed by position null reads wrong data #8397

Closed
@defunctzombie

Description

@defunctzombie
  • Version: 6.4.0
  • Platform: Ubuntu 14.04 x64
  • Subsystem: fs

After reading from position 0 (using fs.read), subsequence read with position null reads the incorrect data.

I would expect a read from position 0 followed by a read from null position to read from the next set of data per the documentation:

position is an integer specifying where to begin reading from in the file. If position is null, data will be read from the current file position.

Save the following to a file and run it.

const fs = require('fs');
const assert = require('assert');

// change this to toggle failing vs passing behavior
const fail = true;

fs.open(__filename, 'r', (err, fd) => {
    assert(!err);

    const buff = new Buffer(4);

    // this is the line that causes incorrect (unexpected?) behavior
    // if 0 is passed to read, then the subsequent read with 'null'
    // results in the wrong data being read
    // when `null` is used I get `cons` followed by `t fs`
    // when 0 is used first, I get `cons` followed by `cons`
    fs.read(fd, buff, 0, 4, (fail) ? 0 : null, (err, bytes, buffer) => {
        assert(!err);
        assert.equal(buffer.toString(), 'cons');
        console.log(buffer.toString());
        fs.read(fd, buff, 0, 4, null, (err, bytes, buffer) => {
            assert(!err);
            assert.equal(buffer.toString(), 't fs');
            console.log(buffer.toString());
        });
    });
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    fsIssues and PRs related to the fs subsystem / file system.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions