Mixer: add string wconditioning check.

This introduces a correctly designed pre-check for the input parsers. This fixes the mixer unit test and should fix all issues occuring on real hardware.
;
This commit is contained in:
Lorenz Meier 2017-01-01 18:34:08 +01:00 committed by Daniel Agar
parent 0810bcfe8e
commit ff18140cf4
2 changed files with 33 additions and 15 deletions

View File

@ -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] == ':')) {

View File

@ -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 */