From 11683343460f588b8cca4a22f2f94c15b29f6797 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: Thu, 22 Feb 2018 19:23:55 +0300 Subject: [PATCH 1/3] fix: avoid using deprecated Buffer constructor Buffer.alloc() is supported on Node.js >= 4.5.0, and allocates a zero-filled buffer on all supported versions. This solves two issues: * Buffer() constructor (aka 'new Buffer()') is deprecated, so avoid using it * On 4.x and 6.x, Buffer(number) is not zero-filled, so this code was allocating an uninitialized Buffer when file length was less than 150, so the behaviour of readShebang() was actually undefined in that case (as in could have returned anything, depending on the uninitialized memory chunk). Refs: https://nodejs.org/api/deprecations.html#deprecations_dep0005_buffer_constructor --- lib/util/readShebang.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/util/readShebang.js b/lib/util/readShebang.js index be35dff..2617aca 100644 --- a/lib/util/readShebang.js +++ b/lib/util/readShebang.js @@ -6,7 +6,7 @@ const shebangCommand = require('shebang-command'); function readShebang(command) { // Read the first 150 bytes from the file let fd; - const buffer = new Buffer(150); + const buffer = Buffer.alloc(150); try { fd = fs.openSync(command, 'r'); From 4ebff01a5ab9c731aad855bc0d3e7608132f612b 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: Thu, 1 Mar 2018 16:46:49 +0300 Subject: [PATCH 2/3] readShebang: name reused inline constant '150' was used two times, it deserves a name. It's the size of the pre-read buffer. --- lib/util/readShebang.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/util/readShebang.js b/lib/util/readShebang.js index 2617aca..66f14a3 100644 --- a/lib/util/readShebang.js +++ b/lib/util/readShebang.js @@ -5,12 +5,14 @@ const shebangCommand = require('shebang-command'); function readShebang(command) { // Read the first 150 bytes from the file + const size = 150; + let fd; - const buffer = Buffer.alloc(150); + const buffer = Buffer.alloc(size); try { fd = fs.openSync(command, 'r'); - fs.readSync(fd, buffer, 0, 150, 0); + fs.readSync(fd, buffer, 0, size, 0); fs.closeSync(fd); } catch (e) { /* Empty */ } From 433f94795c015d0b82c06af3006a664fa61f6eb9 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: Thu, 1 Mar 2018 16:51:20 +0300 Subject: [PATCH 3/3] readShebang: Buffer API compat for old Node.js To be reverted when dropping outdated Node.js versions support. --- lib/util/readShebang.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/util/readShebang.js b/lib/util/readShebang.js index 66f14a3..957dd70 100644 --- a/lib/util/readShebang.js +++ b/lib/util/readShebang.js @@ -7,8 +7,17 @@ function readShebang(command) { // Read the first 150 bytes from the file const size = 150; + let buffer; + if (Buffer.alloc) { + // Node.js v4.5+ / v5.10+ + buffer = Buffer.alloc(size); + } else { + // Old Node.js API + buffer = new Buffer(size); + buffer.fill(0); // zero-fill + } + let fd; - const buffer = Buffer.alloc(size); try { fd = fs.openSync(command, 'r');