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

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

Open
decryp2kanon opened this issue Oct 19, 2020 · 4 comments

Comments

@decryp2kanon
Copy link
Contributor

decryp2kanon commented Oct 19, 2020

// 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()

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
Copy link
Contributor Author

decryp2kanon commented Oct 19, 2020

workaround:

nSatoshisPerK = nFeePaid * 1000 / nSize;

to

nSatoshisPerK = nFeePaid / nSize*1000;

@decryp2kanon
Copy link
Contributor Author

<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
Copy link
Contributor Author

decryp2kanon commented Oct 20, 2020

BTC has error too

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

bitcoin/bitcoin#18775

@decryp2kanon
Copy link
Contributor Author

    // 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

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

No branches or pull requests

1 participant