Skip to content
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

Prevent signed integer overflow in maxGridDiskSize #686

Conversation

isaacbrodsky
Copy link
Collaborator

Signed integer overflow is undefined behavior in C, so we cannot add, multiply, etc. arbitrary integer inputs. This adds a hard cap on the maxGridDiskSize function based on knowledge of the grid system itself.

@isaacbrodsky isaacbrodsky force-pushed the prevent-signed-integer-overflow-maxgriddisksize branch from 231a8bd to e3bb418 Compare September 12, 2022 18:12
@coveralls
Copy link

coveralls commented Sep 12, 2022

Coverage Status

Coverage increased (+0.003%) to 99.038% when pulling a0c4bbe on isaacbrodsky:prevent-signed-integer-overflow-maxgriddisksize into f581626 on uber:master.

@@ -162,6 +162,17 @@ H3Error H3_EXPORT(maxGridDiskSize)(int k, int64_t *out) {
if (k < 0) {
return E_DOMAIN;
}
if (k >= 13780510) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Should we assign this to a const?

Copy link
Collaborator

Choose a reason for hiding this comment

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

And/or a collection of constants for each resolution? I assume a much lower k at res 0 will run into similar problems?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As this function does not accept the resolution we have no way of knowing if the k value will be larger than the grid. This would require an API change.

k that generates >122 cells at resolution 0 is also not the immediate issue. The issue is k values like INT32_MAX which would result in signed integer overflow, which is undefined behavior.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We can do that without breaking backwards compatibility by creating a new function that does take the resolution, and recommending it over the old one? That would help prevent memory over-allocation for the smaller resolutions, and we could mark the current one as deprecated (but still with this fix).

I do agree with @nrabinowitz in any case that this would be better as a self-descriptive constant variable name.

Copy link
Collaborator Author

@isaacbrodsky isaacbrodsky Sep 13, 2022

Choose a reason for hiding this comment

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

Added a comment for the constant. I'd defer a new function which limits the size to a future PR. (Such a function would also need to have a few other considerations: gridDisk would need to use the new function, the new functions' return value can never be higher than maxGridDiskSize's for a given k)

Comment on lines +86 to +88
for (int idx = 1; idx < arrSize; idx++) {
t_assert(compressed[idx] == 0, "expected only 1 cell");
}
Copy link
Contributor

@jogly jogly Sep 14, 2022

Choose a reason for hiding this comment

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

just curious why checking the second element isn't sufficient? i'd expect the first 0 to "terminate" the array, no matter how big it is (like a C string)

Copy link
Collaborator Author

@isaacbrodsky isaacbrodsky Sep 14, 2022

Choose a reason for hiding this comment

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

I'm not sure that is a valid assumption for compactCells. We should document that clearly if it is. For comparison, I don't believe that assumption holds for gridDisk specifically due to the use of a hash function.

Copy link
Contributor

Choose a reason for hiding this comment

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

good point! this has ramifications for bindings (at least for h3-go) where I "post-process" the internally allocated C buffer to fit the language's paradigms, e.g. I resize the returned slice of indexes to the first zero'd index for compaction...

Comment on lines +360 to +361
if (loopCount > // LCOV_EXCL_BR_LINE
numRemainingHexes) {
Copy link
Contributor

Choose a reason for hiding this comment

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

thanks!

@isaacbrodsky isaacbrodsky merged commit 619a2a7 into uber:master Sep 14, 2022
@isaacbrodsky isaacbrodsky deleted the prevent-signed-integer-overflow-maxgriddisksize branch September 14, 2022 03:50
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.

5 participants