Skip to content

Commit 981da68

Browse files
authored
Avoids a nil pointer dereference error in Geo() (#227)
* Avoids a nil pointer dereference error in Geo() There was a case in staging where Locate was returning this for a host: "neubot-mlab4-syd04.mlab-staging.measurement-lab.org": { "Health": null, "Registration": null, "Prometheus": { "Health": false } }, ... this was causing a panic in Locate. This commit avoids this, and adds a new unit test to be sure it working as intended. * Fixes a few typos in a comment in Geo()
1 parent 63bc2bf commit 981da68

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

siteinfo/siteinfo.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,31 @@ func Geo(msgs map[string]v2.HeartbeatMessage, v url.Values) (*geojson.FeatureCol
3535
return nil, err
3636
}
3737

38-
for k, v := range hosts {
38+
for k, h := range hosts {
3939
parts, err := host.Parse(k)
4040
if err != nil {
4141
returnError := fmt.Errorf("failed to parse hostname: %s", k)
4242
return fc, returnError
4343
}
4444

45-
f = geojson.NewFeature(orb.Point{v.Registration.Longitude, v.Registration.Latitude})
45+
// We have witnessed a case in staging where a v2.HeartbeatMessage's
46+
// Registration field was nil for some reason. It's not clear under
47+
// what circumstances this error condition can happen, but skip this
48+
// host if Registration is nil to avoid panics when trying to
49+
// dereference a nil pointer.
50+
if h.Registration == nil {
51+
continue
52+
}
53+
54+
f = geojson.NewFeature(orb.Point{h.Registration.Longitude, h.Registration.Latitude})
4655
f.Properties = map[string]interface{}{
47-
"health": v.Health.Score,
48-
"hostname": v.Registration.Hostname,
56+
"health": h.Health.Score,
57+
"hostname": h.Registration.Hostname,
4958
"machine": fmt.Sprintf("%s-%s", parts.Site, parts.Machine),
5059
"org": parts.Org,
51-
"probability": v.Registration.Probability,
52-
"uplink": v.Registration.Uplink,
53-
"type": v.Registration.Type,
60+
"probability": h.Registration.Probability,
61+
"uplink": h.Registration.Uplink,
62+
"type": h.Registration.Type,
5463
}
5564

5665
fc.Append(f)

siteinfo/siteinfo_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,19 @@ func TestGeo(t *testing.T) {
299299
"ndt-oma7777-217f832a.mlab.sandbox.measurement-lab.org",
300300
},
301301
},
302+
{
303+
name: "success-missing-data",
304+
instances: map[string]v2.HeartbeatMessage{
305+
"ndt-mlab3-mia0t.mlab-sandbox.measurement-lab.org": {
306+
Health: nil,
307+
Registration: nil,
308+
Prometheus: &v2.Prometheus{
309+
Health: false,
310+
},
311+
},
312+
},
313+
wantErr: false,
314+
},
302315
{
303316
name: "error",
304317
instances: map[string]v2.HeartbeatMessage{

0 commit comments

Comments
 (0)