Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Commit 73bd61b

Browse files
committed
Fix file descriptor leak
CID#318633 Fixes #872
1 parent e4f95a7 commit 73bd61b

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/cmd/ksh93/bltins/hist.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ int b_hist(int argc, char *argv[], Shbltin_t *context) {
248248
com[2] = 0;
249249
error_info.errors = sh_eval(shp, sh_sfeval(com), 0);
250250
}
251+
251252
fdo = sh_chkopen(fname);
252253
unlink(fname);
253254
free(fname);
@@ -258,19 +259,22 @@ int b_hist(int argc, char *argv[], Shbltin_t *context) {
258259
sh_onstate(shp, SH_VERBOSE); // echo lines as read
259260
if (replace) {
260261
hist_subst(shp, error_info.id, fdo, replace);
262+
sh_close(fdo);
261263
} else if (error_info.errors == 0) {
262-
char buff[IOBSIZE + 1];
263-
Sfio_t *iop = sfnew(NULL, buff, IOBSIZE, fdo, SF_READ);
264264
// Read in and run the command.
265265
if (shp->hist_depth++ > HIST_RECURSE) {
266+
sh_close(fdo);
266267
errormsg(SH_DICT, ERROR_exit(1), e_toodeep, "history");
267268
__builtin_unreachable();
268269
}
269-
sh_eval(shp, iop, 1);
270+
271+
char buff[IOBSIZE + 1];
272+
Sfio_t *iop = sfnew(NULL, buff, IOBSIZE, fdo, SF_READ);
273+
sh_eval(shp, iop, 1); // this will close fdo
270274
shp->hist_depth--;
271275
} else {
272276
sh_close(fdo);
273-
if (!sh_isoption(shp, SH_VERBOSE)) sh_offstate(shp, SH_VERBOSE);
277+
sh_offstate(shp, SH_VERBOSE);
274278
sh_offstate(shp, SH_HISTORY);
275279
}
276280
return shp->exitval;

0 commit comments

Comments
 (0)