Skip to content

Commit

Permalink
Merge pull request #139 from yassinebenaid/dev
Browse files Browse the repository at this point in the history
fix: closing a file descriptor should not be an issue until it is used
  • Loading branch information
yassinebenaid authored Jan 25, 2025
2 parents 59dff22 + c18d73e commit 3ed2046
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 25 deletions.
23 changes: 5 additions & 18 deletions runtime/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,23 @@ func NewBuffer(s string, readonly bool) *Buffer {

type proxyStream struct {
original Stream
closed bool
}

func (s *proxyStream) Close() error {
if s.closed {
return fmt.Errorf("cannot close closed stream")
}
s.closed = true
// this is a bad file descirptor, it will throw an error on all operations.
s.original = os.NewFile(^uintptr(0), "")
return nil
}

func (s *proxyStream) Read(p []byte) (n int, err error) { return 0, nil }

func (s *proxyStream) Write(p []byte) (n int, err error) { return 0, nil }
func (s *proxyStream) getOriginal() (Stream, error) {
if s.closed {
return nil, fmt.Errorf("file descriptor is closed")
}

func (s *proxyStream) getOriginal() Stream {
if o, ok := s.original.(*proxyStream); ok {
return o.getOriginal()
}

return s.original, nil
return s.original
}

type StreamManager struct {
Expand Down Expand Up @@ -141,18 +134,12 @@ func (sm *StreamManager) Get(fd string) (Stream, error) {
return nil, fmt.Errorf("file descriptor %q is not open", fd)
}

if stream, err := proxy.getOriginal(); err != nil {
return nil, fmt.Errorf("bad file descriptor %q, %w", fd, err)
} else {
return stream, nil
}
return proxy.getOriginal(), nil
}

func (sm *StreamManager) Duplicate(newfd, oldfd string) error {
if proxy, ok := sm.mappings[oldfd]; !ok {
return fmt.Errorf("trying to duplicate bad file descriptor: %s", oldfd)
} else if proxy.closed {
return fmt.Errorf("trying to duplicate closed file descriptor: %s", oldfd)
} else {
sm.mappings[newfd] = &proxyStream{
original: proxy.original,
Expand Down
55 changes: 48 additions & 7 deletions tests/01-redirections.yml
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ cases:
"file2.txt": ""

- name: "cannot duplicate a bad file descriptor"
runs_on: linux
script: |
# file open and closed
echo helloworld 3>file.txt 3>&- >&3
Expand All @@ -240,17 +241,57 @@ cases:
echo helloworld <&3
# duplicating a closed piped-stream
cat 3<<<foobar 3>&- <&3
grep foo 3<<<foobar 3>&- <&3
# closing a file descriptor is not an issue until it is used
true <&- >&- 2>&-
expect:
exit_code: 1
exit_code: 0
stderr: |
trying to duplicate closed file descriptor: 3
trying to duplicate closed file descriptor: 3
trying to duplicate closed file descriptor: 1
trying to duplicate closed file descriptor: 1
echo: write error: Bad file descriptor
echo: write error: Bad file descriptor
echo: write error: Bad file descriptor
echo: write error: Bad file descriptor
trying to duplicate bad file descriptor: 3
trying to duplicate bad file descriptor: 3
grep: (standard input): Bad file descriptor
files:
"file.txt": ""

- name: "cannot duplicate a bad file descriptor"
runs_on: darwin
script: |
# file open and closed
echo helloworld 3>file.txt 3>&- >&3
# file open and closed (second syntax)
echo helloworld 3>file.txt 3<&- >&3
# stdout is closed
echo helloworld >&- 3<&1
# stdout is duplicated and closed
echo helloworld 3>&1- 4>&1
# duplicating a non open file descriptor
echo helloworld >&3
echo helloworld <&3
# duplicating a closed piped-stream
grep foo 3<<<foobar 3>&- <&3
# closing a file descriptor is not an issue until it is used
true <&- >&- 2>&-
expect:
exit_code: 0
stderr: |
echo: fflush: Bad file descriptor
echo: fflush: Bad file descriptor
echo: fflush: Bad file descriptor
echo: fflush: Bad file descriptor
trying to duplicate bad file descriptor: 3
trying to duplicate bad file descriptor: 3
trying to duplicate closed file descriptor: 3
grep: (standard input): Bad file descriptor
files:
"file.txt": ""

Expand Down

0 comments on commit 3ed2046

Please sign in to comment.