diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml new file mode 100644 index 0000000000..10788d5b47 --- /dev/null +++ b/.github/workflows/codeql-analysis.yml @@ -0,0 +1,72 @@ +# For most projects, this workflow file will not need changing; you simply need +# to commit it to your repository. +# +# You may wish to alter this file to override the set of languages analyzed, +# or to provide custom queries or build logic. +# +# ******** NOTE ******** +# We have attempted to detect the languages in your repository. Please check +# the `language` matrix defined below to confirm you have the correct set of +# supported CodeQL languages. +# +name: "CodeQL" + +on: + push: + branches: [ "master" ] + pull_request: + # The branches below must be a subset of the branches above + branches: [ "master" ] + schedule: + - cron: '31 1 * * 5' + +jobs: + analyze: + name: Analyze + runs-on: ubuntu-latest + permissions: + actions: read + contents: read + security-events: write + + strategy: + fail-fast: false + matrix: + language: [ 'go' ] + # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ] + # Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + # Initializes the CodeQL tools for scanning. + - name: Initialize CodeQL + uses: github/codeql-action/init@v2 + with: + languages: ${{ matrix.language }} + # If you wish to specify custom queries, you can do so here or in a config file. + # By default, queries listed here will override any specified in a config file. + # Prefix the list here with "+" to use these queries and those in the config file. + + # Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs + # queries: security-extended,security-and-quality + + + # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). + # If this step fails, then you should remove it and run the build manually (see below) + - name: Autobuild + uses: github/codeql-action/autobuild@v2 + + # ℹī¸ Command-line programs to run using the OS shell. + # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun + + # If the Autobuild fails above, remove it and uncomment the following three lines. + # modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance. + + # - run: | + # echo "Run, Build Application using script" + # ./location_of_script_within_repo/buildscript.sh + + - name: Perform CodeQL Analysis + uses: github/codeql-action/analyze@v2 diff --git a/s2/cmd/s2c/main.go b/s2/cmd/s2c/main.go index 4292d62783..9e913562ac 100644 --- a/s2/cmd/s2c/main.go +++ b/s2/cmd/s2c/main.go @@ -91,7 +91,7 @@ Options:`) flag.PrintDefaults() os.Exit(0) } - opts := []s2.WriterOption{s2.WriterBlockSize(int(sz)), s2.WriterConcurrency(*cpu), s2.WriterPadding(int(pad))} + opts := []s2.WriterOption{s2.WriterBlockSize(sz), s2.WriterConcurrency(*cpu), s2.WriterPadding(pad)} if *index { opts = append(opts, s2.WriterAddIndex()) } @@ -123,7 +123,7 @@ Options:`) dstFile, err := os.OpenFile(*out, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm) exitErr(err) defer dstFile.Close() - bw := bufio.NewWriterSize(dstFile, int(sz)*2) + bw := bufio.NewWriterSize(dstFile, sz*2) defer bw.Flush() wr.Reset(bw) } @@ -612,7 +612,7 @@ func exitErr(err error) { } // toSize converts a size indication to bytes. -func toSize(size string) (uint64, error) { +func toSize(size string) (int, error) { size = strings.ToUpper(strings.TrimSpace(size)) firstLetter := strings.IndexFunc(size, unicode.IsLetter) if firstLetter == -1 { @@ -620,18 +620,18 @@ func toSize(size string) (uint64, error) { } bytesString, multiple := size[:firstLetter], size[firstLetter:] - bytes, err := strconv.ParseUint(bytesString, 10, 64) + sz, err := strconv.Atoi(bytesString) if err != nil { return 0, fmt.Errorf("unable to parse size: %v", err) } switch multiple { case "M", "MB", "MIB": - return bytes * 1 << 20, nil + return sz * 1 << 20, nil case "K", "KB", "KIB": - return bytes * 1 << 10, nil + return sz * 1 << 10, nil case "B", "": - return bytes, nil + return sz, nil default: return 0, fmt.Errorf("unknown size suffix: %v", multiple) } diff --git a/s2/cmd/s2d/main.go b/s2/cmd/s2d/main.go index 5fc0abe692..8f7030183d 100644 --- a/s2/cmd/s2d/main.go +++ b/s2/cmd/s2d/main.go @@ -292,9 +292,9 @@ Options:`) rs, err := r.ReadSeeker(tailBytes > 0, nil) exitErr(err) if tailBytes > 0 { - _, err = rs.Seek(-int64(tailBytes), io.SeekEnd) + _, err = rs.Seek(-tailBytes, io.SeekEnd) } else { - _, err = rs.Seek(int64(offset), io.SeekStart) + _, err = rs.Seek(offset, io.SeekStart) } exitErr(err) } @@ -408,7 +408,7 @@ func (w *rCountSeeker) BytesRead() int64 { } // toSize converts a size indication to bytes. -func toSize(size string) (uint64, error) { +func toSize(size string) (int64, error) { if len(size) == 0 { return 0, nil } @@ -419,22 +419,25 @@ func toSize(size string) (uint64, error) { } bytesString, multiple := size[:firstLetter], size[firstLetter:] - bytes, err := strconv.ParseUint(bytesString, 10, 64) + sz, err := strconv.ParseInt(bytesString, 10, 64) if err != nil { return 0, fmt.Errorf("unable to parse size: %v", err) } + if sz < 0 { + return 0, errors.New("negative size given") + } switch multiple { case "T", "TB", "TIB": - return bytes * 1 << 40, nil + return sz * 1 << 40, nil case "G", "GB", "GIB": - return bytes * 1 << 30, nil + return sz * 1 << 30, nil case "M", "MB", "MIB": - return bytes * 1 << 20, nil + return sz * 1 << 20, nil case "K", "KB", "KIB": - return bytes * 1 << 10, nil + return sz * 1 << 10, nil case "B", "": - return bytes, nil + return sz, nil default: return 0, fmt.Errorf("unknown size suffix: %v", multiple) } diff --git a/s2/decode.go b/s2/decode.go index b5fa4d3f00..27c0f3c2c4 100644 --- a/s2/decode.go +++ b/s2/decode.go @@ -11,6 +11,7 @@ import ( "fmt" "io" "io/ioutil" + "math" "runtime" "sync" ) @@ -719,7 +720,11 @@ func (r *Reader) Skip(n int64) error { // decoded[i:j] contains decoded bytes that have not yet been passed on. left := int64(r.j - r.i) if left >= n { - r.i += int(n) + tmp := int64(r.i) + n + if tmp > math.MaxInt32 { + return errors.New("s2: internal overflow in skip") + } + r.i = int(tmp) return nil } n -= int64(r.j - r.i)