Skip to content

Commit 082f952

Browse files
committed
fs: cleanup fd lchown and lchownSync
lchown and lchownSync were opening file descriptors without closing them. Looks like it has been that way for 7 years. Does anyone actually use these functions? PR-URL: #18329 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent fa33f02 commit 082f952

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

lib/fs.js

+19-11
Original file line numberDiff line numberDiff line change
@@ -1330,20 +1330,16 @@ if (constants.O_SYMLINK !== undefined) {
13301330
};
13311331

13321332
fs.lchmodSync = function(path, mode) {
1333-
var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
1333+
const fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
13341334

13351335
// Prefer to return the chmod error, if one occurs,
13361336
// but still try to close, and report closing errors if they occur.
1337-
var ret;
1337+
let ret;
13381338
try {
13391339
ret = fs.fchmodSync(fd, mode);
1340-
} catch (err) {
1341-
try {
1342-
fs.closeSync(fd);
1343-
} catch (ignore) {}
1344-
throw err;
1340+
} finally {
1341+
fs.closeSync(fd);
13451342
}
1346-
fs.closeSync(fd);
13471343
return ret;
13481344
};
13491345
}
@@ -1381,13 +1377,25 @@ if (constants.O_SYMLINK !== undefined) {
13811377
callback(err);
13821378
return;
13831379
}
1384-
fs.fchown(fd, uid, gid, callback);
1380+
// Prefer to return the chown error, if one occurs,
1381+
// but still try to close, and report closing errors if they occur.
1382+
fs.fchown(fd, uid, gid, function(err) {
1383+
fs.close(fd, function(err2) {
1384+
callback(err || err2);
1385+
});
1386+
});
13851387
});
13861388
};
13871389

13881390
fs.lchownSync = function(path, uid, gid) {
1389-
var fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
1390-
return fs.fchownSync(fd, uid, gid);
1391+
const fd = fs.openSync(path, constants.O_WRONLY | constants.O_SYMLINK);
1392+
let ret;
1393+
try {
1394+
ret = fs.fchownSync(fd, uid, gid);
1395+
} finally {
1396+
fs.closeSync(fd);
1397+
}
1398+
return ret;
13911399
};
13921400
}
13931401

0 commit comments

Comments
 (0)