Skip to content

Commit 4a71936

Browse files
authored
Merge pull request #57 from thaJeztah/update_actions
fix linting and update golangci-lint to v2.11
2 parents d342b58 + 13d2eed commit 4a71936

6 files changed

Lines changed: 55 additions & 34 deletions

File tree

.github/workflows/test.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,24 @@ jobs:
2929
go-version: ${{ matrix.go }}
3030
- name: Checkout code
3131
uses: actions/checkout@v6
32+
with:
33+
persist-credentials: false
3234
- name: Test
3335
run: go test -v ./...
3436
lint:
3537
runs-on: ubuntu-latest
3638
steps:
3739
- name: Checkout code
3840
uses: actions/checkout@v6
41+
with:
42+
persist-credentials: false
3943
- name: go mod tidy
4044
run: |
4145
go mod tidy
4246
git diff --exit-code
4347
- name: Lint
4448
run: |
45-
docker run --rm -v ./:/go/src/github.com/moby/term -w /go/src/github.com/moby/term \
46-
golangci/golangci-lint:v2.8-alpine golangci-lint run -v
49+
docker run --rm \
50+
-v ./:/go/src/github.com/moby/term \
51+
-w /go/src/github.com/moby/term \
52+
golangci/golangci-lint:v2.11-alpine golangci-lint run -v

term.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ type Winsize struct {
1010
Height uint16
1111
Width uint16
1212

13-
// Only used on Unix
14-
x uint16
15-
y uint16
13+
x uint16 //nolint:unused // only used on Unix
14+
y uint16 //nolint:unused // only used on Unix
1615
}
1716

1817
// StdStreams returns the standard streams (stdin, stdout, stderr).

