Skip to content

Commit

Permalink
fix: Lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bodgit committed Aug 29, 2024
1 parent d263b66 commit 5ffe3ff
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 32 deletions.
20 changes: 18 additions & 2 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ linters:
- asciicheck
- bidichk
- bodyclose
- canonicalheader
- containedctx
- contextcheck
- copyloopvar
- cyclop
- decorder
- dogsled
Expand All @@ -18,15 +20,17 @@ linters:
- errchkjson
- errname
- errorlint
- execinquery
- exhaustive
- exportloopref
- fatcontext
- forbidigo
- forcetypeassert
- funlen
- gci
- ginkgolinter
- gocheckcompilerdirectives
- gochecknoglobals
- gochecknoinits
- gochecksumtype
- gocognit
- goconst
- gocritic
Expand All @@ -41,16 +45,21 @@ linters:
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- importas
- inamedparam
- ineffassign
- interfacebloat
- intrange
- lll
- loggercheck
- maintidx
- makezero
- mirror
- misspell
- musttag
- nakedret
- nestif
- nilerr
Expand All @@ -60,18 +69,24 @@ linters:
- nolintlint
- nosprintfhostport
- paralleltest
- perfsprint
- prealloc
- predeclared
- promlinter
- protogetter
- reassign
- revive
- rowserrcheck
- sloglint
- spancheck
- sqlclosecheck
- staticcheck
- stylecheck
- tagalign
- tagliatelle
- tenv
- testableexamples
- testifylint
- testpackage
- thelper
- tparallel
Expand All @@ -83,3 +98,4 @@ linters:
- wastedassign
- whitespace
- wsl
- zerologlint
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ repos:
hooks:
- id: commitizen
- repo: https://github.com/golangci/golangci-lint
rev: v1.54.1
rev: v1.60.3
hooks:
- id: golangci-lint
- repo: https://github.com/gitleaks/gitleaks
Expand Down
1 change: 1 addition & 0 deletions internal/aes7z/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func calculateKey(password string, cycles int, salt []byte) ([]byte, error) {
_, _ = h.Write(b.Bytes())
_ = binary.Write(h, binary.LittleEndian, i)
}

copy(key, h.Sum(nil))
}

