Skip to content

Commit 9924b88

Browse files
satazorxiehuanyi
andcommitted
fix: close the file descriptor when reading a shebang fails
readShebang only closed the file descriptor after a successful read, so it leaked when fs.readSync threw, e.g.: when the file is a directory. Based on #184. Co-authored-by: Huanyi Xie <74809554+xiehuanyi@users.noreply.github.com>
1 parent 462a846 commit 9924b88

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

‎lib/util/readShebang.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,14 @@ function readShebang(command) {
1313
try {
1414
fd = fs.openSync(command, 'r');
1515
fs.readSync(fd, buffer, 0, size, 0);
16-
fs.closeSync(fd);
17-
} catch (e) { /* Empty */ }
16+
} catch (e) { /* Empty */ } finally {
17+
// Always close the file descriptor, even if reading failed
18+
if (fd !== undefined) {
19+
try {
20+
fs.closeSync(fd);
21+
} catch (e) { /* Empty */ }
22+
}
23+
}
1824

1925
// Attempt to extract shebang (null is returned if not a shebang)
2026
return shebangCommand(buffer.toString());

‎lib/util/readShebang.test.js‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const path = require('path');
5+
const readShebang = require('./readShebang');
6+
7+
describe('readShebang', () => {
8+
afterEach(() => jest.restoreAllMocks());
9+
10+
it('should read the shebang command', () => {
11+
const close = jest.spyOn(fs, 'closeSync');
12+
13+
expect(readShebang(path.join(__dirname, '../../test/fixtures/shebang'))).toBe('node');
14+
expect(close).toHaveBeenCalledTimes(1);
15+
});
16+
17+
it('should return null if the file has no shebang', () => {
18+
expect(readShebang(path.join(__dirname, '../../test/fixtures/echo.js'))).toBe(null);
19+
});
20+
21+
it('should close the file descriptor when reading fails, including descriptor zero', () => {
22+
jest.spyOn(fs, 'openSync').mockReturnValue(0);
23+
jest.spyOn(fs, 'readSync').mockImplementation(() => {
24+
throw new Error('read failed');
25+
});
26+
const close = jest.spyOn(fs, 'closeSync').mockImplementation(() => {});
27+
28+
expect(readShebang('command')).toBe(null);
29+
expect(close).toHaveBeenCalledTimes(1);
30+
expect(close).toHaveBeenCalledWith(0);
31+
});
32+
33+
it('should close the file descriptor when the file is a directory', () => {
34+
const open = jest.spyOn(fs, 'openSync');
35+
const close = jest.spyOn(fs, 'closeSync');
36+
37+
expect(readShebang(__dirname)).toBe(null);
38+
39+
// Opening a directory fails on Windows, but succeeds on other platforms and then reading fails
40+
expect(close).toHaveBeenCalledTimes(open.mock.results.filter((result) => result.type === 'return').length);
41+
});
42+
43+
it('should not close a file descriptor when opening fails', () => {
44+
jest.spyOn(fs, 'openSync').mockImplementation(() => {
45+
throw new Error('open failed');
46+
});
47+
const close = jest.spyOn(fs, 'closeSync').mockImplementation(() => {});
48+
49+
expect(readShebang('command')).toBe(null);
50+
expect(close).not.toHaveBeenCalled();
51+
});
52+
53+
it('should return null when closing also fails', () => {
54+
jest.spyOn(fs, 'openSync').mockReturnValue(0);
55+
jest.spyOn(fs, 'readSync').mockImplementation(() => {
56+
throw new Error('read failed');
57+
});
58+
jest.spyOn(fs, 'closeSync').mockImplementation(() => {
59+
throw new Error('close failed');
60+
});
61+
62+
expect(readShebang('command')).toBe(null);
63+
});
64+
});

0 commit comments

Comments
 (0)