From 8f0c4e62ffeff86148547c968b0743813d70e649 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Thu, 8 Jun 2023 18:42:51 +0200 Subject: libc/exec: don't leak file descriptors --- src/user/lib/elfload.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/user/lib/elfload.c') diff --git a/src/user/lib/elfload.c b/src/user/lib/elfload.c index 7b92d35..c1a08f8 100644 --- a/src/user/lib/elfload.c +++ b/src/user/lib/elfload.c @@ -10,6 +10,7 @@ #include void elf_execf(FILE *f, char **argv, char **envp) { + size_t ret; void *buf; long buflen; @@ -20,8 +21,12 @@ void elf_execf(FILE *f, char **argv, char **envp) { // TODO don't read the entire file into memory fseek(f, 0, SEEK_SET); - if (buf && fread(buf, 1, buflen, f)) + if (!buf) return; + ret = fread(buf, buflen, 1, f); + fclose(f); + if (ret == 1) { elf_exec(buf, argv, envp); + } free(buf); } -- cgit v1.2.3