Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved g115 golanglint warning #5871

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ linters-settings:
excludes:
- G104 # G104: Errors unhandled; (TODO: reduce unhandled errors, or explicitly ignore)
- G113 # G113: Potential uncontrolled memory consumption in Rat.SetString (CVE-2022-23772); (only affects go < 1.16.14. and go < 1.17.7)
- G115 # G115: integer overflow conversion; (TODO: verify these: https://github.com/docker/cli/issues/5584)
- G306 # G306: Expect WriteFile permissions to be 0600 or less (too restrictive; also flags "0o644" permissions)
- G307 # G307: Deferring unsafe method "*os.File" on type "Close" (also EXC0008); (TODO: evaluate these and fix where needed: G307: Deferring unsafe method "*os.File" on type "Close")
govet:
Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func copyProgress(ctx context.Context, dst io.Writer, header string, total *int6
}

// Write to the buffer first to avoid flickering and context switching
fmt.Fprint(buf, aec.Column(uint(len(header)+1)))
fmt.Fprint(buf, aec.Column(uint(len(header)+1))) // #nosec G115 -- Ignore "integer overflow conversion int -> uint" (go len value always start from 0)
fmt.Fprint(buf, aec.EraseLine(aec.EraseModes.Tail))
fmt.Fprint(buf, progressHumanSize(n))

