Skip to content

Address panic-ing quadtree.Matching(…) method when finding no closest node#73

Merged
paulmach merged 4 commits intopaulmach:masterfrom
willsalz:willsalz/nearest-panic
Oct 12, 2021
Merged

Address panic-ing quadtree.Matching(…) method when finding no closest node#73
paulmach merged 4 commits intopaulmach:masterfrom
willsalz:willsalz/nearest-panic

Conversation

@willsalz
Copy link
Copy Markdown
Contributor

@willsalz willsalz commented Sep 29, 2021

a fix here is not straight forward because the method signature does not
return error. It's possible that we could add a type orb.NilPointer
[ ugh naming ] that satisfies the orb.Poitner interface but could be
returned without breaking existing clients

…ching

this test case panics because in quadtree/quadtree.go does not check
if the `findVisitor.closest` value is nil before returning
`v.closest.Value` at the end of the method.

a fix here is not straight forward because the method signature does not
return `error`. It's possible that we could add a `type orb.NilPointer`
[ ugh naming ] that satisfies the orb.Poitner interface but could be
returned without breaking existing clients
just return a private, nilPointer struct that implements orb.Pointer by
returning an orb.Point{} directly. This change is technically breaking
in that it no longer reliably panics if users were relying on that
behavior. It is the simplest change I could think of that didn't change
the API.

Alternatively, we can introduce new FindV2 and MatchingV2 methods that
return (orb.Pointer, error) that do not panic that can survive until a
minor orb version bump with breaking API.
@willsalz willsalz marked this pull request as ready for review September 29, 2021 00:31
@willsalz willsalz changed the title add a panicing test case to quadtree/quadtree_test.go/TestQuadTreeMatching Address panic-ing quadtree.Matching(…) method when finding no closest node Sep 29, 2021
return q.Matching(p, nil)
}

type nilPointer struct{}
Copy link
Copy Markdown
Contributor Author

@willsalz willsalz Sep 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what an unfortunate name; consider emptyPointer?

)

if v.closest == nil {
return nilPointer{}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why can't this just return nil? The caller would have the do the if results != nil check, but at this point the code is panicing.

expected: orb.Point{1, 1},
},
{
name: "match none filter",
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will need to be updated. Maybe change the expected to a pointer expected *orb.Point and handle the case below.

@paulmach
Copy link
Copy Markdown
Owner

Thank you for taking the time to make this change.

@paulmach paulmach merged commit d1cf592 into paulmach:master Oct 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants