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

Ub fixes #367

Merged
merged 5 commits into from
Oct 24, 2023
Merged

Ub fixes #367

merged 5 commits into from
Oct 24, 2023

Conversation

syyyr
Copy link
Contributor

@syyyr syyyr commented Oct 18, 2023

No description provided.

@syyyr syyyr marked this pull request as draft October 18, 2023 11:42
@syyyr
Copy link
Contributor Author

syyyr commented Oct 18, 2023

Needs tests... I'll add them later

@syyyr syyyr force-pushed the ub-fixes branch 3 times, most recently from 6f0d9d3 to 4e64062 Compare October 24, 2023 09:27
@syyyr
Copy link
Contributor Author

syyyr commented Oct 24, 2023

Ok, now we have tests. @fvacek If you think you can simplify the fromChainpack test, feel free to do so, I have just copied the input from the fuzzer.

@syyyr syyyr marked this pull request as ready for review October 24, 2023 11:23
@@ -2,6 +2,9 @@

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
std::string err;
auto rpc_value = shv::chainpack::RpcValue::fromChainPack({reinterpret_cast<const char*>(data), size}, &err);
try {
auto rpc_value = shv::chainpack::RpcValue::fromChainPack({reinterpret_cast<const char*>(data), size}, &err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shv::chainpack::RpcValue::fromChainPack() should not throw if err is provided

RpcValue::Int toInt() const override { return static_cast<RpcValue::Int>(m_value); }
RpcValue::Int toInt() const override {
if (!is_representable_as_int(m_value)) {
throw std::runtime_error("This double is not representable as int");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, if I adding throw to to*() is good idea, what about return nan

syyyr added 5 commits October 24, 2023 14:40
clang-tidy is having problems with this, hopefully this will shut it
up.
/home/vk/git/libshv/libshvchainpack/c/cchainpack.c:467:46: runtime error: shift exponent 151 is too large for 64-bit type 'uint64_t' (aka 'unsigned long')
    0 0x7fe8580cd0f0 in unpack_int /home/vk/git/libshv/libshvchainpack/c/cchainpack.c:467:46
    1 0x7fe8580caefd in cchainpack_unpack_next /home/vk/git/libshv/libshvchainpack/c/cchainpack.c:587:4
    2 0x7fe857ed592d in shv::chainpack::ChainPackReader::unpackNext() /home/vk/git/libshv/libshvchainpack/src/chainpack/chainpackreader.cpp:50:2
    3 0x7fe857ed96d2 in shv::chainpack::ChainPackReader::read(shv::chainpack::RpcValue&) /home/vk/git/libshv/libshvchainpack/src/chainpack/chainpackreader.cpp:105:2
    4 0x7fe857edeb90 in shv::chainpack::ChainPackReader::parseMap(shv::chainpack::RpcValue&) /home/vk/git/libshv/libshvchainpack/src/chainpack/chainpackreader.cpp:239:3
    5 0x7fe857ed99aa in shv::chainpack::ChainPackReader::read(shv::chainpack::RpcValue&) /home/vk/git/libshv/libshvchainpack/src/chainpack/chainpackreader.cpp:117:3
    6 0x7fe857ed53f4 in shv::chainpack::ChainPackReader::operator>>(shv::chainpack::RpcValue&) /home/vk/git/libshv/libshvchainpack/src/chainpack/chainpackreader.cpp:38:2
    7 0x7fe857f959a5 in shv::chainpack::RpcValue::fromChainPack(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*) /home/vk/git/libshv/libshvchainpack/src/chainpack/rpcvalue.cpp:848:7
    8 0x564b6b867aa5 in LLVMFuzzerTestOneInput /home/vk/git/libshv/libshvchainpack/fuzz/fromChainpack.cpp:5:22
    9 0x564b6b70b758 in fuzzer::Fuzzer::ExecuteCallback(unsigned char const*, unsigned long) (/home/vk/git/libshv/build/libshvchainpack/fuzz_fromChainpack+0x5a758) (BuildId: d1fecbc5a170f40512cda964cb241424ad6c48ea)
    10 0x564b6b70c430 in fuzzer::Fuzzer::RunOne(unsigned char const*, unsigned long, bool, fuzzer::InputInfo*, bool, bool*) (/home/vk/git/libshv/build/libshvchainpack/fuzz_fromChainpack+0x5b430) (BuildId: d1fecbc5a170f40512cda964cb241424ad6c48ea)
    11 0x564b6b70d4c1 in fuzzer::Fuzzer::MutateAndTestOne() (/home/vk/git/libshv/build/libshvchainpack/fuzz_fromChainpack+0x5c4c1) (BuildId: d1fecbc5a170f40512cda964cb241424ad6c48ea)
    12 0x564b6b70e2e7 in fuzzer::Fuzzer::Loop(std::vector<fuzzer::SizedFile, std::allocator<fuzzer::SizedFile>>&) (/home/vk/git/libshv/build/libshvchainpack/fuzz_fromChainpack+0x5d2e7) (BuildId: d1fecbc5a170f40512cda964cb241424ad6c48ea)
    13 0x564b6b6ee7d2 in fuzzer::FuzzerDriver(int*, char***, int (*)(unsigned char const*, unsigned long)) (/home/vk/git/libshv/build/libshvchainpack/fuzz_fromChainpack+0x3d7d2) (BuildId: d1fecbc5a170f40512cda964cb241424ad6c48ea)
    14 0x564b6b6d8537 in main (/home/vk/git/libshv/build/libshvchainpack/fuzz_fromChainpack+0x27537) (BuildId: d1fecbc5a170f40512cda964cb241424ad6c48ea)
    15 0x7fe8578e7ccf  (/usr/lib/libc.so.6+0x27ccf) (BuildId: 8bfe03f6bf9b6a6e2591babd0bbc266837d8f658)
    16 0x7fe8578e7d89 in __libc_start_main (/usr/lib/libc.so.6+0x27d89) (BuildId: 8bfe03f6bf9b6a6e2591babd0bbc266837d8f658)
    17 0x564b6b6d8574 in _start (/home/vk/git/libshv/build/libshvchainpack/fuzz_fromChainpack+0x27574) (BuildId: d1fecbc5a170f40512cda964cb241424ad6c48ea)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/vk/git/libshv/libshvchainpack/c/cchainpack.c:467:46 in
/home/vk/git/libshv/libshvchainpack/src/chainpack/rpcvalue.cpp:192:75: runtime error: inf is outside the range of representable values of type 'int'
    0x7f20f57c905d in shv::chainpack::ChainPackDecimal::toInt() const /home/vk/git/libshv/libshvchainpack/src/chainpack/rpcvalue.cpp:192:75

/home/vk/git/libshv/libshvchainpack/src/chainpack/rpcvalue.cpp:192:75: runtime error: -3.584e+39 is outside the range of representable values of type 'int'
    0x7f5cbf9c945d in shv::chainpack::ChainPackDecimal::toInt() const /home/vk/git/libshv/libshvchainpack/src/chainpack/rpcvalue.cpp:192:75
@fvacek fvacek merged commit b0f19e6 into master Oct 24, 2023
6 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