From 607c4ec647edc29092d413d93e6078acd1e3a81f Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Fri, 24 Jun 2022 16:08:14 +0000 Subject: [PATCH] - fixed buffer overflow due to not consumming buffer git-svn-id: http://moon:8086/svn/software/trunk/libsrc/cpp@1080 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- radio/DeFormatter.cpp | 48 +++++++++++++++++++++++++++---------------- radio/DeFormatter.hpp | 6 +++--- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/radio/DeFormatter.cpp b/radio/DeFormatter.cpp index d0a2ceb..1cdc4af 100644 --- a/radio/DeFormatter.cpp +++ b/radio/DeFormatter.cpp @@ -39,24 +39,34 @@ void DeFormatter::prepare() void DeFormatter::process() { - // Process input - if (m_state == Idle) + while(m_buf_in->len()) { - process_sync(); - } - else if (m_state == Header) - { - process_header(); - } - else if (m_state == Data) - { - process_data(); + bool exit_loop = false; + + // Process input + if (m_state == Idle) + { + exit_loop = process_sync(); + } + else if (m_state == Header) + { + exit_loop = process_header(); + } + else if (m_state == Data) + { + exit_loop = process_data(); + } + + if (exit_loop) + { + break; + } } } -void DeFormatter::process_sync() +bool DeFormatter::process_sync() { - while(m_buf_in->len()) + if(m_buf_in->len()) { uint8_t data; m_buf_in->read(&data, 1); @@ -66,16 +76,16 @@ void DeFormatter::process_sync() if (m_sync_preamble == 0x55AA) { m_state = Header; - break; } } + return false; } -void DeFormatter::process_header() +bool DeFormatter::process_header() { if (m_buf_in->len() < sizeof(frame_hdr_t)) { - return; + return true; } m_buf_in->read((uint8_t*)&m_Hdr, sizeof(frame_hdr_t)); @@ -85,9 +95,10 @@ void DeFormatter::process_header() m_scrambler.process((uint8_t*)&m_Hdr.length, sizeof(m_Hdr.length)); m_dataCount = 0; m_state = Data; + return false; } -void DeFormatter::process_data() +bool DeFormatter::process_data() { if (m_Hdr.length != sizeof(m_data)) { @@ -96,7 +107,7 @@ void DeFormatter::process_data() if(m_buf_in->len() < m_Hdr.length) { - return; + return true; } m_buf_in->read(m_data, m_Hdr.length); @@ -118,6 +129,7 @@ void DeFormatter::process_data() m_frameStats.numTotalFrame++; m_dataCount = 0; m_state = Idle; + return false; } // -------------------------------------------------------------- diff --git a/radio/DeFormatter.hpp b/radio/DeFormatter.hpp index 7b199e5..e6a43c1 100644 --- a/radio/DeFormatter.hpp +++ b/radio/DeFormatter.hpp @@ -40,9 +40,9 @@ private: Processor::Buffer *m_buf_in; Processor::Buffer *m_buf_out; - void process_sync(); - void process_header(); - void process_data(); + bool process_sync(); + bool process_header(); + bool process_data(); }; // --------------------------------------------------------------