Expand Down
4 changes: 2 additions & 2 deletions internal/bcj2/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type readCloser struct {
sd [256 + 2]uint

previous byte
written uint64
written uint32

buf *bytes.Buffer
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func (rc *readCloser) read() error {
return err
}

dest -= uint32(rc.written + 4)
dest -= rc.written + 4
_ = binary.Write(rc.buf, binary.LittleEndian, dest)

rc.previous = byte(dest >> 24)
Expand Down
16 changes: 8 additions & 8 deletions internal/bra/bcj.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func (c *bcj) Convert(b []byte, encoding bool) int {
}

var (
pos int
pos uint32
mask = c.state & 7
)

for {
p := pos
for ; p < len(b)-bcjLookAhead; p++ {
for ; int(p) < len(b)-bcjLookAhead; p++ {
if b[p]&0xfe == 0xe8 {
break
}
Expand All @@ -39,23 +39,23 @@ func (c *bcj) Convert(b []byte, encoding bool) int {
d := p - pos
pos = p

if p >= len(b)-bcjLookAhead {
if int(p) >= len(b)-bcjLookAhead {
if d > 2 {
c.state = 0
} else {
c.state = mask >> d
}

c.ip += uint32(pos)
c.ip += pos

return pos
return int(pos)
}

if d > 2 {
mask = 0
} else {
mask >>= d
if mask != 0 && (mask > 4 || mask == 3 || test86MSByte(b[p+int(mask>>1)+1])) {
if mask != 0 && (mask > 4 || mask == 3 || test86MSByte(b[p+(mask>>1)+1])) {
mask = (mask >> 1) | 4
pos++

Expand All @@ -66,8 +66,8 @@ func (c *bcj) Convert(b []byte, encoding bool) int {
//nolint:nestif
if test86MSByte(b[p+4]) {
v := binary.LittleEndian.Uint32(b[p+1:])
cur := c.ip + uint32(c.Size()+pos)
pos += c.Size()
cur := c.ip + uint32(c.Size()) + pos //nolint:gosec
pos += uint32(c.Size()) //nolint:gosec

if encoding {
v += cur
Expand Down
2 changes: 1 addition & 1 deletion internal/bra/bra.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package bra

type converter interface {
Size() int
Convert([]byte, bool) int
Convert(b []byte, encoding bool) int
}
2 changes: 2 additions & 0 deletions internal/bra/minmax_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package bra

//nolint:predeclared
func min(x, y int) int {
if x < y {
return x
Expand All @@ -10,6 +11,7 @@ func min(x, y int) int {
return y
}

//nolint:predeclared
func max(x, y int) int {
if x > y {
return x
Expand Down
4 changes: 2 additions & 2 deletions internal/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

// Pooler is the interface implemented by a pool.
type Pooler interface {
Get(int64) (util.SizeReadSeekCloser, bool)
Put(int64, util.SizeReadSeekCloser) (bool, error)
Get(offset int64) (util.SizeReadSeekCloser, bool)
Put(offset int64, rc util.SizeReadSeekCloser) (bool, error)
}

// Constructor is the function prototype used to instantiate a pool.
Expand Down
8 changes: 4 additions & 4 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (f *File) Open() (io.ReadCloser, error) {
return &fileReader{
rc: rc,
f: f,
n: int64(f.UncompressedSize),
n: int64(f.UncompressedSize), //nolint:gosec
}, nil
}

Expand Down Expand Up @@ -349,7 +349,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
}

// Seek over the streams
if z.end, err = sr.Seek(int64(start.Offset), io.SeekCurrent); err != nil {
if z.end, err = sr.Seek(int64(start.Offset), io.SeekCurrent); err != nil { //nolint:gosec
return err
}

Expand All @@ -359,7 +359,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
h.Reset()

// Bound bufio.Reader otherwise it can read trailing garbage which screws up the CRC check
br := bufio.NewReader(io.NewSectionReader(tra, z.end, int64(start.Size)))
br := bufio.NewReader(io.NewSectionReader(tra, z.end, int64(start.Size))) //nolint:gosec

id, err := br.ReadByte()
if err != nil {
Expand Down Expand Up @@ -449,7 +449,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
}

f.offset = offset
offset += int64(f.UncompressedSize)
offset += int64(f.UncompressedSize) //nolint:gosec
folder = f.folder
j++
}
Expand Down
18 changes: 9 additions & 9 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var errAlgorithm = errors.New("sevenzip: unsupported compression algorithm")

// CryptoReadCloser adds a Password method to decompressors.
type CryptoReadCloser interface {
Password(string) error
Password(password string) error
}

type signatureHeader struct {
Expand Down Expand Up @@ -97,7 +97,7 @@ func (f *folder) coderReader(readers []io.ReadCloser, coder uint64, password str
}
}

return plumbing.LimitReadCloser(cr, int64(f.size[coder])), nil
return plumbing.LimitReadCloser(cr, int64(f.size[coder])), nil //nolint:gosec
}

type folderReadCloser struct {
Expand All @@ -118,7 +118,7 @@ func (rc *folderReadCloser) Seek(offset int64, whence int) (int64, error) {
case io.SeekStart:
newo = offset
case io.SeekCurrent:
newo = int64(rc.wc.Count()) + offset
newo = int64(rc.wc.Count()) + offset //nolint:gosec
case io.SeekEnd:
newo = rc.Size() + offset
default:
Expand All @@ -129,15 +129,15 @@ func (rc *folderReadCloser) Seek(offset int64, whence int) (int64, error) {
return 0, errors.New("negative seek")
}

if newo < int64(rc.wc.Count()) {
if uint64(newo) < rc.wc.Count() {
return 0, errors.New("cannot seek backwards")
}

if newo > rc.Size() {
return 0, errors.New("cannot seek beyond EOF")
}

if _, err := io.CopyN(io.Discard, rc, newo-int64(rc.wc.Count())); err != nil {
if _, err := io.CopyN(io.Discard, rc, newo-int64(rc.wc.Count())); err != nil { //nolint:gosec
return 0, err
}

Expand Down Expand Up @@ -232,7 +232,7 @@ func (si *streamsInfo) folderOffset(folder int) int64 {
k += si.unpackInfo.folder[i].packedStreams
}

return int64(si.packInfo.position + offset)
return int64(si.packInfo.position + offset) //nolint:gosec
}

//nolint:cyclop,funlen
Expand All @@ -249,7 +249,7 @@ func (si *streamsInfo) FolderReader(r io.ReaderAt, folder int, password string)
offset := int64(0)

for i, input := range f.packed {
size := int64(si.packInfo.size[packedOffset+i])
size := int64(si.packInfo.size[packedOffset+i]) //nolint:gosec
in[input] = util.NopCloser(bufio.NewReader(io.NewSectionReader(r, si.folderOffset(folder)+offset, size)))
offset += size
}
Expand Down Expand Up @@ -297,7 +297,7 @@ func (si *streamsInfo) FolderReader(r io.ReaderAt, folder int, password string)
return nil, 0, errors.New("expecting one unbound output stream")
}

fr := newFolderReadCloser(out[unbound[0]], int64(f.unpackSize()))
fr := newFolderReadCloser(out[unbound[0]], int64(f.unpackSize())) //nolint:gosec

if si.unpackInfo.digest != nil {
return fr, si.unpackInfo.digest[folder], nil
Expand Down Expand Up @@ -344,7 +344,7 @@ type headerFileInfo struct {
}

func (fi headerFileInfo) Name() string { return path.Base(fi.fh.Name) }
func (fi headerFileInfo) Size() int64 { return int64(fi.fh.UncompressedSize) }
func (fi headerFileInfo) Size() int64 { return int64(fi.fh.UncompressedSize) } //nolint:gosec
func (fi headerFileInfo) IsDir() bool { return fi.Mode().IsDir() }
func (fi headerFileInfo) ModTime() time.Time { return fi.fh.Modified.UTC() }
func (fi headerFileInfo) Mode() fs.FileMode { return fi.fh.Mode() }
Expand Down
6 changes: 3 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func readCoder(r util.Reader) (*coder, error) {
}

c.properties = make([]byte, size)
if n, err := r.Read(c.properties); err != nil || n != int(size) {
if n, err := r.Read(c.properties); err != nil || uint64(n) != size {
if err != nil {
return nil, fmt.Errorf("readCoder: Read error: %w", err)
}
Expand Down Expand Up @@ -605,7 +605,7 @@ func readNames(r util.Reader, count, length uint64) ([]string, error) {
}

utf16le := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
scanner := bufio.NewScanner(transform.NewReader(io.LimitReader(r, int64(length-1)), utf16le.NewDecoder()))
scanner := bufio.NewScanner(transform.NewReader(io.LimitReader(r, int64(length-1)), utf16le.NewDecoder())) //nolint:gosec,lll
scanner.Split(splitNull)

names, i := make([]string, 0, count), uint64(0)
Expand Down Expand Up @@ -765,7 +765,7 @@ func readFilesInfo(r util.Reader) (*filesInfo, error) {
case idStartPos:
return nil, errors.New("sevenzip: TODO idStartPos") //nolint:goerr113
case idDummy:
if _, err := io.CopyN(io.Discard, r, int64(length)); err != nil {
if _, err := io.CopyN(io.Discard, r, int64(length)); err != nil { //nolint:gosec
return nil, fmt.Errorf("readFilesInfo: CopyN error: %w", err)
}
default:
Expand Down

0 comments on commit 5ffe3ff

Please sign in to comment.