Skip to content

Conversation

@reject-i
Copy link
Contributor

@reject-i reject-i commented Jun 18, 2025

Description:
PR addresses two issues in the modulo function in the utils library:

  1. Adds proper handling for the case when n = 0, returning 0 instead of potentially causing division by zero
  2. Prevents potential overflow when working with large numbers by applying modulo after each shift operation
  3. Adds comprehensive test cases to verify the fixes, including:
    • Test for n = 0
    • Test with large numbers close to u64::MAX to verify overflow handling

These changes improve the robustness of the modulo function when used with arbitrary inputs

@BrendanChou
Copy link
Collaborator

"failing silently" (returning 0) when using 0 as divisor would be non-standard. panic as before is fine

@BrendanChou
Copy link
Collaborator

Thanks, you bring up two issues:

  • Behavior when n == 0 is not specified or tested
  • Modulo values with greater than 7 bytes may silently overflow when doing shl (<<)

Issues with the PR:

  • We should not return 0 for n == 0. 0 should instead be returned for n == 1. For n == 0 we should panic instead
  • Your tests do not actually show the incorrect behavior for large values of n, nor do the code changes fix the issue

Closing in favor of #1123 which will fix the above issues

@patrick-ogrady
Copy link
Contributor

(Thanks for calling attention to this @reject-i 🙏 )

@reject-i
Copy link
Contributor Author

Thanks for your work!

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.

3 participants