some fixes, check for preamble size

This commit is contained in:
Robin Müller 2020-06-04 19:50:26 +02:00
parent 6ff1cf46c5
commit 4d59ddc3db
2 changed files with 15 additions and 9 deletions

View File

@ -17,8 +17,8 @@ ServiceInterfaceBuffer::ServiceInterfaceBuffer(std::string setMessage,
// Set pointers if the stream is buffered. // Set pointers if the stream is buffered.
setp( buf, buf + BUF_SIZE ); setp( buf, buf + BUF_SIZE );
} }
preamble.reserve(96); preamble.reserve(MAX_PREAMBLE_SIZE);
preamble.resize(96); preamble.resize(MAX_PREAMBLE_SIZE);
} }
void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) { void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
@ -36,7 +36,6 @@ void ServiceInterfaceBuffer::putChars(char const* begin, char const* end) {
else { else {
printChar(begin, false); printChar(begin, false);
} }
} }
} }
@ -80,8 +79,6 @@ int ServiceInterfaceBuffer::sync(void) {
size_t preambleSize = 0; size_t preambleSize = 0;
auto preamble = getPreamble(&preambleSize); auto preamble = getPreamble(&preambleSize);
uint8_t debugArray[96];
memcpy(debugArray, preamble.data(), preambleSize);
// Write logMessage and time // Write logMessage and time
this->putChars(preamble.data(), preamble.data() + preambleSize); this->putChars(preamble.data(), preamble.data() + preambleSize);
// Handle output // Handle output
@ -98,7 +95,6 @@ bool ServiceInterfaceBuffer::isBuffered() const {
std::string ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) { std::string ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
Clock::TimeOfDay_t loggerTime; Clock::TimeOfDay_t loggerTime;
Clock::getDateAndTime(&loggerTime); Clock::getDateAndTime(&loggerTime);
std::string preamble (96, 0);
size_t currentSize = 0; size_t currentSize = 0;
char* parsePosition = &preamble[0]; char* parsePosition = &preamble[0];
if(addCrToPreamble) { if(addCrToPreamble) {
@ -113,7 +109,12 @@ std::string ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
loggerTime.second, loggerTime.second,
loggerTime.usecond /1000); loggerTime.usecond /1000);
if(charCount < 0) { if(charCount < 0) {
printf("ServiceInterfaceBuffer: Failure parsing preamble"); printf("ServiceInterfaceBuffer: Failure parsing preamble\r\n");
return "";
}
if(charCount > MAX_PREAMBLE_SIZE) {
printf("ServiceInterfaceBuffer: Char count too large for maximum "
"preamble size");
return ""; return "";
} }
currentSize += charCount; currentSize += charCount;

View File

@ -20,6 +20,8 @@ class ServiceInterfaceBuffer:
public std::streambuf { public std::streambuf {
friend class ServiceInterfaceStream; friend class ServiceInterfaceStream;
public: public:
static constexpr uint8_t MAX_PREAMBLE_SIZE = 40;
ServiceInterfaceBuffer(std::string setMessage, bool addCrToPreamble, ServiceInterfaceBuffer(std::string setMessage, bool addCrToPreamble,
bool buffered, bool errStream, uint16_t port); bool buffered, bool errStream, uint16_t port);
@ -41,14 +43,17 @@ private:
std::string preamble; std::string preamble;
// For EOF detection // For EOF detection
typedef std::char_traits<char> Traits; typedef std::char_traits<char> Traits;
//! This is useful for some terminal programs which do not have //! This is useful for some terminal programs which do not have
//! implicit carriage return with newline characters. //! implicit carriage return with newline characters.
bool addCrToPreamble; bool addCrToPreamble;
//! Specifies whether the stream operates in buffered or unbuffered mode.
bool buffered;
//! This specifies to print to stderr and work in unbuffered mode. //! This specifies to print to stderr and work in unbuffered mode.
bool errStream; bool errStream;
bool buffered;
// Work in buffer mode. It is also possible to work without buffer. //! Needed for buffered mode.
static size_t const BUF_SIZE = 128; static size_t const BUF_SIZE = 128;
char buf[BUF_SIZE]; char buf[BUF_SIZE];