term_unix.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package term
66
import (
77
"errors"
88
"io"
9+
"math"
910
"os"
1011

1112
"golang.org/x/sys/unix"
@@ -31,12 +32,18 @@ func getFdInfo(in interface{}) (uintptr, bool) {
3132
}
3233

3334
func getWinsize(fd uintptr) (*Winsize, error) {
35+
if fd > math.MaxInt {
36+
return nil, errors.New("invalid file descriptor")
37+
}
3438
uws, err := unix.IoctlGetWinsize(int(fd), unix.TIOCGWINSZ)
3539
ws := &Winsize{Height: uws.Row, Width: uws.Col, x: uws.Xpixel, y: uws.Ypixel}
3640
return ws, err
3741
}
3842

3943
func setWinsize(fd uintptr, ws *Winsize) error {
44+
if fd > math.MaxInt {
45+
return errors.New("invalid file descriptor")
46+
}
4047
return unix.IoctlSetWinsize(int(fd), unix.TIOCSWINSZ, &unix.Winsize{
4148
Row: ws.Height,
4249
Col: ws.Width,
@@ -81,6 +88,9 @@ func setRawTerminalOutput(uintptr) (*State, error) {
8188
}
8289

8390
func tcget(fd uintptr) (*unix.Termios, error) {
91+
if fd > math.MaxInt {
92+
return nil, errors.New("invalid file descriptor")
93+
}
8494
p, err := unix.IoctlGetTermios(int(fd), getTermios)
8595
if err != nil {
8696
return nil, err
@@ -89,5 +99,8 @@ func tcget(fd uintptr) (*unix.Termios, error) {
8999
}
90100

91101
func tcset(fd uintptr, p *unix.Termios) error {
102+
if fd > math.MaxInt {
103+
return errors.New("invalid file descriptor")
104+
}
92105
return unix.IoctlSetTermios(int(fd), setTermios, p)
93106
}

term_windows.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package term
33
import (
44
"errors"
55
"io"
6+
"math"
67
"os"
78
"os/signal"
89

@@ -94,13 +95,19 @@ func getWinsize(fd uintptr) (*Winsize, error) {
9495
return nil, err
9596
}
9697

98+
w := int32(info.Window.Right) - int32(info.Window.Left) + 1
99+
h := int32(info.Window.Bottom) - int32(info.Window.Top) + 1
100+
if w < 0 || w > math.MaxUint16 || h < 0 || h > math.MaxUint16 {
101+
return nil, errors.New("invalid console window size")
102+
}
103+
97104
return &Winsize{
98-
Width: uint16(info.Window.Right - info.Window.Left + 1),
99-
Height: uint16(info.Window.Bottom - info.Window.Top + 1),
105+
Width: uint16(w),
106+
Height: uint16(h),
100107
}, nil
101108
}
102109

103-
func setWinsize(fd uintptr, ws *Winsize) error {
110+
func setWinsize(uintptr, *Winsize) error {
104111
return errors.New("not implemented on Windows")
105112
}
106113

@@ -167,7 +174,7 @@ func restoreAtInterrupt(fd uintptr, state *State) {
167174
signal.Notify(sigchan, os.Interrupt)
168175

169176
go func() {
170-
_ = <-sigchan
177+
<-sigchan
171178
_ = RestoreTerminal(fd, state)
172179
os.Exit(0)
173180
}()

windows/ansi_reader.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ const (
2222

2323
// ansiReader wraps a standard input file (e.g., os.Stdin) providing ANSI sequence translation.
2424
type ansiReader struct {
25-
file *os.File
26-
fd uintptr
27-
buffer []byte
28-
cbBuffer int
29-
command []byte
25+
file *os.File
26+
fd uintptr
27+
buffer []byte
28+
command []byte
3029
}
3130

3231
// NewAnsiReader returns an io.ReadCloser that provides VT100 terminal emulation on top of a
@@ -99,11 +98,8 @@ func (ar *ansiReader) Read(p []byte) (int, error) {
9998

10099
// readInputEvents polls until at least one event is available.
101100
func readInputEvents(ar *ansiReader, maxBytes int) ([]winterm.INPUT_RECORD, error) {
102-
// Determine the maximum number of records to retrieve
103-
// -- Cast around the type system to obtain the size of a single INPUT_RECORD.
104-
// unsafe.Sizeof requires an expression vs. a type-reference; the casting
105-
// tricks the type system into believing it has such an expression.
106-
recordSize := int(unsafe.Sizeof(*((*winterm.INPUT_RECORD)(unsafe.Pointer(&maxBytes)))))
101+
// Determine the size of a single INPUT_RECORD.
102+
recordSize := int(unsafe.Sizeof(winterm.INPUT_RECORD{}))
107103
countRecords := maxBytes / recordSize
108104
if countRecords > ansiterm.MAX_INPUT_EVENTS {
109105
countRecords = ansiterm.MAX_INPUT_EVENTS
@@ -167,9 +163,10 @@ var keyMapPrefix = map[uint16]string{
167163
// translateKeyEvents converts the input events into the appropriate ANSI string.
168164
func translateKeyEvents(events []winterm.INPUT_RECORD, escapeSequence []byte) []byte {
169165
var buffer bytes.Buffer
170-
for _, event := range events {
166+
for i := range events {
167+
event := events[i]
171168
if event.EventType == winterm.KEY_EVENT && event.KeyEvent.KeyDown != 0 {
172-
buffer.WriteString(keyToString(&event.KeyEvent, escapeSequence))
169+
buffer.WriteString(keyToString(&events[i].KeyEvent, escapeSequence))
173170
}
174171
}
175172

@@ -181,24 +178,24 @@ func keyToString(keyEvent *winterm.KEY_EVENT_RECORD, escapeSequence []byte) stri
181178
if keyEvent.UnicodeChar == 0 {
182179
return formatVirtualKey(keyEvent.VirtualKeyCode, keyEvent.ControlKeyState, escapeSequence)
183180
}
184-
181+
key := string(rune(keyEvent.UnicodeChar))
185182
_, alt, control := getControlKeys(keyEvent.ControlKeyState)
186-
if control {
183+
switch {
184+
case control && !alt:
187185
// TODO(azlinux): Implement following control sequences
188186
// <Ctrl>-D Signals the end of input from the keyboard; also exits current shell.
189187
// <Ctrl>-H Deletes the first character to the left of the cursor. Also called the ERASE key.
190188
// <Ctrl>-Q Restarts printing after it has been stopped with <Ctrl>-s.
191189
// <Ctrl>-S Suspends printing on the screen (does not stop the program).
192190
// <Ctrl>-U Deletes all characters on the current line. Also called the KILL key.
193191
// <Ctrl>-E Quits current command and creates a core
192+
return key
193+
case !control && alt:
194+
// <Alt>+Key generates ESC N Key
195+
return ansiterm.KEY_ESC_N + strings.ToLower(key)
196+
default:
197+
return key
194198
}
195-
196-
// <Alt>+Key generates ESC N Key
197-
if !control && alt {
198-
return ansiterm.KEY_ESC_N + strings.ToLower(string(rune(keyEvent.UnicodeChar)))
199-
}
200-
201-
return string(rune(keyEvent.UnicodeChar))
202199
}
203200

204201
// formatVirtualKey converts a virtual key (e.g., up arrow) into the appropriate ANSI string.
@@ -219,9 +216,9 @@ func formatVirtualKey(key uint16, controlState uint32, escapeSequence []byte) st
219216

220217
// getControlKeys extracts the shift, alt, and ctrl key states.
221218
func getControlKeys(controlState uint32) (shift, alt, control bool) {
222-
shift = 0 != (controlState & winterm.SHIFT_PRESSED)
223-
alt = 0 != (controlState & (winterm.LEFT_ALT_PRESSED | winterm.RIGHT_ALT_PRESSED))
224-
control = 0 != (controlState & (winterm.LEFT_CTRL_PRESSED | winterm.RIGHT_CTRL_PRESSED))
219+
shift = controlState&winterm.SHIFT_PRESSED != 0
220+
alt = controlState&(winterm.LEFT_ALT_PRESSED|winterm.RIGHT_ALT_PRESSED) != 0
221+
control = controlState&(winterm.LEFT_CTRL_PRESSED|winterm.RIGHT_CTRL_PRESSED) != 0
225222
return shift, alt, control
226223
}
227224

windows/ansi_writer.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ type ansiWriter struct {
1818
infoReset *winterm.CONSOLE_SCREEN_BUFFER_INFO
1919
command []byte
2020
escapeSequence []byte
21-
inAnsiSequence bool
2221
parser *ansiterm.AnsiParser
2322
}
2423

0 commit comments

Comments
 (0)