Skip to content

Buffer.allocUnsafe with randomFillSync is broken in Node.js 15.0.0 #3033

@Zabrah

Description

@Zabrah
  • Node.js Version: 15.0.0
  • OS: macOS 10.15.7
  • Scope (install, code, runtime, meta, other?): runtime
  • Module (and version) (if relevant): buffer, crypto

In the new Node.js 15.0.0 the use of Buffer.allocUnsafe gives a zero-filled Buffer, like with Buffer.alloc.
This kind of Buffer can be filled will buf.fill or manipulated by buf.writeUInt8. But it cannot be filled with crypto.randomFillSync(buf). If this is tried, the Buffer keeps its previously content.
By using asynchronous randomFill or still the Sync-one but with Buffer.alloc this problem does not appear.

const crypto = require("crypto");
const buf = Buffer.allocUnsafe(4);
console.log(buf);             //--> <Buffer 00 00 00 00>
buf.fill(255);
console.log(buf);             //--> <Buffer ff ff ff ff>
crypto.randomFillSync(buf);
console.log(buf);             //--> <Buffer ff ff ff ff>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions