Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Node Buffers less than 8192 bytes #774

Closed
jerfowler opened this issue Mar 19, 2017 · 1 comment
Closed

Bug: Node Buffers less than 8192 bytes #774

jerfowler opened this issue Mar 19, 2017 · 1 comment

Comments

@jerfowler
Copy link
Contributor

jerfowler commented Mar 19, 2017

When passing a small "unsafe" node buffer to Axios that is less than the size of Buffer.poolSize which is 8192 bytes, axios passes the entire 8192 bytes in the pool which can contain random data.

    var buf = new Buffer(1024); // Unsafe buffer < Buffer.poolSize (8192 bytes)
    buf.fill('x');

--or --

    var buf = Buffer.allocUnsafe(1024).fill('x'); // Unsafe buffer < Buffer.poolSize (8192 bytes)

Buffer.allocUnsafe(size)

Use of this pre-allocated internal memory pool is a key difference between calling Buffer.alloc(size, fill) vs. Buffer.allocUnsafe(size).fill(fill). Specifically, Buffer.alloc(size, fill) will never use the internal Buffer pool, while Buffer.allocUnsafe(size).fill(fill) will use the internal Buffer pool if size is less than or equal to half Buffer.poolSize. The difference is subtle but can be important when an application requires the additional performance that Buffer.allocUnsafe() provides.

Here is a test to reproduce the issue:

  testBuffer: function(test) {
    var buf = new Buffer(1024); // Unsafe buffer < Buffer.poolSize (8192 bytes)
    buf.fill('x');
    server = http.createServer(function (req, res) {
      test.equal(req.headers['content-length'], buf.length.toString());
      req.pipe(res);
    }).listen(4444, function () {
      axios.post('http://localhost:4444/',
        buf, {
        responseType: 'stream'
      }).then(function (res) {
        var stream = res.data;
        var string = '';
        stream.on('data', function (chunk) {
          string += chunk.toString('utf8');
        });
        stream.on('end', function () {
          test.equal(string, buf.toString());
          test.done();
        });
      });
    });
  },

Pull Request w/ test: #773

@nickuraltsev
Copy link
Member

Fixed via #773

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants