From ba2216e6e923c65798ccce988af485ffc7ac74d2 Mon Sep 17 00:00:00 2001 From: DDoSolitary Date: Sun, 10 Mar 2019 11:45:36 +0800 Subject: [PATCH 1/4] Avoid float precision problems of logarithm. --- src/utils/radiotap_writer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/radiotap_writer.cpp b/src/utils/radiotap_writer.cpp index 3a18187..7bd2d92 100644 --- a/src/utils/radiotap_writer.cpp +++ b/src/utils/radiotap_writer.cpp @@ -45,7 +45,7 @@ uint32_t calculate_padding(uint32_t alignment, uint32_t offset) { } uint32_t get_bit(uint32_t value) { - return log(value) / log(2); + return round(log2(value)); } RadioTapWriter::RadioTapWriter(vector& buffer) From 78aa7d178734694addd1becd929791cc80afa35f Mon Sep 17 00:00:00 2001 From: DDoSolitary Date: Sun, 10 Mar 2019 11:46:38 +0800 Subject: [PATCH 2/4] Correct option upper bound testing. --- src/utils/radiotap_writer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/radiotap_writer.cpp b/src/utils/radiotap_writer.cpp index 7bd2d92..cd17f44 100644 --- a/src/utils/radiotap_writer.cpp +++ b/src/utils/radiotap_writer.cpp @@ -54,7 +54,7 @@ RadioTapWriter::RadioTapWriter(vector& buffer) void RadioTapWriter::write_option(const RadioTap::option& option) { const uint32_t bit = get_bit(option.option()); - if (bit > RadioTapParser::MAX_RADIOTAP_FIELD) { + if (bit >= RadioTapParser::MAX_RADIOTAP_FIELD) { throw malformed_option(); } const bool is_empty = buffer_.empty(); From 2f16497bf84400b53d5527306895f70d6288ae20 Mon Sep 17 00:00:00 2001 From: DDoSolitary Date: Sun, 10 Mar 2019 11:48:56 +0800 Subject: [PATCH 3/4] Fix alignment padding calculation. --- src/utils/radiotap_writer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/utils/radiotap_writer.cpp b/src/utils/radiotap_writer.cpp index cd17f44..4bb3465 100644 --- a/src/utils/radiotap_writer.cpp +++ b/src/utils/radiotap_writer.cpp @@ -41,7 +41,8 @@ namespace Tins { namespace Utils { uint32_t calculate_padding(uint32_t alignment, uint32_t offset) { - return offset % alignment; + uint32_t extra = offset % alignment; + return extra == 0 ? 0 : alignment - extra; } uint32_t get_bit(uint32_t value) { From b803959e1153687081d0ccb940eb54f3e64ef2b6 Mon Sep 17 00:00:00 2001 From: DDoSolitary Date: Tue, 12 Mar 2019 14:18:59 +0800 Subject: [PATCH 4/4] Add tests for 2f16497bf84400b53d5527306895f70d6288ae20 and 78aa7d178734694addd1becd929791cc80afa35f. --- tests/src/radiotap_test.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/src/radiotap_test.cpp b/tests/src/radiotap_test.cpp index 04d6c6b..7670178 100644 --- a/tests/src/radiotap_test.cpp +++ b/tests/src/radiotap_test.cpp @@ -759,4 +759,27 @@ TEST_F(RadioTapTest, RadioTapWritingEmptyBuffer) { } +TEST_F(RadioTapTest, RadioTapWritingInvalidOption) { + vector buffer(4, 0); + RadioTapWriter writer(buffer); + uint8_t foo = 0; + RadioTap::option option((RadioTap::PresentFlags)(1 << RadioTapParser::MAX_RADIOTAP_FIELD), sizeof(foo), &foo); + EXPECT_THROW(writer.write_option(option), malformed_option); +} + +TEST_F(RadioTapTest, RadioTapWriterAlignment) { + vector buffer(4, 0); + RadioTapWriter writer(buffer); + uint8_t flags = 10; + uint8_t xchannel[sizeof(RadioTap::xchannel_type)] = { + 1, 2, 3, 4, 5, 6, 7, 8 + }; + writer.write_option(RadioTap::option(RadioTap::FLAGS, sizeof(flags), &flags)); + writer.write_option(RadioTap::option(RadioTap::XCHANNEL, sizeof(xchannel), xchannel)); + vector expected = { + 2, 0, 4, 0, 10, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8 + }; + EXPECT_EQ(buffer, expected); +} + #endif // TINS_HAVE_DOT11