From 5d9cb1fb28f04daa14887cff024a0df5cc11cc8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=BA=D0=BE=D0=B2=D0=BE=D1=80=D0=BE=D0=B4=D0=B0=20?= =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=B8=D1=82=D0=B0=20=D0=90=D0=BD=D0=B4=D1=80?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2=D0=B8=D1=87?= Date: Sun, 18 Feb 2018 22:42:57 +0300 Subject: [PATCH] nodejsUtils: additional Buffer safeguards for older Node.js 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. --- lib/nodejsUtils.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/nodejsUtils.js b/lib/nodejsUtils.js index 0716cd57..2202f152 100644 --- a/lib/nodejsUtils.js +++ b/lib/nodejsUtils.js @@ -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); } }, @@ -29,7 +34,7 @@ module.exports = { if (Buffer.alloc) { return Buffer.alloc(size); } else { - return new Buffer(size); + return new Buffer(size).fill(0); } }, /**