1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 07:14:47 -05:00

fix: panic when process stdio rid is 0 or invalid (#6405)

This commit is contained in:
Luca Casonato 2020-06-25 18:38:19 +02:00 committed by GitHub
parent 1fcb71b355
commit 4102a19585
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 20 deletions

View file

@ -33,12 +33,12 @@ fn clone_file(
}) })
} }
fn subprocess_stdio_map(s: &str) -> std::process::Stdio { fn subprocess_stdio_map(s: &str) -> Result<std::process::Stdio, OpError> {
match s { match s {
"inherit" => std::process::Stdio::inherit(), "inherit" => Ok(std::process::Stdio::inherit()),
"piped" => std::process::Stdio::piped(), "piped" => Ok(std::process::Stdio::piped()),
"null" => std::process::Stdio::null(), "null" => Ok(std::process::Stdio::null()),
_ => unreachable!(), _ => Err(OpError::other("Invalid resource for stdio".to_string())),
} }
} }
@ -86,28 +86,25 @@ fn op_run(
} }
// TODO: make this work with other resources, eg. sockets // TODO: make this work with other resources, eg. sockets
let stdin_rid = run_args.stdin_rid; if run_args.stdin != "" {
if stdin_rid > 0 { c.stdin(subprocess_stdio_map(run_args.stdin.as_ref())?);
let file = clone_file(stdin_rid, &mut resource_table)?; } else {
let file = clone_file(run_args.stdin_rid, &mut resource_table)?;
c.stdin(file); c.stdin(file);
} else {
c.stdin(subprocess_stdio_map(run_args.stdin.as_ref()));
} }
let stdout_rid = run_args.stdout_rid; if run_args.stdout != "" {
if stdout_rid > 0 { c.stdout(subprocess_stdio_map(run_args.stdout.as_ref())?);
let file = clone_file(stdout_rid, &mut resource_table)?; } else {
let file = clone_file(run_args.stdout_rid, &mut resource_table)?;
c.stdout(file); c.stdout(file);
} else {
c.stdout(subprocess_stdio_map(run_args.stdout.as_ref()));
} }
let stderr_rid = run_args.stderr_rid; if run_args.stderr != "" {
if stderr_rid > 0 { c.stderr(subprocess_stdio_map(run_args.stderr.as_ref())?);
let file = clone_file(stderr_rid, &mut resource_table)?;
c.stderr(file);
} else { } else {
c.stderr(subprocess_stdio_map(run_args.stderr.as_ref())); let file = clone_file(run_args.stderr_rid, &mut resource_table)?;
c.stderr(file);
} }
// We want to kill child when it's closed // We want to kill child when it's closed

View file

@ -1,6 +1,7 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { import {
assert, assert,
assertThrows,
assertEquals, assertEquals,
assertStringContains, assertStringContains,
assertThrows, assertThrows,
@ -26,6 +27,46 @@ unitTest({ perms: { run: true } }, async function runSuccess(): Promise<void> {
p.stdout.close(); p.stdout.close();
p.close(); p.close();
}); });
unitTest({ perms: { run: true } }, async function runStdinRid0(): Promise<
void
> {
const p = Deno.run({
cmd: ["python", "-c", "print('hello world')"],
stdin: 0,
stdout: "piped",
stderr: "null",
});
const status = await p.status();
assertEquals(status.success, true);
assertEquals(status.code, 0);
assertEquals(status.signal, undefined);
p.stdout.close();
p.close();
});
unitTest({ perms: { run: true } }, function runInvalidStdio(): void {
assertThrows(() =>
Deno.run({
cmd: ["python", "-c", "print('hello world')"],
// @ts-expect-error because Deno.run should throw on invalid stdin.
stdin: "a",
})
);
assertThrows(() =>
Deno.run({
cmd: ["python", "-c", "print('hello world')"],
// @ts-expect-error because Deno.run should throw on invalid stdout.
stdout: "b",
})
);
assertThrows(() =>
Deno.run({
cmd: ["python", "-c", "print('hello world')"],
// @ts-expect-error because Deno.run should throw on invalid stderr.
stderr: "c",
})
);
});
unitTest( unitTest(
{ perms: { run: true } }, { perms: { run: true } },