-
Notifications
You must be signed in to change notification settings - Fork 503
Fix segfault in removeVertexNode
due to negative hash
#94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix segfault in removeVertexNode
due to negative hash
#94
Conversation
…gned The hash in `_hashVertex` produces out of range values if `(vertex->lat + vertex->lon)` was negative, because it would produce a negative module (such as -1) and then cast it to an unsigned integer type. This index would then cause a segfault. testH3SetToLinkedGeo.c is also updated to not `calloc` a fixed size of 1.
@@ -29,130 +29,123 @@ H3Index* makeSet(char** hexes, int numHexes) { | |||
BEGIN_TESTS(h3SetToLinkedGeo); | |||
|
|||
TEST(empty) { | |||
LinkedGeoPolygon* polygon = calloc(1, sizeof(LinkedGeoPolygon)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
heh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the fix!
@@ -29,130 +29,123 @@ H3Index* makeSet(char** hexes, int numHexes) { | |||
BEGIN_TESTS(h3SetToLinkedGeo); | |||
|
|||
TEST(empty) { | |||
LinkedGeoPolygon* polygon = calloc(1, sizeof(LinkedGeoPolygon)); | |||
LinkedGeoPolygon polygon; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
@@ -70,7 +70,7 @@ void destroyVertexGraph(VertexGraph* graph) { | |||
uint32_t _hashVertex(const GeoCoord* vertex, int res, int numBuckets) { | |||
// Simple hash: Take the sum of the lat and lon with a precision level | |||
// determined by the resolution, converted to int, modulo bucket count. | |||
return (uint32_t)fmod((vertex->lat + vertex->lon) * pow(10, 15 - res), | |||
return (uint32_t)fmod(fabs((vertex->lat + vertex->lon) * pow(10, 15 - res)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These math functions can kill your performance. You should benchmark these changes. Also might be worth comparing to sip hash using (const char*) &vertex->lat
as input. I wonder which is faster.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even better would be to assign an index to the individual vertices, removing the need for floating point equality/hashing entirely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed - I'd suggest we don't focus on the performance in this diff (can't imagine that a single fabs
call will make that big a difference), and come back to it when the vertex index mode change is implemented. I'll open an issue for that now.
Fix segfault in `removeVertexNode` due to negative hash
Fixes #92
The hash in
_hashVertex
produces out of range values if(vertex->lat + vertex->lon)
was negative, because it would produce a negative remainder (such as -1) and then cast it to
an unsigned integer type. This index would then cause a segfault.
testH3SetToLinkedGeo.c is also updated to not
calloc
a fixed size of 1.