From ad82e8b0b4526d15a97f9c2a180f24e24dc4f7e6 Mon Sep 17 00:00:00 2001 From: Kayan Date: Mon, 27 May 2019 17:30:36 +0800 Subject: [PATCH 1/4] fc varint unpacking out of bound check --- libraries/fc | 2 +- unittests/misc_tests.cpp | 85 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/libraries/fc b/libraries/fc index 5eb88e0f5c3..3e1dc53f26d 160000 --- a/libraries/fc +++ b/libraries/fc @@ -1 +1 @@ -Subproject commit 5eb88e0f5c3d2881ae31ee020d76ff7570fb8503 +Subproject commit 3e1dc53f26d8f7194891a083c6a5cc7ac4fa0439 diff --git a/unittests/misc_tests.cpp b/unittests/misc_tests.cpp index fefed60a03a..02db974f1a4 100644 --- a/unittests/misc_tests.cpp +++ b/unittests/misc_tests.cpp @@ -1128,6 +1128,91 @@ BOOST_AUTO_TEST_CASE(stable_priority_queue_test) { } FC_LOG_AND_RETHROW() } +BOOST_AUTO_TEST_CASE(varint_unpack) { + struct stream { + char buffer[4096]; + size_t read_pos = 0; + size_t write_pos = 0; + stream() { + memset(buffer, 0, sizeof(buffer)); + } + void get(char &b) { + ++read_pos; + b = buffer[read_pos - 1]; + } + void write(char *data, size_t len) { + memcpy(&(buffer[write_pos]), data, len); + write_pos += len; + } + }; + + std::vector slist = { + 0, 1, -1, 63, -63, 64, -64, 127, -127, 128, -128, 255, -255, 256, -256, 32768, -32768, 65535, -65535, + 65536, -65536, -2147483648, -2147483647, 2147483646, 2147483647}; + + std::vector ulist = { + 0, 1, 2, 3, 63, 64, 127, 128, 65535, 65536, 2147483646, 2147483647, 2147483648, 4294967294, 4294967295}; + + stream s1; + fc::raw::pack(s1, slist); + std::vector slist2; + fc::raw::unpack(s1, slist2); + + BOOST_CHECK_EQUAL(slist.size(), slist2.size()); + BOOST_CHECK_EQUAL(s1.read_pos, s1.write_pos); + for (int i = 0; i < slist.size(); ++i) { + BOOST_CHECK_EQUAL(slist[i], slist2[i]); + } + + stream s2; + fc::raw::pack(s2, ulist); + std::vector ulist2; + fc::raw::unpack(s2, ulist2); + + BOOST_CHECK_EQUAL(ulist.size(), ulist2.size()); + BOOST_CHECK_EQUAL(s2.read_pos, s2.write_pos); + for (int i = 0; i < ulist.size(); ++i) { + BOOST_CHECK_EQUAL(ulist[i], ulist2[i]); + } + + auto unpack_uint = [](unsigned char *buf, size_t len, uint32_t expect_value) { + stream s; + s.write((char *)buf, len); + unsigned_int ui; + fc::raw::unpack(s, ui); + BOOST_CHECK_EQUAL((uint32_t)ui, expect_value); + BOOST_CHECK_EQUAL(s.read_pos, len); + }; + + auto unpack_int = [](unsigned char *buf, size_t len, int32_t expect_value) { + stream s; + s.write((char *)buf, len); + signed_int si; + fc::raw::unpack(s, si); + BOOST_CHECK_EQUAL((int32_t)si, expect_value); + BOOST_CHECK_EQUAL(s.read_pos, len); + }; + + unsigned char buf[] = { 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00}; + unpack_uint(buf, 5, UINT_MAX); + unpack_int(buf, 5, INT_MIN); + + unsigned char buf2[] = { 0xff, 0xff, 0xff, 0xff, 0x8f, 0x00}; // invalid stop bit + BOOST_CHECK_EXCEPTION( unpack_uint(buf2, 5, 0) , fc::assert_exception, [](const auto& e) { + return expect_assert_message(e, "unsigned_int out of bounds"); + }); + BOOST_CHECK_EXCEPTION( unpack_int(buf2, 5, 0) , fc::assert_exception, [](const auto& e) { + return expect_assert_message(e, "signed_int out of bounds"); + }); + + unsigned char buf3[] = { 0xff, 0xff, 0xff, 0xff, 0x10, 0x00}; // data out of bound + BOOST_CHECK_EXCEPTION( unpack_uint(buf3, 5, 0) , fc::assert_exception, [](const auto& e) { + return expect_assert_message(e, "unsigned_int out of bounds"); + }); + BOOST_CHECK_EXCEPTION( unpack_int(buf3, 5, 0) , fc::assert_exception, [](const auto& e) { + return expect_assert_message(e, "signed_int out of bounds"); + }); +} BOOST_AUTO_TEST_SUITE_END() From 2f56b59e02efaeabcc96a60581e76821be0f4d96 Mon Sep 17 00:00:00 2001 From: Kayan Date: Thu, 13 Jun 2019 15:58:11 +0800 Subject: [PATCH 2/4] relocate test case --- libraries/fc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/fc b/libraries/fc index 3e1dc53f26d..1135e96d3ca 160000 --- a/libraries/fc +++ b/libraries/fc @@ -1 +1 @@ -Subproject commit 3e1dc53f26d8f7194891a083c6a5cc7ac4fa0439 +Subproject commit 1135e96d3cabdb727354710bd1549ddf2775078b From ca1e105e2623cf347d140fb84fedaa95d4d40526 Mon Sep 17 00:00:00 2001 From: Kayan Date: Wed, 19 Jun 2019 10:56:07 +0800 Subject: [PATCH 3/4] relocate varint unpack test case --- libraries/fc | 2 +- unittests/misc_tests.cpp | 86 ---------------------------------------- 2 files changed, 1 insertion(+), 87 deletions(-) diff --git a/libraries/fc b/libraries/fc index 1135e96d3ca..1ca4da1f428 160000 --- a/libraries/fc +++ b/libraries/fc @@ -1 +1 @@ -Subproject commit 1135e96d3cabdb727354710bd1549ddf2775078b +Subproject commit 1ca4da1f428bac6c595d94444c9708e131d246b7 diff --git a/unittests/misc_tests.cpp b/unittests/misc_tests.cpp index 02db974f1a4..ef0dba5e2f7 100644 --- a/unittests/misc_tests.cpp +++ b/unittests/misc_tests.cpp @@ -1128,92 +1128,6 @@ BOOST_AUTO_TEST_CASE(stable_priority_queue_test) { } FC_LOG_AND_RETHROW() } -BOOST_AUTO_TEST_CASE(varint_unpack) { - struct stream { - char buffer[4096]; - size_t read_pos = 0; - size_t write_pos = 0; - stream() { - memset(buffer, 0, sizeof(buffer)); - } - void get(char &b) { - ++read_pos; - b = buffer[read_pos - 1]; - } - void write(char *data, size_t len) { - memcpy(&(buffer[write_pos]), data, len); - write_pos += len; - } - }; - - std::vector slist = { - 0, 1, -1, 63, -63, 64, -64, 127, -127, 128, -128, 255, -255, 256, -256, 32768, -32768, 65535, -65535, - 65536, -65536, -2147483648, -2147483647, 2147483646, 2147483647}; - - std::vector ulist = { - 0, 1, 2, 3, 63, 64, 127, 128, 65535, 65536, 2147483646, 2147483647, 2147483648, 4294967294, 4294967295}; - - stream s1; - fc::raw::pack(s1, slist); - std::vector slist2; - fc::raw::unpack(s1, slist2); - - BOOST_CHECK_EQUAL(slist.size(), slist2.size()); - BOOST_CHECK_EQUAL(s1.read_pos, s1.write_pos); - for (int i = 0; i < slist.size(); ++i) { - BOOST_CHECK_EQUAL(slist[i], slist2[i]); - } - - stream s2; - fc::raw::pack(s2, ulist); - std::vector ulist2; - fc::raw::unpack(s2, ulist2); - - BOOST_CHECK_EQUAL(ulist.size(), ulist2.size()); - BOOST_CHECK_EQUAL(s2.read_pos, s2.write_pos); - for (int i = 0; i < ulist.size(); ++i) { - BOOST_CHECK_EQUAL(ulist[i], ulist2[i]); - } - - auto unpack_uint = [](unsigned char *buf, size_t len, uint32_t expect_value) { - stream s; - s.write((char *)buf, len); - unsigned_int ui; - fc::raw::unpack(s, ui); - BOOST_CHECK_EQUAL((uint32_t)ui, expect_value); - BOOST_CHECK_EQUAL(s.read_pos, len); - }; - - auto unpack_int = [](unsigned char *buf, size_t len, int32_t expect_value) { - stream s; - s.write((char *)buf, len); - signed_int si; - fc::raw::unpack(s, si); - BOOST_CHECK_EQUAL((int32_t)si, expect_value); - BOOST_CHECK_EQUAL(s.read_pos, len); - }; - - unsigned char buf[] = { 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00}; - unpack_uint(buf, 5, UINT_MAX); - unpack_int(buf, 5, INT_MIN); - - unsigned char buf2[] = { 0xff, 0xff, 0xff, 0xff, 0x8f, 0x00}; // invalid stop bit - BOOST_CHECK_EXCEPTION( unpack_uint(buf2, 5, 0) , fc::assert_exception, [](const auto& e) { - return expect_assert_message(e, "unsigned_int out of bounds"); - }); - BOOST_CHECK_EXCEPTION( unpack_int(buf2, 5, 0) , fc::assert_exception, [](const auto& e) { - return expect_assert_message(e, "signed_int out of bounds"); - }); - - unsigned char buf3[] = { 0xff, 0xff, 0xff, 0xff, 0x10, 0x00}; // data out of bound - BOOST_CHECK_EXCEPTION( unpack_uint(buf3, 5, 0) , fc::assert_exception, [](const auto& e) { - return expect_assert_message(e, "unsigned_int out of bounds"); - }); - BOOST_CHECK_EXCEPTION( unpack_int(buf3, 5, 0) , fc::assert_exception, [](const auto& e) { - return expect_assert_message(e, "signed_int out of bounds"); - }); -} - BOOST_AUTO_TEST_SUITE_END() } // namespace eosio From f233ae4a25338408312e895c2f5d58ad007a1b2b Mon Sep 17 00:00:00 2001 From: Kayan Date: Wed, 19 Jun 2019 11:15:45 +0800 Subject: [PATCH 4/4] try it again --- unittests/misc_tests.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/unittests/misc_tests.cpp b/unittests/misc_tests.cpp index ef0dba5e2f7..fefed60a03a 100644 --- a/unittests/misc_tests.cpp +++ b/unittests/misc_tests.cpp @@ -1128,6 +1128,7 @@ BOOST_AUTO_TEST_CASE(stable_priority_queue_test) { } FC_LOG_AND_RETHROW() } + BOOST_AUTO_TEST_SUITE_END() } // namespace eosio