Skip to content

Commit

Permalink
Fixed some potential nil pointer exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Aug 11, 2023
1 parent d1869b6 commit 1d3e528
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
11 changes: 8 additions & 3 deletions arduino/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,17 @@ func PortFromRPCPort(o *rpc.Port) (p *Port) {
if o == nil {
return nil
}
return &Port{
res := &Port{
Address: o.Address,
AddressLabel: o.Label,
Protocol: o.Protocol,
ProtocolLabel: o.ProtocolLabel,
HardwareID: o.HardwareId,
Properties: properties.NewFromHashmap(o.Properties),
}
if o.Properties != nil {
res.Properties = properties.NewFromHashmap(o.Properties)
}
return res
}

func (p *Port) String() string {
Expand All @@ -141,7 +144,9 @@ func (p *Port) Clone() *Port {
return nil
}
var res Port = *p
res.Properties = p.Properties.Clone()
if p.Properties != nil {
res.Properties = p.Properties.Clone()
}
return &res
}

Expand Down
10 changes: 6 additions & 4 deletions arduino/discovery/discovery_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ func main() {
fmt.Printf(" Address: %s\n", port.Address)
fmt.Printf(" Protocol: %s\n", port.Protocol)
if ev.Type == "add" {
keys := port.Properties.Keys()
sort.Strings(keys)
for _, k := range keys {
fmt.Printf(" %s=%s\n", k, port.Properties.Get(k))
if port.Properties != nil {
keys := port.Properties.Keys()
sort.Strings(keys)
for _, k := range keys {
fmt.Printf(" %s=%s\n", k, port.Properties.Get(k))
}
}
}
fmt.Println()
Expand Down
13 changes: 8 additions & 5 deletions commands/board/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/internal/inventory"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/arduino/go-properties-orderedmap"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -128,20 +129,22 @@ func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
}, nil
}

func identifyViaCloudAPI(port *discovery.Port) ([]*rpc.BoardListItem, error) {
func identifyViaCloudAPI(props *properties.Map) ([]*rpc.BoardListItem, error) {
// If the port is not USB do not try identification via cloud
id := port.Properties
if !id.ContainsKey("vid") || !id.ContainsKey("pid") {
if !props.ContainsKey("vid") || !props.ContainsKey("pid") {
return nil, nil
}

logrus.Debug("Querying builder API for board identification...")
return cachedAPIByVidPid(id.Get("vid"), id.Get("pid"))
return cachedAPIByVidPid(props.Get("vid"), props.Get("pid"))
}

// identify returns a list of boards checking first the installed platforms or the Cloud API
func identify(pme *packagemanager.Explorer, port *discovery.Port) ([]*rpc.BoardListItem, error) {
boards := []*rpc.BoardListItem{}
if port.Properties == nil {
return boards, nil
}

// first query installed cores through the Package Manager
logrus.Debug("Querying installed cores for board identification...")
Expand All @@ -167,7 +170,7 @@ func identify(pme *packagemanager.Explorer, port *discovery.Port) ([]*rpc.BoardL
// if installed cores didn't recognize the board, try querying
// the builder API if the board is a USB device port
if len(boards) == 0 {
items, err := identifyViaCloudAPI(port)
items, err := identifyViaCloudAPI(port.Properties)
if err != nil {
// this is bad, but keep going
logrus.WithError(err).Debug("Error querying builder API")
Expand Down
6 changes: 4 additions & 2 deletions commands/upload/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,10 @@ func runProgramAction(pme *packagemanager.Explorer,
uploadProperties.Set("upload.port.label", port.AddressLabel)
uploadProperties.Set("upload.port.protocol", port.Protocol)
uploadProperties.Set("upload.port.protocolLabel", port.ProtocolLabel)
for prop, value := range actualPort.Properties.AsMap() {
uploadProperties.Set(fmt.Sprintf("upload.port.properties.%s", prop), value)
if actualPort.Properties != nil {
for prop, value := range actualPort.Properties.AsMap() {
uploadProperties.Set(fmt.Sprintf("upload.port.properties.%s", prop), value)
}
}

// Run recipes for upload
Expand Down

0 comments on commit 1d3e528

Please sign in to comment.