Skip to content

Commit

Permalink
nodejsUtils: additional Buffer safeguards for older Node.js
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 8461371
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 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,9 @@ module.exports = {
if (Buffer.alloc) {
return Buffer.alloc(size);
} else {
return new Buffer(size);
var buf = new Buffer(size);
buf.fill(0);
return buf;
}
},
/**
Expand Down

0 comments on commit 8461371

Please sign in to comment.