Skip to content

Commit af873e0

Browse files
committed
Fixed some potential nil pointer exceptions
1 parent d1869b6 commit af873e0

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

Diff for: arduino/discovery/discovery.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,17 @@ func PortFromRPCPort(o *rpc.Port) (p *Port) {
118118
if o == nil {
119119
return nil
120120
}
121-
return &Port{
121+
res := &Port{
122122
Address: o.Address,
123123
AddressLabel: o.Label,
124124
Protocol: o.Protocol,
125125
ProtocolLabel: o.ProtocolLabel,
126126
HardwareID: o.HardwareId,
127-
Properties: properties.NewFromHashmap(o.Properties),
128127
}
128+
if o.Properties != nil {
129+
res.Properties = properties.NewFromHashmap(o.Properties)
130+
}
131+
return res
129132
}
130133

131134
func (p *Port) String() string {
@@ -141,7 +144,9 @@ func (p *Port) Clone() *Port {
141144
return nil
142145
}
143146
var res Port = *p
144-
res.Properties = p.Properties.Clone()
147+
if p.Properties != nil {
148+
res.Properties = p.Properties.Clone()
149+
}
145150
return &res
146151
}
147152

Diff for: arduino/discovery/discovery_client/main.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ func main() {
5151
fmt.Printf(" Address: %s\n", port.Address)
5252
fmt.Printf(" Protocol: %s\n", port.Protocol)
5353
if ev.Type == "add" {
54-
keys := port.Properties.Keys()
55-
sort.Strings(keys)
56-
for _, k := range keys {
57-
fmt.Printf(" %s=%s\n", k, port.Properties.Get(k))
54+
if port.Properties != nil {
55+
keys := port.Properties.Keys()
56+
sort.Strings(keys)
57+
for _, k := range keys {
58+
fmt.Printf(" %s=%s\n", k, port.Properties.Get(k))
59+
}
5860
}
5961
}
6062
fmt.Println()

Diff for: commands/board/list.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/arduino/arduino-cli/commands"
3535
"github.com/arduino/arduino-cli/internal/inventory"
3636
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
37+
"github.com/arduino/go-properties-orderedmap"
3738
"github.com/pkg/errors"
3839
"github.com/sirupsen/logrus"
3940
)
@@ -128,20 +129,22 @@ func apiByVidPid(vid, pid string) ([]*rpc.BoardListItem, error) {
128129
}, nil
129130
}
130131

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

138138
logrus.Debug("Querying builder API for board identification...")
139-
return cachedAPIByVidPid(id.Get("vid"), id.Get("pid"))
139+
return cachedAPIByVidPid(props.Get("vid"), props.Get("pid"))
140140
}
141141

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

146149
// first query installed cores through the Package Manager
147150
logrus.Debug("Querying installed cores for board identification...")
@@ -167,7 +170,7 @@ func identify(pme *packagemanager.Explorer, port *discovery.Port) ([]*rpc.BoardL
167170
// if installed cores didn't recognize the board, try querying
168171
// the builder API if the board is a USB device port
169172
if len(boards) == 0 {
170-
items, err := identifyViaCloudAPI(port)
173+
items, err := identifyViaCloudAPI(port.Properties)
171174
if err != nil {
172175
// this is bad, but keep going
173176
logrus.WithError(err).Debug("Error querying builder API")

Diff for: commands/board/list_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ func TestGetByVidPidMalformedResponse(t *testing.T) {
103103
}
104104

105105
func TestBoardDetectionViaAPIWithNonUSBPort(t *testing.T) {
106-
port := &discovery.Port{
107-
Properties: properties.NewMap(),
108-
}
109-
items, err := identifyViaCloudAPI(port)
106+
items, err := identifyViaCloudAPI(properties.NewMap())
110107
require.NoError(t, err)
111108
require.Empty(t, items)
112109
}

Diff for: commands/upload/upload.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,10 @@ func runProgramAction(pme *packagemanager.Explorer,
496496
uploadProperties.Set("upload.port.label", port.AddressLabel)
497497
uploadProperties.Set("upload.port.protocol", port.Protocol)
498498
uploadProperties.Set("upload.port.protocolLabel", port.ProtocolLabel)
499-
for prop, value := range actualPort.Properties.AsMap() {
500-
uploadProperties.Set(fmt.Sprintf("upload.port.properties.%s", prop), value)
499+
if actualPort.Properties != nil {
500+
for prop, value := range actualPort.Properties.AsMap() {
501+
uploadProperties.Set(fmt.Sprintf("upload.port.properties.%s", prop), value)
502+
}
501503
}
502504

503505
// Run recipes for upload

0 commit comments

Comments
 (0)