mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -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:
parent
c867c1aa47
commit
543080de55
3 changed files with 62 additions and 14 deletions
|
@ -77,3 +77,21 @@ unitTest({ perms: { read: true } }, function readFileSyncLoop(): void {
|
|||
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());
|
||||
},
|
||||
);
|
||||
|
|
|
@ -69,3 +69,21 @@ unitTest({ perms: { read: true } }, function readTextFileSyncLoop(): void {
|
|||
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());
|
||||
},
|
||||
);
|
||||
|
|
|
@ -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 = {
|
||||
|
|
Loading…
Reference in a new issue