Skip to content

div by zero#536

Merged
0xaslan merged 1 commit into
mainfrom
at/protocol-fees-div-zero
Sep 22, 2025
Merged

div by zero#536
0xaslan merged 1 commit into
mainfrom
at/protocol-fees-div-zero

Conversation

@0xaslan

@0xaslan 0xaslan commented Sep 22, 2025

Copy link
Copy Markdown
Collaborator

No description provided.

@claude

claude Bot commented Sep 22, 2025

Copy link
Copy Markdown

Claude finished @0xaslan's task —— View job


Code Review Complete ✅

Reviewing the pull request for division by zero fix:

Todo List:

  • Read and analyze the changed file
  • Review code quality and best practices
  • Check for potential bugs or issues
  • Assess performance considerations
  • Evaluate security concerns
  • Examine test coverage needs
  • Provide comprehensive feedback

Analysis Summary

This PR adds a critical division by zero protection in the calculate_and_claim function in packages/margin_trading/sources/margin_pool/protocol_fees.move:131.

The Fix:

if (elapsed == 0) return 0;

This prevents a division by zero that would occur on line 133:

let shares = math::div(share_ms_delta, elapsed);

Code Review Feedback

Code Quality & Best Practices

  • Good: The fix follows Move's early return pattern and is minimal
  • Good: Consistent with existing code style in the file
  • Good: The error handling approach (returning 0) is appropriate for this context

Bug Prevention

  • Critical Fix: Properly addresses the division by zero vulnerability
  • Good: The elapsed == 0 check happens immediately after calculation, preventing the division
  • Good: Returning 0 fees when elapsed time is 0 is logically correct

Performance Considerations

  • Excellent: Minimal performance impact - just one integer comparison
  • Good: Early return prevents unnecessary subsequent calculations

⚠️ Security Concerns

  • Minor: While the fix prevents the crash, consider if there are edge cases where rapid successive calls could be exploited
  • Note: The function is public(package) which limits exposure appropriately

Test Coverage

  • Missing: No test coverage found for the protocol_fees module
  • Recommendation: Add tests specifically covering:
    • calculate_and_claim with elapsed == 0
    • Rapid successive calls to the same referral
    • Edge cases around timestamp handling

📝 Additional Recommendations

  1. Consider Adding Documentation: A brief comment explaining why we return 0 when elapsed == 0 would be helpful:

    // Return 0 fees if no time has elapsed since last claim
    if (elapsed == 0) return 0;
  2. Verify Math Library: The math::div function in deepbook::math doesn't appear to have explicit division by zero protection, so this fix is definitely needed.

  3. Function Context: This function is called from margin_pool.move:290 during referral fee claiming, so the early return behavior is appropriate.

Overall Assessment

This is a solid, necessary fix that addresses a real division by zero vulnerability. The implementation is clean and follows good practices. The main concern is the lack of test coverage for this critical functionality.

Recommendation: APPROVE with suggestion to add test coverage in a follow-up PR.


@0xaslan 0xaslan merged commit 57be511 into main Sep 22, 2025
4 checks passed
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