Skip to content

Commit

Permalink
Fix possible out of bounds error.
Browse files Browse the repository at this point in the history
  • Loading branch information
gsexton committed Nov 23, 2024
1 parent 63d802c commit 9d985ec
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions gpioioctl/gpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ func (d *driverGPIO) After() []string {
//
// https://docs.kernel.org/userspace-api/gpio/chardev.html
func (d *driverGPIO) Init() (bool, error) {
Chips = make([]*GPIOChip, 0)
if runtime.GOOS != "linux" {
return true, nil
}
Expand All @@ -607,7 +606,7 @@ func (d *driverGPIO) Init() (bool, error) {
return false, errors.New("no GPIO chips found")
}

Check warning on line 607 in gpioioctl/gpio.go

View check run for this annotation

Codecov / codecov/patch

gpioioctl/gpio.go#L598-L607

Added lines #L598 - L607 were not covered by tests
// First, get all of the chips on the system.
chips := make([]*GPIOChip, 0)
var chips []*GPIOChip
var chip *GPIOChip
for _, item := range items {
chip, err = newGPIOChip(item)
Expand All @@ -623,24 +622,22 @@ func (d *driverGPIO) Init() (bool, error) {
sort.Slice(chips, func(i, j int) bool {
I := chips[i]
J := chips[j]
if I.Label()[:8] == "pinctrl-" {
if J.Label()[:8] == "pinctrl-" {
if strings.HasPrefix(I.Label(), "pinctrl-") {
if strings.HasPrefix(J.Label(), "pinctrl-") {
return I.Label() < J.Label()

Check warning on line 627 in gpioioctl/gpio.go

View check run for this annotation

Codecov / codecov/patch

gpioioctl/gpio.go#L622-L627

Added lines #L622 - L627 were not covered by tests
} else {
return true
}
} else if J.Label()[:8] == "pinctrl-" {
return true
} else if strings.HasPrefix(J.Label(), "pinctrl-") {
return false
} else {
return I.Label() < J.Label()
}
return I.Label() < J.Label()

Check warning on line 633 in gpioioctl/gpio.go

View check run for this annotation

Codecov / codecov/patch

gpioioctl/gpio.go#L629-L633

Added lines #L629 - L633 were not covered by tests
})

mName := make(map[string]bool, 0)
mName := make(map[string]struct{})
// Get a list of already registered GPIO Line names.
registeredPins := make(map[string]bool)
registeredPins := make(map[string]struct{})
for _, pin := range gpioreg.All() {
registeredPins[pin.Name()] = true
registeredPins[pin.Name()] = struct{}{}
}

Check warning on line 641 in gpioioctl/gpio.go

View check run for this annotation

Codecov / codecov/patch

gpioioctl/gpio.go#L636-L641

Added lines #L636 - L641 were not covered by tests

// Now, iterate over the chips we found and add their lines to conn/gpio/gpioreg
Expand All @@ -649,7 +646,7 @@ func (d *driverGPIO) Init() (bool, error) {
// ensures we don't duplicate the chip.
if _, found := mName[chip.Name()]; !found {

Check warning on line 647 in gpioioctl/gpio.go

View check run for this annotation

Codecov / codecov/patch

gpioioctl/gpio.go#L644-L647

Added lines #L644 - L647 were not covered by tests
Chips = append(Chips, chip)
mName[chip.Name()] = true
mName[chip.Name()] = struct{}{}
// Now, iterate over the lines on this chip.

Check warning on line 650 in gpioioctl/gpio.go

View check run for this annotation

Codecov / codecov/patch

gpioioctl/gpio.go#L649-L650

Added lines #L649 - L650 were not covered by tests
for _, line := range chip.lines {
// If the line has some sort of reasonable name...

Check warning on line 652 in gpioioctl/gpio.go

View check run for this annotation

Codecov / codecov/patch

gpioioctl/gpio.go#L652

Added line #L652 was not covered by tests
Expand All @@ -665,7 +662,7 @@ func (d *driverGPIO) Init() (bool, error) {
continue

Check warning on line 662 in gpioioctl/gpio.go

View check run for this annotation

Codecov / codecov/patch

gpioioctl/gpio.go#L654-L662

Added lines #L654 - L662 were not covered by tests
}
}
registeredPins[line.Name()] = true
registeredPins[line.Name()] = struct{}{}

Check warning on line 665 in gpioioctl/gpio.go

View check run for this annotation

Codecov / codecov/patch

gpioioctl/gpio.go#L665

Added line #L665 was not covered by tests
if err = gpioreg.Register(line); err != nil {
log.Println("chip", chip.Name(), " gpioreg.Register(line) ", line, " returned ", err)
}
Expand Down

0 comments on commit 9d985ec

Please sign in to comment.