Skip to content

Commit 8162e58

Browse files
committed
Changed types of some member variables in structs wherever possible and added nolint:gosec declarative if it required more work
Signed-off-by: HoustonBoston <rorajesh@redhat.com>
1 parent e547586 commit 8162e58

10 files changed

Lines changed: 45 additions & 27 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,4 @@ linters-settings:
4949
forbid-focus-container: true
5050
nakedret:
5151
max-func-lines: 0
52-
gosec:
53-
excludes:
54-
# This is changing things up quite a bit and requires evaluation
55-
# per conversion
56-
- G115
52+

cmd/cdi-controller/controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ func main() {
372372
klog.Fatalf("Unable to get environment variables: %v\n", errors.WithStack(err))
373373
}
374374

375-
logf.SetLogger(zap.New(zap.Level(zapcore.Level(-1*verbosityLevel)), zap.UseDevMode(debug)))
375+
logf.SetLogger(zap.New(zap.Level(zapcore.Level(-1*int8(verbosityLevel))), zap.UseDevMode(debug))) //nolint:gosec
376+
// using nolint since Atoi, used for verbosityLevel, strictly returns an int
376377
logf.Log.WithName("main").Info("Verbosity level", "verbose", verbose, "debug", debug)
377378

378379
if err = createReadyFile(); err != nil {

cmd/cdi-operator/operator.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ func main() {
8383
// implementing the logr.Logger interface. This logger will
8484
// be propagated through the whole operator, generating
8585
// uniform and structured logs.
86-
logf.SetLogger(zap.New(zap.Level(zapcore.Level(-1*verbosityLevel)), zap.UseDevMode(debug)))
86+
// Note: zapcore.Level is an int8, so we need to convert the verbosityLevel (which has no reason to be an int) to an int8.
87+
// using nolint since Atoi, used for verbosityLevel, strictly returns an int
88+
logf.SetLogger(zap.New(zap.Level(zapcore.Level(-1*int8(verbosityLevel))), zap.UseDevMode(debug))) //nolint:gosec
8789

8890
printVersion()
8991

pkg/importer/file.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func GetAvailableSpace(path string) (int64, error) {
5858
return int64(-1), err
5959
}
6060
//nolint:unconvert
61-
return int64(stat.Bavail) * int64(stat.Bsize), nil
61+
return int64(stat.Bavail) * int64(stat.Bsize), nil //nolint:gosec
6262
}
6363

6464
// GetAvailableSpaceBlock gets the amount of available space at the block device path specified.

pkg/importer/imageio-datasource.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ func (is *ImageioDataSource) StreamExtents(extentsReader *extentReader, fileName
294294
return err
295295
}
296296
isBlock := !info.Mode().IsRegular()
297-
preallocated := info.Size() >= int64(is.contentLength)
297+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
298+
preallocated := info.Size() >= int64(is.contentLength) //nolint:gosec
298299

299300
// Choose seek for regular files, and hole punching for block devices and pre-allocated files
300301
zeroRange := AppendZeroWithTruncate
@@ -314,7 +315,8 @@ func (is *ImageioDataSource) StreamExtents(extentsReader *extentReader, fileName
314315
return errors.Wrap(err, "failed to zero range on destination")
315316
}
316317
}
317-
is.readers.progressReader.Current += uint64(extent.Length)
318+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
319+
is.readers.progressReader.Current += uint64(extent.Length) //nolint:gosec
318320
} else {
319321
klog.Infof("Downloading %d-byte extent at offset %d", extent.Length, extent.Start)
320322
responseBody, err := extentsReader.GetRange(extent.Start, extent.Start+extent.Length-1)
@@ -584,7 +586,8 @@ func createImageioReader(ctx context.Context, ep string, accessKey string, secKe
584586
total = 0
585587
nonzero := int64(0)
586588
for _, extent := range extents {
587-
total += uint64(extent.Length)
589+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
590+
total += uint64(extent.Length) //nolint:gosec
588591
if !extent.Zero {
589592
nonzero += extent.Length
590593
}
@@ -595,7 +598,8 @@ func createImageioReader(ctx context.Context, ep string, accessKey string, secKe
595598
client: client,
596599
extents: extents,
597600
transferURL: transferURL,
598-
size: int64(total),
601+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
602+
size: int64(total), //nolint:gosec
599603
}
600604
} else {
601605
req, err := http.NewRequest(http.MethodGet, transferURL, nil)
@@ -906,7 +910,8 @@ func getTransfer(conn ConnectionInterface, disk *ovirtsdk4.Disk, snapshot *ovirt
906910
}
907911
}
908912

909-
return it, uint64(totalSize), nil
913+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
914+
return it, uint64(totalSize), nil //nolint:gosec
910915
}
911916

912917
func loadCA(certDir string) (*x509.CertPool, error) {

pkg/importer/vddk-datasource_amd64.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,8 @@ func GetBlockStatus(handle NbdOperations, extent types.DiskChangeExtent) []*Bloc
746746
blocks = []*BlockStatusData{block}
747747
return blocks
748748
}
749-
err := handle.BlockStatus(uint64(length), uint64(lastOffset), updateBlocksCallback, &fixedOptArgs)
749+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
750+
err := handle.BlockStatus(uint64(length), uint64(lastOffset), updateBlocksCallback, &fixedOptArgs) //nolint:gosec
750751
if err != nil {
751752
klog.Errorf("Error getting block status at offset %d, returning whole block instead. Error was: %v", lastOffset, err)
752753
return createWholeBlock()
@@ -792,13 +793,15 @@ func CopyRange(handle NbdOperations, sink VDDKDataSink, block *BlockStatusData,
792793
length := len(buffer)
793794

794795
offset := block.Offset + count
795-
err := handle.Pread(buffer, uint64(offset), nil)
796+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
797+
err := handle.Pread(buffer, uint64(offset), nil) //nolint:gosec
796798
if err != nil {
797799
klog.Errorf("Error reading from source at offset %d: %v", offset, err)
798800
return err
799801
}
800802

801-
written, err := sink.Pwrite(buffer, uint64(offset))
803+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
804+
written, err := sink.Pwrite(buffer, uint64(offset)) //nolint:gosec
802805
if err != nil {
803806
klog.Errorf("Failed to write data block at offset %d to local file: %v", block.Offset, err)
804807
return err
@@ -851,7 +854,8 @@ func createVddkDataSink(destinationFile string, size uint64, volumeMode v1.Persi
851854

852855
// Pwrite writes the given byte buffer to the sink at the given offset
853856
func (sink *VDDKFileSink) Pwrite(buffer []byte, offset uint64) (int, error) {
854-
written, err := syscall.Pwrite(int(sink.file.Fd()), buffer, int64(offset))
857+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
858+
written, err := syscall.Pwrite(int(sink.file.Fd()), buffer, int64(offset)) //nolint:gosec
855859
blocksize := len(buffer)
856860
if written < blocksize {
857861
klog.Infof("Wrote less than blocksize (%d): %d", blocksize, written)
@@ -908,7 +912,8 @@ func (sink *VDDKFileSink) ZeroRange(offset int64, length int64) error {
908912
if remaining < blocksize {
909913
buffer = bytes.Repeat([]byte{0}, int(remaining))
910914
}
911-
written, err := sink.Pwrite(buffer, uint64(offset))
915+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
916+
written, err := sink.Pwrite(buffer, uint64(offset)) //nolint:gosec
912917
if err != nil {
913918
klog.Errorf("Unable to write %d zeroes at offset %d: %v", length, offset, err)
914919
break
@@ -1120,7 +1125,8 @@ func (vs *VDDKDataSource) TransferFile(fileName string, preallocation bool) (Pro
11201125
initialProgressTime := time.Now()
11211126
updateProgress := func(written int) {
11221127
// Only log progress at approximately 1% minimum intervals.
1123-
currentProgressBytes += uint64(written)
1128+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
1129+
currentProgressBytes += uint64(written) //nolint:gosec
11241130
currentProgressPercent := uint(100.0 * (float64(currentProgressBytes) / float64(vs.Size)))
11251131
if currentProgressPercent > previousProgressPercent {
11261132
progressMessage := fmt.Sprintf("Transferred %d/%d bytes (%d%%)", currentProgressBytes, vs.Size, currentProgressPercent)
@@ -1304,8 +1310,11 @@ func (vs *VDDKDataSource) TransferFile(fileName string, preallocation bool) (Pro
13041310
}
13051311

13061312
extent := types.DiskChangeExtent{
1307-
Length: int64(blocksize),
1308-
Start: int64(i),
1313+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
1314+
Length: int64(blocksize), //nolint:gosec
1315+
1316+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
1317+
Start: int64(i), //nolint:gosec
13091318
}
13101319

13111320
blocks := GetBlockStatus(vs.NbdKit.Handle, extent)

pkg/importer/vddk-datasource_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,8 @@ func defaultMockNbdFunctions() mockNbdFunctions {
10321032
}
10331033
ops.BlockStatus = func(length uint64, offset uint64, callback libnbd.ExtentCallback, optargs *libnbd.BlockStatusOptargs) error {
10341034
err := 0
1035-
callback("base:allocation", offset, []uint32{uint32(length), 0}, &err)
1035+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
1036+
callback("base:allocation", offset, []uint32{uint32(length), 0}, &err) //nolint:gosec
10361037
return nil
10371038
}
10381039
return ops
@@ -1095,14 +1096,16 @@ type mockVddkDataSink struct {
10951096

10961097
func (sink *mockVddkDataSink) ZeroRange(offset int64, length int64) error {
10971098
buf := bytes.Repeat([]byte{0x00}, int(length))
1098-
_, err := sink.Pwrite(buf, uint64(offset))
1099+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
1100+
_, err := sink.Pwrite(buf, uint64(offset)) //nolint:gosec
10991101
return err
11001102
}
11011103

11021104
func (sink *mockVddkDataSink) Pwrite(buf []byte, offset uint64) (int, error) {
11031105
copy(mockSinkBuffer[offset:offset+uint64(len(buf))], buf)
11041106
if len(buf) > sink.position {
1105-
sink.position = int(offset) + len(buf)
1107+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
1108+
sink.position = int(offset) + len(buf) //nolint:gosec
11061109
}
11071110
return len(buf), nil
11081111
}

pkg/system/prlimit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ func executeWithLimits(limits *ProcessLimitValues, callback func(string), logErr
127127

128128
if limits != nil && limits.CPUTimeLimit > 0 {
129129
klog.V(3).Infof("Setting CPU limit to %d\n", limits.CPUTimeLimit)
130-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(limits.CPUTimeLimit)*time.Second)
130+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration((limits.CPUTimeLimit)*uint64(time.Second))) //nolint:gosec
131+
// using nolint since time can be negative apparently
131132
defer cancel()
132133
cmd = execCommandContext(ctx, command, args...)
133134
} else {

pkg/util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func ParseEnvVar(envVarName string, decode bool) (string, error) {
8383
// Read reads bytes from the stream and updates the prometheus clone_progress metric according to the progress.
8484
func (r *CountingReader) Read(p []byte) (n int, err error) {
8585
n, err = r.Reader.Read(p)
86-
r.Current += uint64(n)
86+
r.Current += uint64(n) //nolint:gosec
8787
r.Done = errors.Is(err, io.EOF)
8888
return n, err
8989
}

tests/imageio-inventory.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ func getSnapshotSize(snapshot string) uint64 {
492492
path := getSnapshotPath(snapshot)
493493
info, err := os.Stat(path)
494494
gomega.Expect(err).ToNot(gomega.HaveOccurred())
495-
return uint64(info.Size())
495+
// using nolint since changing the type would cause multiple type mismatch errors to the point of it being better to just ignore the error
496+
return uint64(info.Size()) //nolint:gosec
496497
}
497498

498499
// Get snapshot type from file extension, just raw or cow.

0 commit comments

Comments
 (0)