Skip to content

Commit

Permalink
read: clear buffer when full and invalid
Browse files Browse the repository at this point in the history
If for some reason the read buffer got filled with data, but no complete
hdlc frame was present, the library would get stuck. This is because the
buffer is only ever erased from in the good case scenario.

As a very simple workaround, albeit not a great one, just drop the
entire buffer if it is not possible to deframe the data it contains
_and_ it doesn't have room left for more data.

Ideally, the recovery mechanism would only drop bytes until a start byte
is spotted to make it more graceful on a buffer containing a partial
frame.
This is, however, already an edge case (otherwise users of the library
should be experiencing these lockups at the moment), and thus it seems
reasonable to just go for a harsh but simple workaround.
  • Loading branch information
Emil-Juhl committed May 1, 2024
1 parent 4297950 commit a1dc3a7
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/Hdlcpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ class Hdlcpp {
result = decode(address, readFrame, readSequenceNumber, readBuffer.dataSpan(), buffer, discardBytes);
if (result >= 0) {
doTransportRead = false;
} else if (readBuffer.unusedSpan().size() == 0) {
// Drop the buffer in an attempt to recover from getting
// filled with an invalid message.
// FIXME: really start/stop codes should be tracked to
// implement this in a more fail-safe way
readBuffer.erase(readBuffer.begin(), readBuffer.end());
}
}

Expand Down

0 comments on commit a1dc3a7

Please sign in to comment.