diff --git a/src/modules/systemlib/mixer/mixer.cpp b/src/modules/systemlib/mixer/mixer.cpp index a8a9b14eb2..6333f3d95a 100644 --- a/src/modules/systemlib/mixer/mixer.cpp +++ b/src/modules/systemlib/mixer/mixer.cpp @@ -55,6 +55,9 @@ #include "mixer.h" +#define debug(fmt, args...) do { } while(0) +//#define debug(fmt, args...) do { printf("[mixer] " fmt "\n", ##args); } while(0) + Mixer::Mixer(ControlCallback control_cb, uintptr_t cb_handle) : _next(nullptr), _control_cb(control_cb), @@ -151,6 +154,28 @@ Mixer::skipline(const char *buf, unsigned &buflen) return nullptr; } +bool +Mixer::string_well_formed(const char *buf, unsigned &buflen) +{ + /* enforce that the mixer ends with a new line */ + for (int i = buflen - 1; i >= 0; i--) { + if (buf[i] == '\0') { + continue; + } + + /* require a space or newline at the end of the buffer, fail on printable chars */ + if (buf[i] == '\n' || buf[i] == '\r') { + /* found a line ending, so no split symbols / numbers. good. */ + return true; + } + + } + + debug("pre-parser rejected: No newline in buf"); + + return false; +} + /****************************************************************************/ NullMixer::NullMixer() : @@ -186,21 +211,9 @@ NullMixer::from_text(const char *buf, unsigned &buflen) { NullMixer *nm = nullptr; - /* enforce that the mixer ends with space or a new line */ - for (int i = buflen - 1; i >= 0; i--) { - if (buf[i] == '\0') { - continue; - } - - /* require a space or newline at the end of the buffer, fail on printable chars */ - if (buf[i] == ' ' || buf[i] == '\n' || buf[i] == '\r') { - /* found a line ending or space, so no split symbols / numbers. good. */ - break; - - } else { - return nm; - } - + /* enforce that the mixer ends with a new line */ + if (!string_well_formed(buf, buflen)) { + return nullptr; } if ((buflen >= 2) && (buf[0] == 'Z') && (buf[1] == ':')) { diff --git a/src/modules/systemlib/mixer/mixer.h b/src/modules/systemlib/mixer/mixer.h index 9ade606eaa..2a6c1cbb37 100644 --- a/src/modules/systemlib/mixer/mixer.h +++ b/src/modules/systemlib/mixer/mixer.h @@ -262,6 +262,11 @@ protected: */ static const char *skipline(const char *buf, unsigned &buflen); + /** + * Check wether the string is well formed and suitable for parsing + */ + static bool string_well_formed(const char *buf, unsigned &buflen); + private: /* do not allow to copy due to pointer data members */