Expand Down
2 changes: 1 addition & 1 deletion cli/command/container/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
BlkioDeviceReadIOps: copts.deviceReadIOps.GetList(),
BlkioDeviceWriteIOps: copts.deviceWriteIOps.GetList(),
IOMaximumIOps: copts.ioMaxIOps,
IOMaximumBandwidth: uint64(copts.ioMaxBandwidth),
IOMaximumBandwidth: uint64(copts.ioMaxBandwidth), // #nosec G115 -- ignore "integer overflow conversion int64 -> uint64" (Using MemBytes value, which alway assumed to be positive)
Ulimits: copts.ulimits.GetList(),
DeviceCgroupRules: copts.deviceCgroupRules.GetAll(),
Devices: deviceMappings,
Expand Down
10 changes: 7 additions & 3 deletions cli/command/container/stats_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,13 @@ func calculateCPUPercentUnix(previousCPU, previousSystem uint64, v *container.St

func calculateCPUPercentWindows(v *container.StatsResponse) float64 {
// Max number of 100ns intervals between the previous time read and now
possIntervals := uint64(v.Read.Sub(v.PreRead).Nanoseconds()) // Start with number of ns intervals
possIntervals /= 100 // Convert to number of 100ns intervals
possIntervals *= uint64(v.NumProcs) // Multiple by the number of processors
preRead := v.Read.Sub(v.PreRead).Nanoseconds()
if preRead <= 0 {
return 0.00 // Avoid calculation with 0 or negative
}
possIntervals := uint64(preRead) // Start with number of ns intervals
possIntervals /= 100 // Convert to number of 100ns intervals
possIntervals *= uint64(v.NumProcs) // Multiple by the number of processors

// Intervals used
intervalsUsed := v.CPUStats.CPUUsage.TotalUsage - v.PreCPUStats.CPUUsage.TotalUsage
Expand Down
14 changes: 7 additions & 7 deletions cli/command/image/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func printImageTree(dockerCLI command.Cli, view treeView) error {

out.PrintlnWithColor(tui.ColorWarning, "WARNING: This is an experimental feature. The output may change and shouldn't be depended on.")

out.Println(generateLegend(out, width))
out.Println(generateLegend(out, int(width))) // #nosec G115 -- ignore "overflow conversion uint -> int", int expansion won't cause lost of value
out.Println()

possibleChips := getPossibleChips(view)
Expand Down Expand Up @@ -259,7 +259,7 @@ func printImageTree(dockerCLI command.Cli, view treeView) error {
},
}

columns = adjustColumns(width, columns, view.images)
columns = adjustColumns(int(width), columns, view.images) // #nosec G115 -- ignore "overflow conversion uint -> int", int expansion won't cause lost of value

// Print columns
for i, h := range columns {
Expand Down Expand Up @@ -289,8 +289,8 @@ func printImageTree(dockerCLI command.Cli, view treeView) error {
// adjustColumns adjusts the width of the first column to maximize the space
// available for image names and removes any columns that would be too narrow
// to display their content.
func adjustColumns(width uint, columns []imgColumn, images []topImage) []imgColumn {
nameWidth := int(width)
func adjustColumns(width int, columns []imgColumn, images []topImage) []imgColumn {
nameWidth := width
for idx, h := range columns {
if h.Width == 0 {
continue
Expand All @@ -316,7 +316,7 @@ func adjustColumns(width uint, columns []imgColumn, images []topImage) []imgColu
return columns
}

func generateLegend(out tui.Output, width uint) string {
func generateLegend(out tui.Output, width int) string {
var legend string
legend += out.Sprint(tui.InfoHeader)
for idx, chip := range allChips {
Expand All @@ -327,7 +327,7 @@ func generateLegend(out tui.Output, width uint) string {
}
legend += " "

r := int(width) - tui.Width(legend)
r := width - tui.Width(legend)
if r < 0 {
r = 0
}
Expand Down Expand Up @@ -388,7 +388,7 @@ func printNames(out tui.Output, headers []imgColumn, img topImage, color, untagg
// name will be printed alongside other columns.
if nameIdx < len(img.Names)-1 {
_, fullWidth := out.GetTtySize()
_, _ = fmt.Fprintln(out, color.Apply(tui.Ellipsis(name, int(fullWidth))))
_, _ = fmt.Fprintln(out, color.Apply(tui.Ellipsis(name, int(fullWidth)))) // #nosec G115 -- ignore "overflow conversion uint -> int", int expansion won't cause lost of value
} else {
_, _ = fmt.Fprint(out, headers[0].Print(color, name))
}
Expand Down
2 changes: 1 addition & 1 deletion cli/command/service/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func runLogs(ctx context.Context, dockerCli command.Cli, opts *logsOptions) erro
if service.Spec.Mode.Replicated != nil && service.Spec.Mode.Replicated.Replicas != nil {
// if replicas are initialized, figure out if we need to pad them
replicas := *service.Spec.Mode.Replicated.Replicas
maxLength = getMaxLength(int(replicas))
maxLength = getMaxLength(int(replicas)) // #nosec G115 -- ignore "integer overflow conversion uint64 -> int" (The only fail case is having 2^32 or more replicas on 32bit system)
}
}

Expand Down
9 changes: 5 additions & 4 deletions cli/command/service/progress/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (u *replicatedProgressUpdater) update(service swarm.Service, tasks []swarm.
u.slotMap = make(map[int]int)

// Draw progress bars in order
writeOverallProgress(u.progressOut, 0, int(replicas), rollback)
writeOverallProgress(u.progressOut, 0, int(replicas), rollback) // #nosec G115 -- ignore "overflow conversion uint64 -> int", safe for less than 2^32 replica in 32bit system

if replicas <= maxProgressBars {
for i := uint64(1); i <= replicas; i++ {
Expand Down Expand Up @@ -340,7 +340,7 @@ func (u *replicatedProgressUpdater) update(service swarm.Service, tasks []swarm.
}

if !u.done {
writeOverallProgress(u.progressOut, int(running), int(replicas), rollback)
writeOverallProgress(u.progressOut, int(running), int(replicas), rollback) // #nosec G115 -- ignore "overflow conversion uint64 -> int", safe for less than 2^32 running tasks in 32bit system

if running == replicas {
u.done = true
Expand Down Expand Up @@ -383,6 +383,7 @@ func (*replicatedProgressUpdater) tasksBySlot(tasks []swarm.Task, activeNodes ma
}

func (u *replicatedProgressUpdater) writeTaskProgress(task swarm.Task, mappedSlot int, replicas uint64) {
// #nosec G115 -- ignore "overflow conversion uint64 -> int", mappedSlot never negative
if u.done || replicas > maxProgressBars || uint64(mappedSlot) > replicas {
return
}
Expand Down Expand Up @@ -572,8 +573,8 @@ type replicatedJobProgressUpdater struct {
}

func newReplicatedJobProgressUpdater(service swarm.Service, progressOut progress.Output) *replicatedJobProgressUpdater {
concurrent := int(*service.Spec.Mode.ReplicatedJob.MaxConcurrent)
total := int(*service.Spec.Mode.ReplicatedJob.TotalCompletions)
concurrent := int(*service.Spec.Mode.ReplicatedJob.MaxConcurrent) // #nosec G115 -- ignore "overflow conversion uint64 -> int", safe for less than 2^32 MaxConcurrent in 32bit system
total := int(*service.Spec.Mode.ReplicatedJob.TotalCompletions) // #nosec G115 -- ignore "overflow conversion uint64 -> int", safe for less than 2^32 TotalCompletions in 32bit system

return &replicatedJobProgressUpdater{
progressOut: progressOut,
Expand Down
3 changes: 2 additions & 1 deletion cli/compose/convert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ func convertHealthcheck(healthcheck *composetypes.HealthCheckConfig) (*container
if healthcheck.StartInterval != nil {
startInterval = time.Duration(*healthcheck.StartInterval)
}
// #nosec G115 -- ignore "overflow conversion uint64 -> int", safe to convert for retries value less than 2^32 in a 32bit system
if healthcheck.Retries != nil {
retries = int(*healthcheck.Retries)
}
Expand Down Expand Up @@ -488,7 +489,7 @@ func convertRestartPolicy(restart string, source *composetypes.RestartPolicy) (*
Condition: swarm.RestartPolicyConditionAny,
}, nil
case policy.IsOnFailure():
attempts := uint64(policy.MaximumRetryCount)
attempts := uint64(policy.MaximumRetryCount) // #nosec G115 -- ignore "overflow onversion int -> uint64", validation for negative value exist on MaximumRetryCount init
return &swarm.RestartPolicy{
Condition: swarm.RestartPolicyConditionOnFailure,
MaxAttempts: &attempts,
Expand Down
4 changes: 2 additions & 2 deletions opts/swarmopts/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ func ConvertPortToPortConfig(
ports = append(ports, swarm.PortConfig{
// TODO Name: ?
Protocol: swarm.PortConfigProtocol(strings.ToLower(port.Proto())),
TargetPort: uint32(port.Int()),
PublishedPort: uint32(i),
TargetPort: uint32(port.Int()), // #nosec G115 -- ignore "integer overflow conversion int -> uint32" (All known port is in range of uint32, including dynamic port)
PublishedPort: uint32(i), // #nosec G115 -- ignore "integer overflow conversion uint64 -> uint32" (All known port is in range of uint32, including dynamic port)
PublishMode: swarm.PortConfigPublishModeIngress,
})
}
Expand Down