Node: fs.readFileSync returns a Buffer object with wrong 'buffer' property

Created on 29 Jun 2017  路  3Comments  路  Source: nodejs/node

  • Version: v6.10.3
  • Platform: Linux thanos 3.16.0-45-generic #60~14.04.1-Ubuntu SMP Fri Jul 24 21:16:23 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem: fs

I read a file whose size is 32 bytes with fs.readFileSync.

I got a Buffer object whose length is 32, but the buffer property (which is an ArrayBuffer of the Buffer object) of the Buffer object has 8192 byteLength.

Code:

var fs = require('fs');
var buf = fs.readFileSync('/tmp/foo');  // foo's size is 32 bytes
console.log(buf.length);  // 32
console.log(buf.buffer.byteLength); // 8192

This behavior might be confusing for someone who wants to use the buffer property of a Buffer object.
(I instantiated a typed array with buf.buffer, but the byteLength of the typed array was 8192, not 32.)

I guess this happens because readFileSync function returns a Buffer object allocated with Buffer.allocUnsafe in fs.js see line 530 in fs.js
(This may be related to a pre-allocated pool (8192 bytes) used in Buffer.allocUnsafe)

I suggest tryCreateBuffer in readFileSync to use Buffer.alloc instead of Buffer.allocUnsafe for creating a Buffer object.

Thanks.

buffer fs

Most helpful comment

Not a bug, no. Node has worked this way ever since it had a Buffer type (the heady days of node.js v0.3.x; oh, to be young again) and it's a performance optimization.

Closing out as working-as-intended.

All 3 comments

This is not limited to readFileSync:

Buffer.from([ 0x0, 0x1, 0x2, 0x3 ]).buffer.byteLength == 8192

A buffer is a view onto an ArrayBuffer and does not necessarily correspond to the length of the buffer:

new Buffer(10).buffer.byteLength == 10
new Buffer(10).slice(0, 5).buffer.byteLength = 10

In general, there is no guarantee that buf.length and buf.buffer.byteLength are equal. I am not sure we consider this a bug. @nodejs/buffer @nodejs/fs

Not a bug, no. Node has worked this way ever since it had a Buffer type (the heady days of node.js v0.3.x; oh, to be young again) and it's a performance optimization.

Closing out as working-as-intended.

To clarify @bnoordhuis' somewhat laconic reply, this issue is in fact a duplicate of #11132. The buffer documentation contains a summary of interaction between Buffer, ArrayBuffer, and other TypedArray types.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevenvachon picture stevenvachon  路  3Comments

addaleax picture addaleax  路  3Comments

cong88 picture cong88  路  3Comments

srl295 picture srl295  路  3Comments

danielstaleiny picture danielstaleiny  路  3Comments