From 76bf421a10abf2ca3cdd4e0c986764ccc522a4a8 Mon Sep 17 00:00:00 2001 From: Jeppe Frandsen Date: Thu, 12 Sep 2019 09:40:46 +0200 Subject: [PATCH] Added fix for duplication scenario (#16) * Added fix for duplication scenario * Minor whitespace fix --- .gitignore | 2 +- include/Hdlcpp.hpp | 3 ++- test/src/TestHdlcpp.cpp | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index a92c5f0..8e8e0ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .vscode/settings.json -.build-* +.build* diff --git a/include/Hdlcpp.hpp b/include/Hdlcpp.hpp index 858c696..e171202 100644 --- a/include/Hdlcpp.hpp +++ b/include/Hdlcpp.hpp @@ -290,7 +290,8 @@ class Hdlcpp { result = -EIO; } - discardBytes = i; + // Be sure to discard bytes (could be that destinationIndex > i in some cases) + discardBytes = destinationIndex > i ? destinationIndex : i; resetValues(); } diff --git a/test/src/TestHdlcpp.cpp b/test/src/TestHdlcpp.cpp index a7c0f56..ff77ddf 100644 --- a/test/src/TestHdlcpp.cpp +++ b/test/src/TestHdlcpp.cpp @@ -228,4 +228,22 @@ TEST_CASE_METHOD(HdlcppFixture, "hdlcpp test", "[single-file]") readBuffer.assign(frame1, frame1 + sizeof(frame1)); CHECK(hdlcpp->read(buffer, sizeof(buffer)) == 2); } + + SECTION("Test duplication scenario") + { + // Frames captured from duplication scenario + const uint8_t frame1[] = { 0x7e, 0xff }; + const uint8_t frame2[] = { 0x12, 0x4a, 0x07, 0x0a, 0x01, 0x03, 0x18, 0x07, 0x20, 0x64, 0xd5, 0x97 }; + const uint8_t frame3[] = { 0x7e }; + const uint8_t frame4[] = { 0x7e, 0xff, 0x61, 0x08, 0x82 }; + + readBuffer.assign(frame1, frame1 + sizeof(frame1)); + CHECK(hdlcpp->read(buffer, sizeof(buffer)) == -ENOMSG); + readBuffer.assign(frame2, frame2 + sizeof(frame2)); + CHECK(hdlcpp->read(buffer, sizeof(buffer)) == -ENOMSG); + readBuffer.assign(frame3, frame3 + sizeof(frame3)); + CHECK(hdlcpp->read(buffer, sizeof(buffer)) == 9); + readBuffer.assign(frame4, frame4 + sizeof(frame4)); + CHECK(hdlcpp->read(buffer, sizeof(buffer)) == -ENOMSG); + } }