From 543080de559d997d5ff911e8a8a1625d51f2e0bd Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Thu, 8 Apr 2021 20:06:52 +0530 Subject: fix(runtime/readFile*): close resources on error during read (#10059) This commit ensures readFile, readFileSync, readTextFile, and readTextFileSync does not leak resources on error. --- runtime/js/40_read_file.js | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'runtime') diff --git a/runtime/js/40_read_file.js b/runtime/js/40_read_file.js index 0ca8f56e9..1efd5338d 100644 --- a/runtime/js/40_read_file.js +++ b/runtime/js/40_read_file.js @@ -7,32 +7,44 @@ function readFileSync(path) { const file = openSync(path); - const contents = readAllSync(file); - file.close(); - return contents; + try { + const contents = readAllSync(file); + return contents; + } finally { + file.close(); + } } async function readFile(path) { const file = await open(path); - const contents = await readAll(file); - file.close(); - return contents; + try { + const contents = await readAll(file); + return contents; + } finally { + file.close(); + } } function readTextFileSync(path) { const file = openSync(path); - const contents = readAllSync(file); - file.close(); - const decoder = new TextDecoder(); - return decoder.decode(contents); + try { + const contents = readAllSync(file); + const decoder = new TextDecoder(); + return decoder.decode(contents); + } finally { + file.close(); + } } async function readTextFile(path) { const file = await open(path); - const contents = await readAll(file); - file.close(); - const decoder = new TextDecoder(); - return decoder.decode(contents); + try { + const contents = await readAll(file); + const decoder = new TextDecoder(); + return decoder.decode(contents); + } finally { + file.close(); + } } window.__bootstrap.readFile = { -- cgit v1.2.3