Skip to content

Commit

Permalink
nodeUtils: additional Buffer safeguards for older Node.js versions
Browse files Browse the repository at this point in the history
1. In allocBuffer(), ensure buffers allocated with new Buffer() are zero-filled.
   On newer Node.js versions, Buffer.alloc() zero-fills already.

2. In newBufferFrom(), throw when 'data' argument is a number.
   On newer Node.js versions, Buffer.from(number)/new Buffer(number, encooding)
   throw already.
  • Loading branch information
ChALkeR committed Feb 18, 2018
1 parent 28cd570 commit dfdd069
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/nodejsUtils.js
Expand Up @@ -17,6 +17,11 @@ module.exports = {
if (Buffer.from && Buffer.from !== Uint8Array.from) {
return Buffer.from(data, encoding);
} else {
if (typeof data === "number") {
// Safeguard for old Node.js versions. On newer versions,
// Buffer.from(number) / Buffer(number, encoding) already throw.
throw new Error("The \"data\" argument must not be a number");
}
return new Buffer(data, encoding);
}
},
Expand All @@ -29,7 +34,7 @@ module.exports = {
if (Buffer.alloc) {
return Buffer.alloc(size);
} else {
return new Buffer(size);
return new Buffer(size).fill(0);
}
},
/**
Expand Down

0 comments on commit dfdd069

Please sign in to comment.