Skip to content

G115: range checks with return #1501

@pmkolo

Description

@pmkolo

Trying to understand the latest G115 update

// number of repetitions from data returned
calcReps := len(payload) / len(ids)

// version 1
repetitions = 255
if calcReps > 0 && calcReps < math.MaxUint8 {
    repetitions = uint8(calcReps) // G115 triggered
}

// version 2
if calcReps < 0 || calcReps >= math.MaxUint8 {
    // repetitions exceed limits - setting max
    repetitions = 255
} else {
    repetitions = uint8(calcReps) // G115 triggered
}

// version 3
if calcReps < 0 || calcReps >= math.MaxUint8 {
    return // well ... not really what I need
}
repetitions = uint8(calcReps) // no G115 

Alternatives 1 and 2 were passing before 2.23.0

So now in order to fulfill the return requirement do I have to setup all possible conversion helpers with/without defaults?

func IntToUint8WithDefault(n int, def uint8) uint8 {
    if n < 0 || n >= math.MaxUint8  {
        return def
    }
    return uint8(n)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions