Skip to content

Commit b2e3a3b

Browse files
committed
refactor: fix gosec linter warnings
Signed-off-by: Christian Stewart <christian@aperture.us>
1 parent 80d1ca1 commit b2e3a3b

File tree

7 files changed

+285
-40
lines changed

7 files changed

+285
-40
lines changed

cmd/kvfile/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"io"
7+
"math"
78
"os"
89

910
"github.com/aperturerobotics/go-kvfile"
@@ -178,6 +179,10 @@ func main() {
178179
return kvfile.Write(file, keys, func(wr io.Writer, key []byte) (uint64, error) {
179180
val := data[string(key)]
180181
n, err := wr.Write([]byte(val))
182+
// Check non-negative before conversion
183+
if n < 0 {
184+
return 0, errors.Wrap(err, "writer returned negative bytes written")
185+
}
181186
return uint64(n), err
182187
})
183188
},
@@ -256,6 +261,10 @@ func printAll(reader *kvfile.Reader) error {
256261
key := indexEntry.GetKey()
257262
printData(key, binKeys)
258263

264+
// Check for overflow before converting i to int
265+
if i > uint64(math.MaxInt) {
266+
return errors.Errorf("key index %v overflows int", i)
267+
}
259268
val, err := reader.GetWithEntry(indexEntry, int(i))
260269
if err != nil {
261270
return err

compress/compress.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
seekable "github.com/SaveTheRbtz/zstd-seekable-format-go"
77
kvfile "github.com/aperturerobotics/go-kvfile"
88
"github.com/klauspost/compress/zstd"
9+
"github.com/pkg/errors"
910
)
1011

1112
// UseCompressedWriter builds a compressed writer and closes it after the
@@ -68,6 +69,12 @@ func BuildCompressReader(rd ReadSeekerAt) (*kvfile.Reader, func(), error) {
6869
_ = r.Close()
6970
return nil, nil, err
7071
}
72+
// Check non-negative before conversion
73+
if size < 0 {
74+
dec.Close()
75+
_ = r.Close()
76+
return nil, nil, errors.Errorf("seek returned negative size: %d", size)
77+
}
7178
kvReader, err := kvfile.BuildReader(r, uint64(size))
7279
if err != nil {
7380
dec.Close()

compress/compress_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package kvfile_compress
22

33
import (
44
"bytes"
5+
"errors"
56
"io"
67
"testing"
78
)
@@ -25,6 +26,10 @@ func TestKvCompress(t *testing.T) {
2526
if err != nil {
2627
return 0, err
2728
}
29+
// Check non-negative before conversion
30+
if nw < 0 {
31+
return 0, errors.New("writer returned negative bytes written")
32+
}
2833
index++
2934
return uint64(nw), nil
3035
})

errors.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package kvfile
2+
3+
import "errors"
4+
5+
// Predefined errors for the kvfile package.
6+
var (
7+
// ErrFileSizeTooSmallForIndexCount indicates the file size is too small to contain the index count.
8+
ErrFileSizeTooSmallForIndexCount = errors.New("file size too small for index count")
9+
// ErrMaxIndexEntrySizeNegative indicates the configured maxIndexEntrySize is negative.
10+
ErrMaxIndexEntrySizeNegative = errors.New("maxIndexEntrySize is negative")
11+
// ErrBufferCapacityTooSmall indicates an internal buffer had insufficient capacity.
12+
ErrBufferCapacityTooSmall = errors.New("buffer capacity less than 10")
13+
// ErrNegativeIndexBinarySearch indicates a negative index was calculated during binary search.
14+
ErrNegativeIndexBinarySearch = errors.New("negative index calculated in binary search")
15+
// ErrNegativeIndexCalculated indicates a negative index was calculated.
16+
ErrNegativeIndexCalculated = errors.New("negative index calculated")
17+
// ErrEntryValueNotFound indicates the value for a given index entry could not be located.
18+
ErrEntryValueNotFound = errors.New("entry value not found")
19+
// ErrNegativeIndexScan indicates a negative index was encountered during a scan operation.
20+
ErrNegativeIndexScan = errors.New("negative index in scan")
21+
)

0 commit comments

Comments
 (0)