FD leak in open()
stephan57160 opened this issue · 4 comments
stephan57160 commented
The code of term.Open() does not close the FD, in case of error:
// Open opens an asynchronous communications port.
func Open(name string, options ...func(*Term) error) (*Term, error) {
fd, e := unix.Open(name, unix.O_NOCTTY|unix.O_CLOEXEC|unix.O_NDELAY|unix.O_RDWR, 0666)
if e != nil {
// FD not opened, so, we don't care.
return nil, &os.PathError{
Op: "open",
Path: name,
Err: e,
}
}
orig, err := termios.Tcgetattr(uintptr(fd))
if err != nil {
// FD opened, but not closed.
return nil, err
}
t := Term{name: name, fd: fd, orig: *orig}
if err := t.SetOption(options...); err != nil {
// FD open, but not closed.
return nil, err
}
// FD opened, but not closed if SetNonblock() fails.
return &t, unix.SetNonblock(t.fd, false)
}
Leak may happen, when the supposed TERM file is not a TERM.
davecheney commented
good catch, would you like to send a PR?
stephan57160 commented
Not sure I can test it fully, but why not, yes.
give me 1h or so.
Le 15-Dec-21 à 11:45, Dave Cheney a écrit :
…
good catch, would you like to send a PR?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#68 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AI7SRNO4FBZHUOPL5IA2KGTURBWWNANCNFSM5KDH7EZQ>.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
stephan57160 commented
Well...
Actually, I found almost the same in
- term_open_posix.go
- term_solaris.go
Working on a similar solution for the 2nd one as well, but, I cannot test anything.
stephan57160 commented
PR #69 raised, in case you have some time to check it.