diff --git a/.golangci.yaml b/.golangci.yaml index 018a766..ee8fbd0 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -6,8 +6,10 @@ linters: - asciicheck - bidichk - bodyclose + - canonicalheader - containedctx - contextcheck + - copyloopvar - cyclop - decorder - dogsled @@ -18,15 +20,17 @@ linters: - errchkjson - errname - errorlint - - execinquery - exhaustive - - exportloopref + - fatcontext - forbidigo - forcetypeassert - funlen - gci + - ginkgolinter + - gocheckcompilerdirectives - gochecknoglobals - gochecknoinits + - gochecksumtype - gocognit - goconst - gocritic @@ -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 @@ -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 @@ -83,3 +98,4 @@ linters: - wastedassign - whitespace - wsl + - zerologlint diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a75fd41..ee68def 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/internal/aes7z/key.go b/internal/aes7z/key.go index cdcf151..79c78dd 100644 --- a/internal/aes7z/key.go +++ b/internal/aes7z/key.go @@ -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)) } diff --git a/internal/bcj2/reader.go b/internal/bcj2/reader.go index 5e0bb0a..343ec5f 100644 --- a/internal/bcj2/reader.go +++ b/internal/bcj2/reader.go @@ -49,7 +49,7 @@ type readCloser struct { sd [256 + 2]uint previous byte - written uint64 + written uint32 buf *bytes.Buffer } @@ -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) diff --git a/internal/bra/bcj.go b/internal/bra/bcj.go index 40fc7c0..05f1fdf 100644 --- a/internal/bra/bcj.go +++ b/internal/bra/bcj.go @@ -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 } @@ -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++ @@ -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 diff --git a/internal/bra/bra.go b/internal/bra/bra.go index bcb0698..a2f0daa 100644 --- a/internal/bra/bra.go +++ b/internal/bra/bra.go @@ -2,5 +2,5 @@ package bra type converter interface { Size() int - Convert([]byte, bool) int + Convert(b []byte, encoding bool) int } diff --git a/internal/bra/minmax_compat.go b/internal/bra/minmax_compat.go index 403bca8..8004c6a 100644 --- a/internal/bra/minmax_compat.go +++ b/internal/bra/minmax_compat.go @@ -2,6 +2,7 @@ package bra +//nolint:predeclared func min(x, y int) int { if x < y { return x @@ -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 diff --git a/internal/pool/pool.go b/internal/pool/pool.go index 3389fb0..4cb61cf 100644 --- a/internal/pool/pool.go +++ b/internal/pool/pool.go @@ -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. diff --git a/reader.go b/reader.go index c3ebb5a..fb5b56d 100644 --- a/reader.go +++ b/reader.go @@ -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 } @@ -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 } @@ -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 { @@ -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++ } diff --git a/struct.go b/struct.go index f87927c..f3ce50e 100644 --- a/struct.go +++ b/struct.go @@ -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 { @@ -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 { @@ -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: @@ -129,7 +129,7 @@ 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") } @@ -137,7 +137,7 @@ func (rc *folderReadCloser) Seek(offset int64, whence int) (int64, error) { 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 } @@ -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 @@ -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 } @@ -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 @@ -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() } diff --git a/types.go b/types.go index fb5c4d0..c518d41 100644 --- a/types.go +++ b/types.go @@ -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) } @@ -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) @@ -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: