sugarchain-project / yumekawa

[TEST] Yumekawa 2.0 Taproot
MIT License
6 stars 5 forks source link

signed integer overflow: 107374182400000000 * 1000 cannot be represented in type 'long' #45

Open decryp2kanon opened 3 years ago

decryp2kanon commented 3 years ago

https://github.com/sugarchain-project/yumekawa/blob/a14eadf6a06463fbb7ede23068c208cf5e217cff/src/test/fuzz/util.h#L127-L130

https://cirrus-ci.com/task/4665805700857856?command=ci#L4087

2020-10-19T00:42:54.494449Z [test] Validating signatures for all blocks.
2020-10-19T00:42:54.494509Z [test] Setting nMinimumChainWork=0000000000000000000000000000000000000000000000000000000000000000
2020-10-19T00:42:54.497020Z [test] Using 16 MiB out of 32/2 requested for signature cache, able to store 524288 elements
2020-10-19T00:42:54.498689Z [test] Using 16 MiB out of 32/2 requested for script execution cache, able to store 524288 elements
�[1;34;49mtest/amount_tests.cpp(89): Leaving test case "BinaryOperatorTest"; testing time: 41103us
�[0;39;49m�[1;34;49mtest/amount_tests.cpp(22): Entering test case "GetFeeTest"
policy/feerate.cpp:16:34: runtime error: signed integer overflow: 107374182400000000 * 1000 cannot be represented in type 'long'
    #0 0x55690377380a in CFeeRate::CFeeRate(long const&, unsigned long) /tmp/cirrus-ci-build/ci/scratch/build/sugarchain-x86_64-pc-linux-gnu/src/policy/feerate.cpp:16:34
    #1 0x55690213856b in amount_tests::GetFeeTest::test_method() /tmp/cirrus-ci-build/ci/scratch/build/sugarchain-x86_64-pc-linux-gnu/src/test/amount_tests.cpp:86:5
    #2 0x5569021318b3 in amount_tests::GetFeeTest_invoker() /tmp/cirrus-ci-build/ci/scratch/build/sugarchain-x86_64-pc-linux-gnu/src/test/amount_tests.cpp:22:1
    #3 0x55690207c46c in boost::detail::function::void_function_invoker0<void (*)(), void>::invoke(boost::detail::function::function_buffer&) /usr/include/boost/function/function_template.hpp:117:11
    #4 0x7f3ce71993f1  (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x353f1)
    #5 0x7f3ce7196c74 in boost::execution_monitor::catch_signals(boost::function<int ()> const&) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x32c74)
    #6 0x7f3ce7196cf7 in boost::execution_monitor::execute(boost::function<int ()> const&) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x32cf7)
    #7 0x7f3ce7196dcd in boost::execution_monitor::vexecute(boost::function<void ()> const&) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x32dcd)
    #8 0x7f3ce71c4134 in boost::unit_test::unit_test_monitor_t::execute_and_translate(boost::function<void ()> const&, unsigned long) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x60134)
    #9 0x7f3ce71a75a8  (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x435a8)
    #10 0x7f3ce71a7b03  (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x43b03)
    #11 0x7f3ce71a7b03  (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x43b03)
    #12 0x7f3ce719e939 in boost::unit_test::framework::run(unsigned long, bool) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x3a939)
    #13 0x7f3ce71c2fea in boost::unit_test::unit_test_main(bool (*)(), int, char**) (/lib/x86_64-linux-gnu/libboost_unit_test_framework.so.1.71.0+0x5efea)
    #14 0x556901fc40ba in main /usr/include/boost/test/unit_test.hpp:63:12
    #15 0x7f3ce68270b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
    #16 0x556901f198bd in _start (/tmp/cirrus-ci-build/ci/scratch/build/sugarchain-x86_64-pc-linux-gnu/src/test/test_sugarchain+0x23a68bd)

SUMMARY: UndefinedBehaviorSanitizer: signed-integer-overflow policy/feerate.cpp:16:34 in 
make[3]: *** [Makefile:21219: test/amount_tests.cpp.test] Error 1
decryp2kanon commented 3 years ago

workaround:

nSatoshisPerK = nFeePaid * 1000 / nSize;

to

nSatoshisPerK = nFeePaid / nSize*1000;
decryp2kanon commented 3 years ago

<fuzz/util.h>

    // Avoid:
    // policy/feerate.cpp:28:34: runtime error: signed integer overflow: 34873208148477500 * 1000 cannot be represented in type 'long'
    //
    // Reproduce using CFeeRate(348732081484775, 10).GetFeePerK()
decryp2kanon commented 3 years ago

BTC has error too

// policy/feerate.cpp:28:34: runtime error: signed integer overflow: 34873208148477500 * 1000 cannot be represented in type 'long'

https://github.com/bitcoin/bitcoin/pull/18775

decryp2kanon commented 3 years ago
    // nSatoshisPerK = nFeePaid * 1000 / nSize;
    nSatoshisPerK = nFeePaid / nSize * 1000;
test/amount_tests.cpp(22): Entering test case "GetFeeTest"
test/amount_tests.cpp(78): error: in "amount_tests/GetFeeTest": check CFeeRate(CAmount(-1), 1000) == CFeeRate(-1) has failed
test/amount_tests.cpp(80): error: in "amount_tests/GetFeeTest": check CFeeRate(CAmount(1), 1000) == CFeeRate(1) has failed
test/amount_tests.cpp(83): error: in "amount_tests/GetFeeTest": check CFeeRate(CAmount(2), 1001) == CFeeRate(1) has failed
test/amount_tests.cpp(85): error: in "amount_tests/GetFeeTest": check CFeeRate(CAmount(26), 789) == CFeeRate(32) has failed
test/amount_tests.cpp(86): error: in "amount_tests/GetFeeTest": check CFeeRate(CAmount(27), 789) == CFeeRate(34) has failed
test/amount_tests.cpp(22): Leaving test case "GetFeeTest"; testing time: 31080us