1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 16:42:21 -05:00

fix(runtime/readFile*): close resources on error during read (#10059)

This commit ensures readFile, readFileSync, readTextFile,
and readTextFileSync does not leak resources on error.
This commit is contained in:
Satya Rohith 2021-04-08 20:06:52 +05:30 committed by GitHub
parent c867c1aa47
commit 543080de55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 14 deletions

View file

@ -77,3 +77,21 @@ unitTest({ perms: { read: true } }, function readFileSyncLoop(): void {
Deno.readFileSync("cli/tests/fixture.json"); Deno.readFileSync("cli/tests/fixture.json");
} }
}); });
unitTest(
{ perms: { read: true } },
async function readFileDoesNotLeakResources(): Promise<void> {
const resourcesBefore = Deno.resources();
await assertThrowsAsync(async () => await Deno.readFile("cli"));
assertEquals(resourcesBefore, Deno.resources());
},
);
unitTest(
{ perms: { read: true } },
function readFileSyncDoesNotLeakResources(): void {
const resourcesBefore = Deno.resources();
assertThrows(() => Deno.readFileSync("cli"));
assertEquals(resourcesBefore, Deno.resources());
},
);

View file

@ -69,3 +69,21 @@ unitTest({ perms: { read: true } }, function readTextFileSyncLoop(): void {
Deno.readTextFileSync("cli/tests/fixture.json"); Deno.readTextFileSync("cli/tests/fixture.json");
} }
}); });
unitTest(
{ perms: { read: true } },
async function readTextFileDoesNotLeakResources(): Promise<void> {
const resourcesBefore = Deno.resources();
await assertThrowsAsync(async () => await Deno.readTextFile("cli"));
assertEquals(resourcesBefore, Deno.resources());
},
);
unitTest(
{ perms: { read: true } },
function readTextFileSyncDoesNotLeakResources(): void {
const resourcesBefore = Deno.resources();
assertThrows(() => Deno.readTextFileSync("cli"));
assertEquals(resourcesBefore, Deno.resources());
},
);

View file

@ -7,32 +7,44 @@
function readFileSync(path) { function readFileSync(path) {
const file = openSync(path); const file = openSync(path);
const contents = readAllSync(file); try {
file.close(); const contents = readAllSync(file);
return contents; return contents;
} finally {
file.close();
}
} }
async function readFile(path) { async function readFile(path) {
const file = await open(path); const file = await open(path);
const contents = await readAll(file); try {
file.close(); const contents = await readAll(file);
return contents; return contents;
} finally {
file.close();
}
} }
function readTextFileSync(path) { function readTextFileSync(path) {
const file = openSync(path); const file = openSync(path);
const contents = readAllSync(file); try {
file.close(); const contents = readAllSync(file);
const decoder = new TextDecoder(); const decoder = new TextDecoder();
return decoder.decode(contents); return decoder.decode(contents);
} finally {
file.close();
}
} }
async function readTextFile(path) { async function readTextFile(path) {
const file = await open(path); const file = await open(path);
const contents = await readAll(file); try {
file.close(); const contents = await readAll(file);
const decoder = new TextDecoder(); const decoder = new TextDecoder();
return decoder.decode(contents); return decoder.decode(contents);
} finally {
file.close();
}
} }
window.__bootstrap.readFile = { window.__bootstrap.readFile = {