fees: prevent divide by zero in CFeeRate #36712

pull ismaelsadeeq wants to merge 1 commits into bitcoin:master from ismaelsadeeq:10-2026-fix-cfeerate-divide-by-zero changing 3 files +14 −1
  1. ismaelsadeeq commented at 4:33 PM on October 30, 2026: member

    A CFeeRate with size 0 GetFeePerK() and CFeeRate+= evaluated EvaluateFeeDown(1000) unconditionally, dividing by that zero size and crashing.

    Return 0 for an empty fee rate, matching GetFee() behaviour, and add minimal unit test coverage.

    This was reported by Jacob F Cudney (aragondragon)

  2. DrahtBot added the label TX fees and policy on Oct 30, 2026
  3. DrahtBot commented at 4:33 PM on October 30, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    ACK polespinasa, musaHaruna, pablomartin4btc
    Concept ACK darosior

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. ismaelsadeeq commented at 4:33 PM on October 30, 2026: member
  5. in src/test/amount_tests.cpp:96 in 40647a2057
      91 | @@ -92,6 +92,11 @@ BOOST_AUTO_TEST_CASE(GetFeeTest)
      92 |      BOOST_CHECK(CFeeRate(CAmount(27), 789) < CFeeRate(35));
      93 |      // Maximum size in bytes, should not crash
      94 |      CFeeRate(MAX_MONEY, std::numeric_limits<int32_t>::max()).GetFeePerK();
      95 | +    BOOST_CHECK_EQUAL(CFeeRate(0, 0).GetFeePerK(), CAmount{0});
      96 | +    BOOST_CHECK_EQUAL(CFeeRate(CAmount{5}, 0).GetFeePerK(), CAmount{0});
    


    maflcko commented at 4:43 PM on October 30, 2026:

    nit: Could move the tests up to where GetFee() returns zero, so that the same tests are grouped? For the next one, could have a newline, as this test is unrelated from the one about "Maximum size in bytes"?


    ismaelsadeeq commented at 9:34 AM on November 2, 2026:

    Fixed.

  6. maflcko approved
  7. maflcko commented at 4:43 PM on October 30, 2026: member

    lgtm

  8. pablomartin4btc commented at 8:14 PM on October 30, 2026: member

    ACK 40647a20572a06b0643e28d92b471a2ab3954de6

  9. darosior commented at 8:48 PM on October 30, 2026: member

    Concept ACK

  10. polespinasa commented at 9:48 PM on October 30, 2026: member

    ACK 40647a20572a06b0643e28d92b471a2ab3954de6

  11. DrahtBot requested review from darosior on Oct 30, 2026
  12. fees: prevent divide by zero in CFeeRate
    A CFeeRate with size 0. GetFeePerK() evaluated EvaluateFeeDown(1000)
    unconditionally, dividing by that zero size and crashing.
    
    Return 0 for an empty fee rate, matching GetFee(), and add coverage.
    f442fa20d4
  13. ismaelsadeeq force-pushed on Nov 2, 2026
  14. polespinasa commented at 10:19 AM on November 2, 2026: member

    re-ACK f442fa20d437f02518c327736206185bd6f6a1a1

  15. DrahtBot requested review from pablomartin4btc on Nov 2, 2026
  16. in src/test/amount_tests.cpp:102 in f442fa20d4
      94 | @@ -93,6 +95,11 @@ BOOST_AUTO_TEST_CASE(GetFeeTest)
      95 |      // Maximum size in bytes, should not crash
      96 |      CFeeRate(MAX_MONEY, std::numeric_limits<int32_t>::max()).GetFeePerK();
      97 |  
      98 | +    // Adding to a zero size fee rate should not crash
      99 | +    CFeeRate empty_rate(0, 0);
     100 | +    empty_rate += CFeeRate(1, 1);
     101 | +    BOOST_CHECK(empty_rate == CFeeRate(1, 1));
     102 | +
    


    musaHaruna commented at 10:36 AM on November 2, 2026:

    Nit: could we also check the amount returned by GetFeePerK() after the addition against the expected amount of 1000?

    BOOST_CHECK_EQUAL(empty_rate.GetFeePerK(), CAmount{1000});
    

    I tried adding this locally and temporarily commenting out empty_rate += CFeeRate(1, 1), leaving the object empty. The existing equality assertion still passed, while this additional check failed with [0 != 1000]. This happens because the comparison uses cross multiplication: {0, 0} and {1, 1} produce 0 × 1 == 1 × 0.

    With the addition present, both assertions pass. I thought the extra check could help verify the expected result more explicitly and catch a case where the object incorrectly remains empty.


    ismaelsadeeq commented at 12:28 PM on November 2, 2026:

    That's a new coverage worth adding, I think. But the goal here is to validate that += does not crash. I believe the current test suffices.

  17. musaHaruna commented at 10:37 AM on November 2, 2026: contributor

    Code review ACK f442fa2

    The fix looks straightforward to me. Checking for an empty rate before the calculation prevents division by zero and matches how GetFee() handles it. Apologies for the comment order, I mistakenly submitted the inline comment first.

  18. pablomartin4btc commented at 2:01 PM on November 2, 2026: member

    re-ACK f442fa20d437f02518c327736206185bd6f6a1a1

  19. fanquake merged this on Nov 2, 2026
  20. fanquake closed this on Nov 2, 2026

  21. in src/policy/feerate.h:71 in f442fa20d4
      67 | @@ -68,7 +68,7 @@ class CFeeRate
      68 |      /**
      69 |       * Return the fee in satoshis for a vsize of 1000 vbytes
      70 |       */
      71 | -    CAmount GetFeePerK() const { return CAmount(m_feerate.EvaluateFeeDown(1000)); }
      72 | +    CAmount GetFeePerK() const;
    


    maflcko commented at 1:28 PM on November 5, 2026:

    now that the impl is moved to the cpp file, the docs could say the rounding mode for both functions. Done in https://github.com/bitcoin/bitcoin/pull/36741


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-21 11:51 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me