From 075e09b10eb1a1e11ac42ff5c6b1d576962b5956 Mon Sep 17 00:00:00 2001 From: waspoza Date: Fri, 9 Jul 2021 22:49:46 +0200 Subject: [PATCH] os: fix a file descriptor leak in os.read_bytes() (#10723) --- vlib/os/os_c.v | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vlib/os/os_c.v b/vlib/os/os_c.v index 22c9423e13..c9d8c3a80a 100644 --- a/vlib/os/os_c.v +++ b/vlib/os/os_c.v @@ -71,6 +71,9 @@ struct C.dirent { [manualfree] pub fn read_bytes(path string) ?[]byte { mut fp := vfopen(path, 'rb') ? + defer { + C.fclose(fp) + } cseek := C.fseek(fp, 0, C.SEEK_END) if cseek != 0 { return error('fseek failed') @@ -85,7 +88,6 @@ pub fn read_bytes(path string) ?[]byte { if nr_read_elements == 0 && fsize > 0 { return error('fread failed') } - C.fclose(fp) res.trim(nr_read_elements * fsize) return res }