Skip to content

Commit

Permalink
Fix ptsname() for big-endian architectures
Browse files Browse the repository at this point in the history
On big-endian architectures unix.IoctlGetInt() leads to a wrong result
because a 32 bit value is stored into a 64 bit buffer. When dereferencing
the result is left shifted by 32. Without this patch ptsname() returns
a wrong path from the second pty onwards.
To protect syscalls against re-arranging by the GC the conversion from
unsafe.Pointer to uintptr must occur in the Syscall expression itself.
See the documentation of the unsafe package for details.

Signed-off-by: Peter Morjan <peter.morjan@de.ibm.com>
  • Loading branch information
pmorjan committed Feb 19, 2018
1 parent 84eeaae commit dbd69c5
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions tc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,21 @@ const (
cmdTcSet = unix.TCSETS
)

func ioctl(fd, flag, data uintptr) error {
if _, _, err := unix.Syscall(unix.SYS_IOCTL, fd, flag, data); err != 0 {
return err
}
return nil
}

// unlockpt unlocks the slave pseudoterminal device corresponding to the master pseudoterminal referred to by f.
// unlockpt should be called before opening the slave side of a pty.
func unlockpt(f *os.File) error {
var u int32
return ioctl(f.Fd(), unix.TIOCSPTLCK, uintptr(unsafe.Pointer(&u)))
if _, _, err := unix.Syscall(unix.SYS_IOCTL, f.Fd(), unix.TIOCSPTLCK, uintptr(unsafe.Pointer(&u))); err != 0 {
return err
}
return nil
}

// ptsname retrieves the name of the first available pts for the given master.
func ptsname(f *os.File) (string, error) {
n, err := unix.IoctlGetInt(int(f.Fd()), unix.TIOCGPTN)
if err != nil {
var u uint32
if _, _, err := unix.Syscall(unix.SYS_IOCTL, f.Fd(), unix.TIOCGPTN, uintptr(unsafe.Pointer(&u))); err != 0 {
return "", err
}
return fmt.Sprintf("/dev/pts/%d", n), nil
return fmt.Sprintf("/dev/pts/%d", u), nil
}

0 comments on commit dbd69c5

Please sign in to comment.