Use of the removed Buffer noAssert argument

Use of the removed Buffer noAssert argument

Description

Node.js removed noAssert from Buffer numeric reads and writes in version 10. Passing an extra true on current versions has no effect on bounds checking.

Potential impact

The same call can have different bounds-check behavior on a runtime older than Node.js 10.

Remediation

Drop noAssert and use the current Buffer signature. Check that the offset is an integer and that all bytes to be read or written fit in the buffer. Document older behavior if you support pre-10 runtimes.

Examples

Before

javascript
const buf = Buffer.alloc(8);
// This removed third argument has no effect in modern Node.js
buf.writeInt32LE(1234, 0, true);

After

javascript
const buf = Buffer.alloc(8);
const offset = 0;
if (offset < 0 || offset + 4 > buf.length) {
  throw new RangeError('invalid buffer offset');
}
buf.writeInt32LE(1234, offset);

The example writes four bytes into an eight-byte buffer. The revised call runs only if the offset is nonnegative and offset + 4 is within the buffer length. The current Buffer API also throws for an out-of-range write.

References