Skip to content

Commit

Permalink
tests: Add codeql-analysis.yml (#642)
Browse files Browse the repository at this point in the history
* Create codeql-analysis.yml

* s2c: Convert directly to target types
  • Loading branch information
klauspost committed Jul 13, 2022
1 parent d8d6319 commit b7c973d
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 17 deletions.
72 changes: 72 additions & 0 deletions .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
14 changes: 7 additions & 7 deletions s2/cmd/s2c/main.go
Expand Up @@ -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())
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -612,26 +612,26 @@ 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 {
firstLetter = len(size)
}

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)
}
Expand Down
21 changes: 12 additions & 9 deletions s2/cmd/s2d/main.go
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down
7 changes: 6 additions & 1 deletion s2/decode.go
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io"
"io/ioutil"
"math"
"runtime"
"sync"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b7c973d

Please sign in to comment.