fix: allow attaching to any combination of stdin/stdout/stderr

Before this PR, if a stdin/stdout/stderr stream is nil,
and the corresponding FIFO is not an empty string,
a panic will occur when Read/Write of the nil stream is invoked in io.CopyBuffer.

Signed-off-by: Hsing-Yu (David) Chen <davidhsingyuchen@gmail.com>
This commit is contained in:
Hsing-Yu (David) Chen
2023-03-28 17:13:28 -07:00
parent 40f26543bd
commit 687a5f51a8
2 changed files with 106 additions and 39 deletions

View File

@@ -166,6 +166,15 @@ func NewAttach(opts ...Opt) Attach {
if fifos == nil {
return nil, fmt.Errorf("cannot attach, missing fifos")
}
if streams.Stdin == nil {
fifos.Stdin = ""
}
if streams.Stdout == nil {
fifos.Stdout = ""
}
if streams.Stderr == nil {
fifos.Stderr = ""
}
return copyIO(fifos, streams)
}
}